Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocity

0
        return x > thumbLeft && x < thumbRight && y > thumbTop && y < thumbBottom;
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) {
        mVelocityTracker.addMovement(ev);
        final int action = ev.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                if (isEnabled() && hitThumb(x, y)) {
                    mTouchMode = TOUCH_MODE_DOWN;
                    mTouchX = x;
                    mTouchY = y;
                } 
                break; 
            } 
 
            case MotionEvent.ACTION_MOVE: {
                switch (mTouchMode) {
                    case TOUCH_MODE_IDLE:
                        // Didn't target the thumb, treat normally. 
                        break; 
 
                    case TOUCH_MODE_DOWN: {
                        final float x = ev.getX();
                        final float y = ev.getY();
                        if (Math.abs(x - mTouchX) > mTouchSlop ||
                                Math.abs(y - mTouchY) > mTouchSlop) {
                            mTouchMode = TOUCH_MODE_DRAGGING;
                            getParent().requestDisallowInterceptTouchEvent(true);
                            mTouchX = x;
                            mTouchY = y;
                            return true; 
                        } 
                        break; 
                    } 
 
                    case TOUCH_MODE_DRAGGING: {
                        final float x = ev.getX();
                        final float dx = x - mTouchX;
                        float newPos = Math.max(0,
                                Math.min(mThumbPosition + dx, getThumbScrollRange()));
                        if (newPos != mThumbPosition) {
                            mThumbPosition = newPos;
                            mTouchX = x;
                            invalidate();
                        } 
                        return true; 
                    } 
                } 
                break; 
            } 
 
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                if (mTouchMode == TOUCH_MODE_DRAGGING) {
                    stopDrag(ev);
                    return true; 
                } 
                mTouchMode = TOUCH_MODE_IDLE;
                mVelocityTracker.clear();
                break; 
            } 
        } 
 
        return super.onTouchEvent(ev);
    } 
 
    private void cancelSuperTouch(MotionEvent ev) {
        MotionEvent cancel = MotionEvent.obtain(ev);
        cancel.setAction(MotionEvent.ACTION_CANCEL);
        super.onTouchEvent(cancel);
        cancel.recycle();
    } 
 
    /** 
     * Called from onTouchEvent to end a drag operation. 
     * 
     * @param ev Event that triggered the end of drag mode - ACTION_UP or ACTION_CANCEL 
     */ 
    private void stopDrag(MotionEvent ev) {
        mTouchMode = TOUCH_MODE_IDLE;
        // Up and not canceled, also checks the switch has not been disabled during the drag 
        boolean commitChange = ev.getAction() == MotionEvent.ACTION_UP && isEnabled();
 
        cancelSuperTouch(ev);
 
        if (commitChange) {
            boolean newState;
            mVelocityTracker.computeCurrentVelocity(1000);
            float xvel = mVelocityTracker.getXVelocity();
            if (Math.abs(xvel) > mMinFlingVelocity) {
                newState = xvel > 0;
            } else { 
                newState = getTargetCheckedState();
            } 
Stop searching for code, let great code find you!  Add Codota to your java IDE