在Android中获取onTouch ACTION_MOVE事件的速度

18

这应该是一个相当简单的事情。用户将手指放在屏幕上并将手指拖动到屏幕上。onTouch上会触发两个事件:

  • MotionEvent.ACTION_DOWN
  • MotionEvent.ACTION_MOVE

现在,我怎么计算ACTION_MOVE手势的速度?用户在手势期间拖动手指的速度可能较慢或较快,因此我认为需要计算两个中间触摸点之间的速度:lastTouchedPointX,lastTouchedPointY和event.getX(),event.getY()。

有人以前做过这件事吗?

4个回答

21

您需要的可以通过使用标准的VelocityTracker类来实现。有关在跟踪移动时Google的用户输入最佳实践的更多详细信息,请参见此处。下面的大部分代码(演示了通过显示每次甩动/移动的X和Y轴速度来使用VelocityTracker)取自上述资源链接:

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.VelocityTrackerCompat;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.TextView;

public class MainActivity extends Activity {

    private VelocityTracker mVelocityTracker = null;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTextView = new TextView(this);
        mTextView.setText("Move finger on screen to get velocity.");
        setContentView(mTextView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (mVelocityTracker == null) {

                // Retrieve a new VelocityTracker object to watch the velocity
                // of a motion.
                mVelocityTracker = VelocityTracker.obtain();
            } else {

                // Reset the velocity tracker back to its initial state.
                mVelocityTracker.clear();
            }

            // Add a user's movement to the tracker.
            mVelocityTracker.addMovement(event);
            break;
        case MotionEvent.ACTION_MOVE:
            mVelocityTracker.addMovement(event);
            // When you want to determine the velocity, call
            // computeCurrentVelocity(). Then call getXVelocity()
            // and getYVelocity() to retrieve the velocity for each pointer ID.
            mVelocityTracker.computeCurrentVelocity(1000);

            // Log velocity of pixels per second
            // Best practice to use VelocityTrackerCompat where possible.
            mTextView.setText("X velocity: "
                    + VelocityTrackerCompat.getXVelocity(mVelocityTracker,
                            pointerId)
                    + "\nY velocity: "
                    + VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                            pointerId));
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_CANCEL:
            // Return a VelocityTracker object back to be re-used by others.
            mVelocityTracker.recycle();
            break;
        }
        return true;
    }

}

13
@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;

            }
}

@wadali 它是在按下和松开动作之间的时间戳,查看注释。 - Dharmendra
距离 = Math.hypot(newX-oldX,newY-newY)https://zh.wikipedia.org/wiki/勾股定理 - 최봉재
1
@bongjaechoe 两者是相同的 double distance = Math.sqrt((newX - olddeltaX) * (newX - olddeltaX) + (newY - olddeltaY) * (newY - olddeltaY));double distanceH = Math.hypot(newX - olddeltaX, newY - olddeltaY); - Dart

2

这是一个关于你想要执行计算的精度的问题。

然而,基本的步骤是获取每个对应的ACTION_DOWN、ACTION_UP时间戳,并计算它们之间的时间差。

然后需要确定被覆盖的像素。这可以通过简单的三角学来完成。

当你有了时间差和被覆盖的像素,就可以计算每秒多少像素的速度,作为两个点(按下和抬起)的平均值。

你可以对每个手指在屏幕上移动时的位置进行这个操作,以获得更好的结果。


-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接