如何在Android中检测触控速度

 乔志造型店长阿杰 发布于 2023-02-06 17:21

我添加了一个drawable到我的应用程序,我可以触摸移动,并根据手指移动速度,我想做不同的动作.

我检查了事件,我只能使用MotionEvent.ACTION_MOVE来检测是否有运动.所以我保存了最后一个动作的时间戳并获得了实际值,我计算了动作的增量距离,并使用了公式

速度=距离/时间

但是,速度值显示各种数字,从0到aprox.6但是如果我慢慢地或快速地使用我的手指并不重要.我应该修改什么才能获得触摸移动的速度?

我使用以下代码:

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

Triode.. 7

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

跟踪运动

1 个回答
  • @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;
    }
    

    跟踪运动

    2023-02-06 17:23 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有