Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-真实绘图和手指运动之间的距离_Android_User Interface_View_Draw_Touch Event - Fatal编程技术网

Android-真实绘图和手指运动之间的距离

Android-真实绘图和手指运动之间的距离,android,user-interface,view,draw,touch-event,Android,User Interface,View,Draw,Touch Event,我是Android开发的新手,这里有一个问题,因为我还没有找到关于Stackoverflow的任何答案 我编写了一个简单的代码,让用户用一个手指在屏幕上绘制一个边界框(从左上角到右下角) 类DrawView如下所示: public class DrawView extends View { Paint paint = new Paint(); public DrawView(Context context) { super(context); } public Point startP

我是Android开发的新手,这里有一个问题,因为我还没有找到关于Stackoverflow的任何答案

我编写了一个简单的代码,让用户用一个手指在屏幕上绘制一个边界框(从左上角到右下角)

类DrawView如下所示:

public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
    super(context);
}
public Point startPoint = new Point();
public Point endPoint = new Point();

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(startPoint.x,startPoint.y,endPoint.x,endPoint.y,paint);
}}
public class MainActivity extends AppCompatActivity {

DrawView drawView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    drawView.startPoint.set(0,0);
    drawView.endPoint.set(0,0);
    setContentView(drawView);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getX();
    int y = (int)event.getY();

    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:
            drawView.startPoint.set(x,y);
            break;
        case MotionEvent.ACTION_UP:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
    }

    // tell the View that we handled the event
    return true;
}}
主要活动如下所示:

public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
    super(context);
}
public Point startPoint = new Point();
public Point endPoint = new Point();

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(startPoint.x,startPoint.y,endPoint.x,endPoint.y,paint);
}}
public class MainActivity extends AppCompatActivity {

DrawView drawView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    drawView.startPoint.set(0,0);
    drawView.endPoint.set(0,0);
    setContentView(drawView);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getX();
    int y = (int)event.getY();

    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:
            drawView.startPoint.set(x,y);
            break;
        case MotionEvent.ACTION_UP:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
    }

    // tell the View that we handled the event
    return true;
}}
结果是我的手指点和屏幕上的真实绘图点之间有一个很长的距离,如下所示:

public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
    super(context);
}
public Point startPoint = new Point();
public Point endPoint = new Point();

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(startPoint.x,startPoint.y,endPoint.x,endPoint.y,paint);
}}
public class MainActivity extends AppCompatActivity {

DrawView drawView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    drawView.startPoint.set(0,0);
    drawView.endPoint.set(0,0);
    setContentView(drawView);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getX();
    int y = (int)event.getY();

    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:
            drawView.startPoint.set(x,y);
            break;
        case MotionEvent.ACTION_UP:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            drawView.endPoint.set(x,y);
            drawView.invalidate();
            break;
    }

    // tell the View that we handled the event
    return true;
}}


蓝色的线是我的手指,红色的边框是屏幕上的真实图形。有人知道原因吗?非常感谢

活动和子视图的坐标原点不相同。我做了一些更正:

   public class MainActivity extends AppCompatActivity implements View.OnTouchListener{

        DrawView drawView;         

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            drawView = new DrawView(this);
            drawView.setBackgroundColor(Color.WHITE);
            drawView.startPoint.set(0, 0);
            drawView.endPoint.set(0, 0);
            setContentView(drawView);
            drawView.setOnTouchListener(this);
        }



        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int eventAction = motionEvent.getAction();


            // you may need the x/y location
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();

            // put your code in here to handle the event
            switch (eventAction) {
                case MotionEvent.ACTION_DOWN:
                    drawView.startPoint.set(x, y);
                    break;
                case MotionEvent.ACTION_UP:
                    drawView.endPoint.set(x, y);
                    drawView.invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    drawView.endPoint.set(x, y);
                    drawView.invalidate();
                    break;
            }

            // tell the View that we handled the event
            return true;
        }
    }

这是因为
视图
的坐标与
活动
的坐标不同。在
视图中处理触摸事件。也就是说,将
onTouchEvent()
方法移动到您的
DrawView