Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocityrecycle

0
        } 
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } 
 
        mVelocityTracker.addMovement(event);
 
        final int action = event.getAction();
        final float x = event.getX();
        final float y = event.getY();
 
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionX = x;
            mLastMotionY = y;
            isDragging = true;
            break; 
        case MotionEvent.ACTION_MOVE:
            if (!isDragging) {
                final int xDiff = (int) Math.abs(x - mLastMotionX);
                final int yDiff = (int) Math.abs(y - mLastMotionY);
 
                if (xDiff > mTouchSlop) {
                    isDragging = (xDiff >= yDiff)
                            && ((isLeftSwipeValid && x < mLastMotionX) || (isRightSwipeValid && x > mLastMotionX));
                } else if (yDiff > mTouchSlop) {
                    isDragging = false;
                } 
            } 
 
            if (isDragging) {
                final int deltaX = (int) (mLastMotionX - x);
                if (listener != null) {
                    listener.swipeTo(deltaX);
                } 
 
                mLastMotionX = x;
                mLastMotionY = y;
            } 
            break; 
        case MotionEvent.ACTION_UP:
            if (isDragging) {
                mVelocityTracker.computeCurrentVelocity(VELOCITY_UNIT_PIXELS_PER_SECOND, mMaximumVelocity);
                final int velocityX = (int) mVelocityTracker.getXVelocity();
                if (listener != null) {
                    if (velocityX > mDensityAdjustedSnapVelocity) {
                        listener.swipeRight();
                    } else if (velocityX < -mDensityAdjustedSnapVelocity) {
                        listener.swipeLeft();
                    } else { 
                        listener.cancelSwipe();
                    } 
                } 
 
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                } 
            } 
            isDragging = false;
            break; 
Contextual code suggestions in your IDE  Get Codota for Java