MOVE事件的Andr​​oid速度

MOVE事件的Andr​​oid速度

本文介绍了得到一个onTouch ACTION_MOVE事件的Andr​​oid速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是pretty的简单的事。用户将画面上他的手指和拖动它在屏幕上。有两个事件射击onTouch:

  • MotionEvent.ACTION_DOWN
  • MotionEvent.ACTION_MOVE

现在,我该怎么计算ACTION_MOVE手势的速度?用户拖动手指的手势时慢或更快,所以我想我需要计算速度之间的两个中间感动点:本lastTouchedPointX,lastTouchedPointY和event.getX(),event.getY()

有没有人这样做过?

解决方案

  @覆盖
        公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面){


            如果(event.getAction()== MotionEvent.ACTION_DOWN){

                oldX = event.getX();
                oldY = event.getY();
                    //启动定时器

            }否则,如果(event.getAction()== MotionEvent.ACTION_UP){

                //长timerTime =的getTime两个事件之间向下向上
                下一页末= event.getX();
                newY = event.getY();

                浮距离=的Math.sqrt((下一页末-oldX)*(下一页末-oldX)+(newY-oldY)*(newY-oldY));
                浮动速度=距离/ timerTime;

            }
}
 

This should be a pretty simple thing to do. The user puts his finger on the screen and drags it around the screen. There are two events firing on onTouch:

  • MotionEvent.ACTION_DOWN
  • MotionEvent.ACTION_MOVE

Now, how can I calculate the speed of the ACTION_MOVE gesture ? The user drags the finger slower or faster during a gesture, so I think I need to calculate the speed between two intermediate touched points: the lastTouchedPointX,lastTouchedPointY and the event.getX(),event.getY().

Has anyone done this before ?

解决方案
@Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {


            if(event.getAction() == MotionEvent.ACTION_DOWN) {

                oldX = event.getX();
                oldY = event.getY();
                    //start timer

            } else if (event.getAction() == MotionEvent.ACTION_UP) {

                //long timerTime = getTime between two event down to Up
                newX = event.getX();
                newY = event.getY();

                float distance = Math.sqrt((newX-oldX) * (newX-oldX) + (newY-oldY) * (newY-oldY));
                float speed = distance / timerTime;

            }
}

这篇关于得到一个onTouch ACTION_MOVE事件的Andr​​oid速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:22