简体   繁体   English

如何检测Android中的触摸速度

[英]How to detect touch speed in Android

I added a drawable to my app that I can move with touch and based on the finger move speed, I'd like to do different actions.我在我的应用程序中添加了一个 drawable,我可以通过触摸移动它,并且根据手指移动速度,我想做不同的动作。

I checked the events and I can only use MotionEvent.ACTION_MOVE to detect if there is movement.我检查了事件,我只能使用 MotionEvent.ACTION_MOVE 来检测是否有移动。 So I save the timestamp of the last movement and get the actual and I calculate the delta distance of the moving and use the formula of所以我保存了最后一次移动的时间戳并获得了实际值,我计算了移动的增量距离并使用了公式

speed=distance/time速度=距离/时间

However, the speed value show various numbers, from 0 to aprox.但是,速度值显示各种数字,从 0 到近似值。 6 but it doesn't matter if I use my finger slowly or fast. 6 但我用手指是慢还是快都没有关系。 What should I modify to get the speed of the touch move?我应该修改什么才能获得触摸移动的速度?

I use the following code:我使用以下代码:

 @Override
  public boolean onTouch(View view, MotionEvent motionEvent)
  {

    final int X = (int) motionEvent.getRawX();
    final int Y = (int) motionEvent.getRawY();
    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
    {
      case MotionEvent.ACTION_DOWN:
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        _xDelta = X - lParams.leftMargin;
        _yDelta = Y - lParams.topMargin;
        oldTimeStamp=System.currentTimeMillis();
        break;
      case MotionEvent.ACTION_UP:
           speed=0;
           textView.setText("speed: " + speed);
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        break;
      case MotionEvent.ACTION_POINTER_UP:
        break;
      case MotionEvent.ACTION_MOVE:
        timeStamp= System.currentTimeMillis();
        long diff=timeStamp-oldTimeStamp;
        double dist=Math.sqrt(Math.pow(_xDelta,2)+Math.pow(_yDelta,2));
        double speed=dist/diff;
        textView.setText("speed: " + speed);
       oldTimeStamp=timeStamp;
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.leftMargin = X - _xDelta;
        layoutParams.topMargin = Y - _yDelta;
        layoutParams.rightMargin = -250;
        layoutParams.bottomMargin = -250;
        view.setLayoutParams(layoutParams);
        break;

    }
    layoutRoot.invalidate();
    return true;
  }
@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.
            Log.d("", "X velocity: " + 
                    VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
                    pointerId));
            Log.d("", "Y velocity: " + 
                    VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                    pointerId));
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Return a VelocityTracker object back to be re-used by others.
            mVelocityTracker.recycle();
            break;
    }
    return true;
}

Tracking Movement追踪运动

Call mVelocityTracker.recycle() on view onDetachedFromWindow在视图 onDetachedFromWindow 上调用 mVelocityTracker.recycle()

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

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