Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocitygetYVelocityrecycle

0
        final float y = event.getY();
 
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } 
        mVelocityTracker.addMovement(event);
 
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mZoomControl.stopFling();
                v.postDelayed(mLongPressRunnable, mLongPressTimeout);
                mDownX = x;
                mDownY = y;
                mX = x;
                mY = y;
                break; 
 
            case MotionEvent.ACTION_MOVE: {
                final float dx = (x - mX) / v.getWidth();
                final float dy = (y - mY) / v.getHeight();
 
                if (mMode == Mode.ZOOM) {
                    mZoomControl.zoom((float)Math.pow(20, -dy), mDownX / v.getWidth(), mDownY
                            / v.getHeight());
                } else if (mMode == Mode.PAN) {
                    mZoomControl.pan(-dx, -dy);
                } else { 
                    final float scrollX = mDownX - x;
                    final float scrollY = mDownY - y;
 
                    final float dist = (float)Math.sqrt(scrollX * scrollX + scrollY * scrollY);
 
                    if (dist >= mScaledTouchSlop) {
                        v.removeCallbacks(mLongPressRunnable);
                        mMode = Mode.PAN;
                    } 
                } 
 
                mX = x;
                mY = y;
                break; 
            } 
 
            case MotionEvent.ACTION_UP:
                if (mMode == Mode.PAN) {
                    mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
                    mZoomControl.startFling(-mVelocityTracker.getXVelocity() / v.getWidth(),
                            -mVelocityTracker.getYVelocity() / v.getHeight());
                } else { 
                    mZoomControl.startFling(0, 0);
                } 
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                v.removeCallbacks(mLongPressRunnable);
                mMode = Mode.UNDEFINED;
                break; 
 
            default: 
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                v.removeCallbacks(mLongPressRunnable);
                mMode = Mode.UNDEFINED;
                break;