android画布未在触摸位置绘制

android画布未在触摸位置绘制,android,canvas,Android,Canvas,我正在使用画布制作一个简单的绘图应用程序。我正在使用onTouchEvent处理触摸事件。我在垂直轴上遇到一个问题,画的点与触摸的位置不同。当我向上移动时,接触位置和绘制位置之间的垂直间隔增加。为了让我的问题更清楚,我附上了我的应用程序截图 蓝线显示实际触摸位置和红色绘制位置 这是我的密码 主要活动 public class MainActivity extends AppCompatActivity{ Path mPath; Canvas canvas; Paint p

我正在使用画布制作一个简单的绘图应用程序。我正在使用onTouchEvent处理触摸事件。我在垂直轴上遇到一个问题,画的点与触摸的位置不同。当我向上移动时,接触位置和绘制位置之间的垂直间隔增加。为了让我的问题更清楚,我附上了我的应用程序截图

蓝线显示实际触摸位置和红色绘制位置

这是我的密码 主要活动

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布局

<?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>

您的问题是画布X轴的位置与屏幕X轴的位置相同,但画布Y轴与屏幕Y轴偏移(“MyDrawingApp”栏设置该偏移与其高度)

发生的情况是,当您“getY()”时,它返回触摸的位置,并将整个屏幕而不是画布作为参考

一种解决方案是获取画布在屏幕上的位置,并将其减去“getY()”值。这样做,你会得到一个触摸你的画布内

我希望你能理解我,我不是英国人

问候语

使用:event.getRawX()

返回此事件的原始X坐标。对于屏幕上的触摸事件,这是在为包含窗口和视图调整事件之前,事件在屏幕上的原始位置

UPD: 我只是试着重复你的应用程序,所有的工作正常

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <com.example.Test
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

发布你的代码here@DixitPanchal我添加了代码。请帮助不要使用
View.getX()
/
View.getY()
,您是否考虑过
View.getRawX()
/
View.getRawY()
?通过这种方式,您可以触摸实际像素,并使用该坐标放置一条线。据我所知,您遇到的问题是,(x)单击的位置与屏幕相关,与视图的(x)密切相关,但(y)单击的位置被另一个视图标题栏“偏移”。@Bonatti我尝试过getRawX()/getRawY(),但没有观察到任何差异,因为
视图几乎与屏幕大小相同,你将很难“解决问题”。将
Canvas
大小更改为更长方形,偏移量将很容易看到,放置一个“400dp*800dp”视图,记录
getX()
getY()
值,并与
getRawX()
getRawY()
进行比较。我已经尝试过减去偏移量。但这没用。在这种情况下,与上述情况正好相反的情况发生了,即画布和轴的差异更多地出现在底部,而较少出现在顶部,但这无助于这项工作的完美。我试过这个。当我在mainlayout的背景中添加画布时,我遇到了一个问题。如果我像你回答的那样使用这个画布,它会非常好用。谢谢你的帮助:)
public class Test extends View {

    private Path path = new Path();
    private Paint paint = new Paint();
    {
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(4);
        paint.setStyle(Paint.Style.STROKE);
    }

    public Test(Context context) {
        super(context);
    }

    public Test(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Test(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Test(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(event.getX(), event.getY());
                break;
        }
        invalidate();
        return true;
    }

}