VelocityTracker会产生不必要的负面结果

时间:2015-06-04 02:15:09

标签: java android mouse motionevent

更新:此人似乎知道我在说什么 https://code.google.com/p/android/issues/detail?id=174970

当前项目:我正在使用远程鼠标,而我正在使用VelocityTracker来获取所需的X和Y移动。 (我尝试直接使用MotionEvent,但它的效果不如VelocityTracker

问题: 在从左到右的随意滑动中(下图中的A到B),您可以假设我的X速度应该始终为正。 HOWEVER ,情况并非如此,因为我减速拇指(接近点B)我开始在X Velocity上接收负值,尽管它们可能不大(几乎接近零)它们会影响鼠标移动,使其不易控制。 如何让此速度跟踪器提供更平滑或更准确的结果?

enter image description here

记录上述动议:

06-03 22:06:33.551    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_DOWN
06-03 22:06:33.671    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 20.689539 Y: 0.112973444
06-03 22:06:33.811    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 25.343899 Y: 0.9028549
06-03 22:06:33.922    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 16.566969 Y: 0.5636469
06-03 22:06:34.042    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: -1.6488186 Y: -0.39940003 // These shouldnt be here!
06-03 22:06:34.182    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: -2.4089027E-4 Y: -2.4089027E-4 // These shouldnt be here!
06-03 22:06:34.322    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 5.488204E-4 Y: 7.3197135E-4
06-03 22:06:34.452    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 0.0 Y: 0.0
06-03 22:06:34.572    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_MOVE X: 0.0 Y: -3.7341337
06-03 22:06:34.582    9413-9413/com.tutorials.jurko.androidmouse I/System.out﹕ ACTION_UP

onTouch()处理代码:

    if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP) {
        mouseTouchPaint.setColor(Color.WHITE);
        speedX = 0;
        speedY = 0;

        System.out.println("ACTION_UP");
        Byte bytes[] = {speedX, speedY};
        sendMessage(bytes);

        mVelocityTracker.recycle();
        mVelocityTracker = null;

        view.invalidate();
        return true;
    }
    else if (motionEvent.getActionMasked() == MotionEvent.ACTION_MOVE || motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {

        if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
            System.out.println("ACTION_DOWN");
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            } else {
                mVelocityTracker.clear();
            }
            mouseTouchPaint.setColor(Color.parseColor("#CD5C5C"));
            view.invalidate();
        }
        mVelocityTracker.addMovement(motionEvent);

        if (motionEvent.getActionMasked() == MotionEvent.ACTION_MOVE) {

            mVelocityTracker.computeCurrentVelocity(10);
            float XVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, motionEvent.getPointerId(motionEvent.getActionIndex()));
            float YVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, motionEvent.getPointerId(motionEvent.getActionIndex()));
            System.out.println("ACTION_MOVE X: " + XVelocity + " Y: " + YVelocity);

            sendMessage(XVelocity, YVelocity);
            mVelocityTracker.clear();
            return true;
        }
        return true;
    }

0 个答案:

没有答案