【问题标题】:Android - a distance between real drawing and finger movementAndroid - 真实绘图和手指运动之间的距离
【发布时间】:2017-03-14 23:25:13
【问题描述】:

我是 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);
}}

mainActivity 看起来像这样:

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;
}}

结果是我的手指点和屏幕上的真实绘图点之间有很大的距离,如下所示:

on screen result

蓝线是我的手指,红色边框是屏幕上的真实绘图。有谁知道这是什么原因?非常感谢!

【问题讨论】:

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

标签: android user-interface view draw touch-event


【解决方案1】:

Activity 和子 View 的坐标原点不同。我做了一些更正:

   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;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多