Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocitygetYVelocityrecycle

0
        final int action = ev.getAction();
 
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } 
        mVelocityTracker.addMovement(ev);
 
        boolean handled = false;
 
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mCurrentDownEvent = MotionEvent.obtain(ev);
            break; 
 
        case MotionEvent.ACTION_UP:
            mCurrentUpEvent = MotionEvent.obtain(ev);
            // A fling must travel the minimum tap distance 
            final VelocityTracker velocityTracker = mVelocityTracker;
            // Number of pixels per 100 milliseconds 
            velocityTracker.computeCurrentVelocity(100);
            final float velocityY = velocityTracker.getYVelocity();
            final float velocityX = velocityTracker.getXVelocity();
             
            if ((Math.abs(velocityY) > ViewConfiguration.getMinimumFlingVelocity())
                    || (Math.abs(velocityX) > ViewConfiguration.getMinimumFlingVelocity())){
                handled = onFling(mCurrentDownEvent, mCurrentUpEvent, velocityX, velocityY);
            } 
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            break; 
        case MotionEvent.ACTION_CANCEL:
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            break; 
        } 
        return handled;
    } 
Connect your IDE to all the code out there  Get Codota for Java