Example usage for android.support.v4.view VelocityTrackerCompat getYVelocity

List of usage examples for android.support.v4.view VelocityTrackerCompat getYVelocity

Introduction

In this page you can find the example usage for android.support.v4.view VelocityTrackerCompat getYVelocity.

Prototype

public static float getYVelocity(VelocityTracker tracker, int pointerId) 

Source Link

Document

Call VelocityTracker#getYVelocity(int) .

Usage

From source file:com.anarchy.classify.library.helper.ClassifyItemTouchHelper.java

private int checkVerticalSwipe(RecyclerView.ViewHolder viewHolder, int flags) {
    if ((flags & (UP | DOWN)) != 0) {
        final int dirFlag = mDy > 0 ? DOWN : UP;
        if (mVelocityTracker != null && mActivePointerId > -1) {
            final float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            final int velDirFlag = yVelocity > 0f ? DOWN : UP;
            if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag
                    && Math.abs(yVelocity) >= mRecyclerView.getMinFlingVelocity()) {
                return velDirFlag;
            }//from w  ww . ja  v a  2s.  c  o  m
        }

        final float threshold = mRecyclerView.getHeight() * mCallback.getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}

From source file:carbon.recycler.ItemTouchHelper.java

private int checkVerticalSwipe(ViewHolder viewHolder, int flags) {
    if ((flags & (UP | DOWN)) != 0) {
        final int dirFlag = mDy > 0 ? DOWN : UP;
        if (mVelocityTracker != null && mActivePointerId > -1) {
            mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND,
                    mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity));
            final float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            final float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            final int velDirFlag = yVelocity > 0f ? DOWN : UP;
            final float absYVelocity = Math.abs(yVelocity);
            if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag
                    && absYVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity)
                    && absYVelocity > Math.abs(xVelocity)) {
                return velDirFlag;
            }/*w w w  .  jav  a2 s .  c om*/
        }

        final float threshold = mRecyclerView.getHeight() * mCallback.getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}

From source file:com.android.launcher3.ItemTouchHelper.java

private int checkHorizontalSwipe(android.support.v7.widget.RecyclerView.ViewHolder viewHolder, int flags) {
    if ((flags & (LEFT | RIGHT)) != 0) {
        final int dirFlag = mDx > 0 ? RIGHT : LEFT;
        if (mVelocityTracker != null && mActivePointerId > -1) {
            mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND,
                    mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity));
            final float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            final float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            final int velDirFlag = xVelocity > 0f ? RIGHT : LEFT;
            final float absXVelocity = Math.abs(xVelocity);
            if ((velDirFlag & flags) != 0 && dirFlag == velDirFlag
                    && absXVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity)
                    && absXVelocity > Math.abs(yVelocity)) {
                return velDirFlag;
            }//from   w  w w.j  av a 2  s .com
        }

        final float threshold = mRecyclerView.getWidth() * mCallback.getSwipeThreshold(viewHolder);

        if ((flags & dirFlag) != 0 && Math.abs(mDx) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}

From source file:com.datarita.ultimatecamera.turu.views.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*  ww w .  j a v  a  2 s .  c o m*/

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the mAdapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:com.yao.zhihudaily.tool.LazyViewPager.java

/**
 * End a fake drag of the pager.// w  ww .j av  a  2s  .co m
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }
    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
    mPopulatePending = true;
    if ((Math.abs(initialVelocity) > mMinimumVelocity)
            || Math.abs(mInitialMotionX - mLastMotionX) >= (getWidth() / 3)) {
        if (mLastMotionX > mInitialMotionX) {
            setCurrentItemInternal(mCurItem - 1, true, true);
        } else {
            setCurrentItemInternal(mCurItem + 1, true, true);
        }
    } else {
        setCurrentItemInternal(mCurItem, true, true);
    }
    endDrag();
    mFakeDragging = false;
}

From source file:com.jihf.swipbackhelper.ViewDragHelper.java

private void releaseViewForPointerUp() {
    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
    final float xvel = clampMag(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    final float yvel = clampMag(VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    if (getViewDragState() == STATE_DRAGGING) {
        dispatchViewReleased(xvel, yvel);
    }/*  ww w. j  a v  a  2s . co m*/
}

From source file:com.wevalue.view.LazyViewPager.java

/**
 * End a fake drag of the pager.//from   w  ww .ja  v a 2 s . c  om
 * 
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
    mPopulatePending = true;
    if ((Math.abs(initialVelocity) > mMinimumVelocity)
            || Math.abs(mInitialMotionX - mLastMotionX) >= (getWidth() / 3)) {
        if (mLastMotionX > mInitialMotionX) {
            setCurrentItemInternal(mCurItem - 1, false, false);
        } else {
            setCurrentItemInternal(mCurItem + 1, false, false);
        }
    } else {
        setCurrentItemInternal(mCurItem, false, false);
    }
    endDrag();

    mFakeDragging = false;
}

From source file:com.appsummary.luoxf.mylibrary.swipbackhelper.ViewDragHelper.java

private void releaseViewForPointerUp() {
    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
    final float xvel = clampMag(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    final float yvel = clampMag(VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    if (getViewDragState() == STATE_DRAGGING)
        dispatchViewReleased(xvel, yvel);
}

From source file:com.android.launcher3.ItemTouchHelper.java

private int checkVerticalSwipe(android.support.v7.widget.RecyclerView.ViewHolder viewHolder, int flags) {
    if ((flags & (UP | DOWN)) != 0) {
        final int dirFlag = mDy > 0 ? DOWN : UP;
        if (mVelocityTracker != null && mActivePointerId > -1) {
            mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND,
                    mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity));
            final float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            final float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            final int velDirFlag = yVelocity > 0f ? DOWN : UP;
            final float absYVelocity = Math.abs(yVelocity);
            if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag
                    && absYVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity)
                    && absYVelocity > Math.abs(xVelocity)) {
                return velDirFlag;
            }/*from  www  .j a  va 2 s. c o m*/
        }

        final float threshold = mRecyclerView.getHeight() * mCallback.getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}

From source file:bw.com.yunifangstore.view.LazyViewPager.java

/**
 * End a fake drag of the pager.//from w  w  w .  j  av  a  2  s  .  c o  m
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
    mPopulatePending = true;
    if ((Math.abs(initialVelocity) > mMinimumVelocity)
            || Math.abs(mInitialMotionX - mLastMotionX) >= (getWidth() / 3)) {
        if (mLastMotionX > mInitialMotionX) {
            setCurrentItemInternal(mCurItem - 1, true, true);
        } else {
            setCurrentItemInternal(mCurItem + 1, true, true);
        }
    } else {
        setCurrentItemInternal(mCurItem, true, true);
    }
    endDrag();

    mFakeDragging = false;
}