Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocitygetYVelocityrecycle

0
 
                case MotionEvent.ACTION_CANCEL: {
                    // Recycle Velocity Tracker 
                    if (null != mVelocityTracker) {
                        mVelocityTracker.recycle();
                        mVelocityTracker = null;
                    } 
                    break; 
                } 
 
                case MotionEvent.ACTION_UP: {
                    if (mIsDragging) {
                        if (null != mVelocityTracker) {
                            mLastTouchX = getActiveX(ev);
                            mLastTouchY = getActiveY(ev);
 
                            // Compute velocity within the last 1000ms 
                            mVelocityTracker.addMovement(ev);
                            mVelocityTracker.computeCurrentVelocity(1000);
 
                            final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                                    .getYVelocity();
 
                            // If the velocity is greater than minVelocity, call 
                            // listener 
                            if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {