Java Code Examples for androidx.core.view.VelocityTrackerCompat#getXVelocity()

The following examples show how to use androidx.core.view.VelocityTrackerCompat#getXVelocity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomViewPager.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
/**
 * End a fake drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    if (mAdapter != null) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int width = getClientWidth();
        final int scrollX = getScrollX();
        final ItemInfo ii = infoForCurrentScrollPosition();
        final int currentPage = ii.position;
        final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
        final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
        int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
                totalDelta);
        setCurrentItemInternal(nextPage, true, true, initialVelocity);
    }
    endDrag();

    mFakeDragging = false;
}
 
Example 2
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
private int checkHorizontalSwipe(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;
            }
        }

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

        if ((flags & dirFlag) != 0 && Math.abs(mDx) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}
 
Example 3
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
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;
            }
        }

        final float threshold = mRecyclerView.getHeight() * mCallback
                .getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}
 
Example 4
Source File: HorTaperChart.java    From WidgetCase with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    boolean eventAddedToVelocityTracker = false;
    final MotionEvent vTev = MotionEvent.obtain(event);
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            mTaperOffsetX = mOffsetX;
        }
        break;
        case MotionEvent.ACTION_MOVE: {
            mOffsetX = mTaperOffsetX + event.getX() - downX;
            LogUtil.logD("move", "mOffsetX = " + mOffsetX);
            if (event.getX() - downX > SCROLL_MIN_DISTANCE) { // 向右滑动,禁止第一张图超出初始值
                if (mOffsetX > 0) {
                    mOffsetX = 0;
                }
            } else if (event.getX() - downX < -SCROLL_MIN_DISTANCE) { // 向左滑动,禁止最后一张图超出初始值
                // 最后一张图形的坐标值
                float endTaperW = getEndTaperW();
                if (Math.abs(mOffsetX) > endTaperW) {
                    mOffsetX = -endTaperW;
                }
                LogUtil.logD("201905071527", "mOffsetX = " + mOffsetX + "->endTaperW = " + endTaperW);
            }
            invalidate();
            break;
        }
        case MotionEvent.ACTION_UP: {
            mVelocityTracker.addMovement(vTev);
            eventAddedToVelocityTracker = true;
            mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
            float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, event.getPointerId(0));
            if (Math.abs(xVelocity) < mMinFlingVelocity) {
                xVelocity = 0F;
            } else {
                xVelocity = Math.max(-mMaxFlingVelocity, Math.min(xVelocity, mMaxFlingVelocity));
            }
            if (xVelocity != 0) {
                // 将速度值反应到滚动器上
                onFling((int) xVelocity);
            } else {
                mScrollState = SCROLL_STATE_IDLE;
            }
            resetTouch();
            break;
        }
        case MotionEvent.ACTION_CANCEL: {
            resetTouch();
        }
        break;
    }
    if (!eventAddedToVelocityTracker) {
        mVelocityTracker.addMovement(vTev);
    }
    vTev.recycle();
    return true;
}