LoginSignup
1
0

More than 5 years have passed since last update.

onTouchEventでmoveしたときの速度を取る方法

Posted at

上記を翻訳、まとめてみた。


private VelocityTracker mVelocityTracker = null;
    @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) {
                    //  動きの速度を監視する新しいVelocityTrackerオブジェクトを取得します。
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // 速度トラッカーを初期状態にリセットします。
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
              //速度を決定する場合、computeCurrentVelocity()を呼び出します。
              //その後、getXVelocity()を呼び出します
              //getYVelocity()は各ポインタIDの速度を取得します。
                mVelocityTracker.computeCurrentVelocity(1000);

                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 他のユーザーによって再利用されるようにバックVelocityTrackerオブジェクトを返します。
                break;
        }
        return true;
    }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0