Example usage for android.view VelocityTracker getXVelocity

List of usage examples for android.view VelocityTracker getXVelocity

Introduction

In this page you can find the example usage for android.view VelocityTracker getXVelocity.

Prototype

public float getXVelocity() 

Source Link

Document

Retrieve the last computed X velocity.

Usage

From source file:com.albedinsky.android.support.ui.HorizontalPullHelper.java

/**
 *///from   w ww.ja v  a 2  s .co  m
@Override
boolean isAllowedVelocity(VelocityTracker tracker, float minVelocity) {
    return Math.abs(tracker.getXVelocity()) >= minVelocity;
}

From source file:com.albedinsky.android.support.ui.HorizontalPullHelper.java

/**
 */// www. j  av a  2s. c o  m
@Override
float computeOverScroll(int scrollX, int scrollY, boolean clampedX, boolean clampedY, VelocityTracker tracker,
        int units) {
    if (scrollX != 0 && clampedX) {
        return tracker.getXVelocity() / (units * 0.05f);
    }
    return 0;
}

From source file:org.kymjs.kjframe.widget.KJViewPager.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isScroll) {
        return false;
    }// www  .  j  a  v  a 2  s .com
    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:
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        mLastMotionX = x;
        mLastMotionY = y;
        break;
    case MotionEvent.ACTION_MOVE:
        int deltaX = (int) (mLastMotionX - x);
        int deltaY = (int) (mLastMotionY - y);
        if (Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10)
            break;
        mLastMotionY = y;
        mLastMotionX = x;
        scrollBy(deltaX, 0);
        break;
    case MotionEvent.ACTION_UP:
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000);
        int velocityX = (int) velocityTracker.getXVelocity();
        if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
            // Fling enough to move left
            snapToScreen(mCurScreen - 1);
        } else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
            // Fling enough to move right
            snapToScreen(mCurScreen + 1);
        } else {
            snapToDestination();
        }
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        mTouchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        break;
    }
    return true;
}

From source file:cn.ieclipse.af.view.ScrollLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }/*www  .j a  v a2  s  .co m*/
    mVelocityTracker.addMovement(event);

    final int action = event.getAction();
    final float x = event.getX();
    final float y = event.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (debug) {
            Log.v(TAG, "event down!");
        }
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        mLastMotionX = x;
        break;

    case MotionEvent.ACTION_MOVE:
        int deltaX = (int) (mLastMotionX - x);
        mLastMotionX = x;

        scrollBy(deltaX, 0);
        break;

    case MotionEvent.ACTION_UP:
        if (debug) {
            Log.v(TAG, "event : up");
        }
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000);
        int velocityX = (int) velocityTracker.getXVelocity();
        if (debug) {
            Log.v(TAG, "velocityX:" + velocityX);
        }

        if (velocityX > mSnapVelocity) {
            if (mCurScreen > 0) {
                // Fling enough to move left
                if (debug) {
                    Log.v(TAG, "snap left");
                }
                snapToScreen(mCurScreen - 1);
            } else {
                // TODO
                disableWipe(false);
                snapToDestination();
            }
        } else if (velocityX < -mSnapVelocity) {
            if (mCurScreen < getChildCount() - 1) {
                // Fling enough to move right
                if (debug) {
                    Log.v(TAG, "snap right");
                }
                snapToScreen(mCurScreen + 1);
            } else {
                // TODO
                disableWipe(false);
                snapToDestination();
            }
        } else {
            snapToDestination();
        }

        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        // }
        mTouchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        break;
    }

    return true;
}

From source file:org.zywx.wbpalmstar.engine.EBounceView.java

private boolean handlerTracker() {
    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000);
    int velocityX = (int) (velocityTracker.getXVelocity() / mDensity);
    int velocityY = (int) (velocityTracker.getYVelocity() / mDensity);
    endDrag();//from w  ww.  j  a va 2 s  . c  om
    return onTracker(velocityX, velocityY);
}

From source file:com.yj.ecard.ui.views.viewflow.ViewFlow.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }/* w  w  w . j a v  a 2s.c o m*/
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

        if (handler != null) {
            handler.removeMessages(0);
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;
        // action_up?????
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        mTouchState = TOUCH_STATE_REST;
    }
    return false;
}

From source file:com.yj.ecard.ui.views.viewflow.ViewFlow.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//  www. j  a  v  a2  s  .  c  om
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        // ??
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            }
            // else if (velocityX < -SNAP_VELOCITY
            // && mCurrentScreen == getChildCount() - 1) {
            // snapToScreen(0);
            // }
            // else if (velocityX > SNAP_VELOCITY
            // && mCurrentScreen == 0) {
            // snapToScreen(getChildCount() - 1);
            // }
            else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        // action_up?????
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        snapToDestination();
        mTouchState = TOUCH_STATE_REST;
    }
    return true;
}

From source file:com.jp.fristandroidapp.widget.adview.AdViewFlow.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//from   w  ww.  j  a  v  a 2 s  .  c  o  m
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setIsInterceptParent(true);
        setIsInterceptParent2(true);
        setIsInterceptParent3(true);
        setIsInterceptParent4(true);
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;
        setIsInterceptParent(false);
        setIsInterceptParent2(false);
        setIsInterceptParent3(false);
        setIsInterceptParent4(false);
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        setIsInterceptParent(false);
        setIsInterceptParent2(false);
        setIsInterceptParent3(false);
        setIsInterceptParent4(false);
    }
    return false;
}

From source file:com.jp.fristandroidapp.widget.adview.AdViewFlow.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//  ww  w . ja  va 2 s .  co  m
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            }
            //            else if (velocityX < -SNAP_VELOCITY
            //                     && mCurrentScreen == getChildCount() - 1) {
            //                  snapToScreen(0);
            //            }
            //            else if (velocityX > SNAP_VELOCITY
            //                     && mCurrentScreen == 0) {
            //                  snapToScreen(getChildCount() - 1);
            //            }
            else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        snapToDestination();
        mTouchState = TOUCH_STATE_REST;
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
    }
    return true;
}

From source file:com.lab47billion.appchooser.HorizontalPicker.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }//from  w w w.  ja v  a  2s . co  m

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);

    int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_MOVE:
        isScrollingStart = true;
        float currentMoveX = event.getX();

        int deltaMoveX = (int) (mLastDownEventX - currentMoveX);

        if (mScrollingX || (Math.abs(deltaMoveX) > mTouchSlop) && mValues != null && mValues.length > 0) {

            if (!mScrollingX) {
                deltaMoveX = 0;
                mPressedItem = -1;
                mScrollingX = true;
            }

            final int range = getScrollRange();

            if (overScrollBy(deltaMoveX, 0, getScrollX(), 0, range, 0, mOverscrollDistance, 0, true)) {
                mVelocityTracker.clear();
            }

            final float pulledToX = getScrollX() + deltaMoveX;
            if (pulledToX < 0) {
                mLeftEdgeEffect.onPull((float) deltaMoveX / getWidth());
                if (!mRightEdgeEffect.isFinished()) {
                    mRightEdgeEffect.onRelease();
                }
            } else if (pulledToX > range) {
                mRightEdgeEffect.onPull((float) deltaMoveX / getWidth());
                if (!mLeftEdgeEffect.isFinished()) {
                    mLeftEdgeEffect.onRelease();
                }
            }

            mLastDownEventX = currentMoveX;
            invalidate();

        }

        break;
    case MotionEvent.ACTION_DOWN:

        if (!mAdjustScrollerX.isFinished()) {
            mAdjustScrollerX.forceFinished(true);
        } else if (!mFlingScrollerX.isFinished()) {
            mFlingScrollerX.forceFinished(true);
        } else {
            mScrollingX = false;
        }

        mLastDownEventX = event.getX();

        if (!mScrollingX) {
            mPressedItem = getPositionFromTouch(event.getX());
        }
        invalidate();

        break;
    case MotionEvent.ACTION_UP:
        isScrollingStart = false;
        VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int initialVelocityX = (int) velocityTracker.getXVelocity();

        if (mScrollingX && Math.abs(initialVelocityX) > mMinimumFlingVelocity) {
            flingX(initialVelocityX);
        } else if (mValues != null) {
            float positionX = event.getX();
            if (!mScrollingX) {

                int itemPos = getPositionOnScreen(positionX);
                int relativePos = itemPos - mSideItems;

                if (relativePos == 0) {
                    selectItem();
                } else {
                    smoothScrollBy(relativePos);
                }

            } else if (mScrollingX) {
                finishScrolling();
            }
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;

        if (mLeftEdgeEffect != null) {
            mLeftEdgeEffect.onRelease();
            mRightEdgeEffect.onRelease();
        }

    case MotionEvent.ACTION_CANCEL:
        mPressedItem = -1;
        invalidate();

        if (mLeftEdgeEffect != null) {
            mLeftEdgeEffect.onRelease();
            mRightEdgeEffect.onRelease();
        }

        break;
    }

    return true;
}