Ich verwende Leinwand, um eine einfache Zeichnung App zu machen. Ich verwende onTouchEvent, um Berührungsereignisse zu behandeln. Ich bin mit einem Problem konfrontiert, das auf der vertikalen Achse gezeichnet wurde. Der Punkt unterscheidet sich von der berührten Position. Die vertikale Trennung zwischen der berührten Position und der gezeichneten Position nimmt zu, wenn ich mich nach oben bewege. Um mein Problem zu verdeutlichen, füge ich einen Screenshot meiner App an. android leinwand nicht zeichnung auf berührte position
Die blaue Linie zeigt die tatsächlich berührte Position und die rot gezeichnete Position.
Hier ist mein Code Mainactivity
public class MainActivity extends AppCompatActivity{
Path mPath;
Canvas canvas;
Paint paint;
float pointX;
float pointY;
int height;
RelativeLayout layout;
int layoutHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(RelativeLayout)findViewById(R.id.main_layout);
DisplayMetrics dp=getResources().getDisplayMetrics();
WindowManager windowManager=getWindowManager();
height=windowManager.getDefaultDisplay().getHeight();
int width =windowManager.getDefaultDisplay().getWidth();
Bitmap bg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
layout.setBackground(new BitmapDrawable(bg));
canvas=new Canvas(bg);
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
mPath=new Path();
canvas.drawPath(mPath,paint);
canvas.drawBitmap(bg,0,0,paint);
}
public void clearCanvas(View v) {
mPath.reset();
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
layout.invalidate();
}
//override the onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
pointX = event.getX();
pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.v("TAG"," action down x:"+pointX+" y:"+pointY);
mPath.moveTo(pointX,pointY);
break;
case MotionEvent.ACTION_MOVE:
Log.v("TAG"," actionmove x:"+pointX+" y:"+pointY);
mPath.lineTo(pointX,pointY);
canvas.drawPath(mPath,paint);
break;
case MotionEvent.ACTION_UP:
break;
}
layout.invalidate();
return true;
}
}
xml Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vikash.mydrawingapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:onClick="clearCanvas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
schreiben Sie hier Ihren Code –
@DixitPanchal Ich habe Code hinzugefügt. Bitte helfe –
Anstatt die 'View.getX()'/'View.getY()' zu verwenden, hast du 'View.getRawX()'/'View.getRawY()'? Auf diese Weise können Sie den tatsächlichen Pixel berühren und eine Linie mit dieser Koordinate platzieren. Soweit ich das beurteilen kann, besteht das Problem darin, dass die Position (x), auf die mit Ihrem Bildschirm geklickt wurde, eng mit der (x) der Ansicht zusammenhängt, aber die (y) angeklickt wird durch die andere "versetzt" Ansicht, die Titelleiste. – Bonatti