简体   繁体   English

衡量您在Android中使用onTouchEvent拖了多远?

[英]Measure how far you dragged with onTouchEvent in Android?

Is it possible to measure how far someone has dragged their finger on the display using onTouchEvent? 是否可以使用onTouchEvent来衡量某人在显示屏上拖动手指的距离? Any ideas would be appreciated. 任何想法,将不胜感激。

You can use ACTION_DOWN and ACTION_UP to store start and end point that finger touched and calculate the distance that dragged. 您可以使用ACTION_DOWNACTION_UP来存储手指触摸的起点和终点,并计算拖动的距离。

For example: 例如:

Point p1;
Point p2;

View view = new View(this);

view.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {
         if(event.getAction() == MotionEvent.ACTION_DOWN)
            p1 = new Point((int) event.getX(), (int) event.getY());
         else if(event.getAction() == MotionEvent.ACTION_UP)
            p2 = new Point((int) event.getX(), (int) event.getY());

         return false;
      }
});

and the distance is equal to Math.sqrt(Math.pow(p1.x-p2.x, 2) + Math.pow(p1.y-p2.y, 2)) . 并且该距离等于Math.sqrt(Math.pow(p1.x-p2.x, 2) + Math.pow(p1.y-p2.y, 2)) And also, if you want to calculate the distance that dragged in any time, you can store the second position if(event.getAction() == MotionEvent.ACTION_MOVE) . 而且,如果要计算在任何时间拖动的距离,则可以存储第二个位置if(event.getAction() == MotionEvent.ACTION_MOVE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM