Java Code Examples for android.support.v4.view.VelocityTrackerCompat#getXVelocity()

The following examples show how to use android.support.v4.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: VelocityViewPager.java    From Rey-MusicPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * End a fake drawable_drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drawable_drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
            velocityTracker, mActivePointerId);
    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: VerticalViewPager.java    From DoubleViewPager with Apache License 2.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.");
    }

    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 3
Source File: ViewPagerCompact.java    From RxZhihuDaily with MIT License 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.");
    }

    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 4
Source File: FuckViewPager.java    From nono-android 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.");
    }

    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 5
Source File: ViewPager.java    From RxJavaApp with Apache License 2.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.");
    }

    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 6
Source File: XBannerViewPager.java    From XBanner with Apache License 2.0 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception ignored) {
    }
    return xVelocity;
}
 
Example 7
Source File: ViewPagerEx.java    From ImageSliderWithSwipes with Apache License 2.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.");
    }

    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 8
Source File: BGAViewPager.java    From KUtils-master with Apache License 2.0 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception e) {
    }
    return xVelocity;
}
 
Example 9
Source File: VerticalViewPager.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public void endFakeDrag()
{
    if (!O)
    {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    } else
    {
        VelocityTracker velocitytracker = K;
        velocitytracker.computeCurrentVelocity(1000, M);
        int i1 = (int)VelocityTrackerCompat.getXVelocity(velocitytracker, I);
        z = true;
        int j1 = (int)(G - H);
        int k1 = getScrollY();
        int l1 = getHeight() + r;
        a(a(k1 / l1, (float)(k1 % l1) / (float)l1, i1, j1), true, true, i1);
        i();
        O = false;
        return;
    }
}
 
Example 10
Source File: ViewPagerEx.java    From LoyalNativeSlider with MIT License 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.");
    }

    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 11
Source File: ItemTouchHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
private int checkVerticalSwipe(ViewHolder viewHolder, int flags) {
    if ((flags & 3) != 0) {
        int dirFlag = this.mDy > 0.0f ? 2 : 1;
        if (this.mVelocityTracker != null && this.mActivePointerId > -1) {
            int velDirFlag;
            this.mVelocityTracker.computeCurrentVelocity(1000, this.mCallback.getSwipeVelocityThreshold(this.mMaxSwipeVelocity));
            float xVelocity = VelocityTrackerCompat.getXVelocity(this.mVelocityTracker, this.mActivePointerId);
            float yVelocity = VelocityTrackerCompat.getYVelocity(this.mVelocityTracker, this.mActivePointerId);
            if (yVelocity > 0.0f) {
                velDirFlag = 2;
            } else {
                velDirFlag = 1;
            }
            float absYVelocity = Math.abs(yVelocity);
            if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag && absYVelocity >= this.mCallback.getSwipeEscapeVelocity(this.mSwipeEscapeVelocity) && absYVelocity > Math.abs(xVelocity)) {
                return velDirFlag;
            }
        }
        float threshold = ((float) this.mRecyclerView.getHeight()) * this.mCallback.getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(this.mDy) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}
 
Example 12
Source File: ItemTouchHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
private int checkHorizontalSwipe(ViewHolder viewHolder, int flags) {
    if ((flags & 12) != 0) {
        int dirFlag = this.mDx > 0.0f ? 8 : 4;
        if (this.mVelocityTracker != null && this.mActivePointerId > -1) {
            int velDirFlag;
            this.mVelocityTracker.computeCurrentVelocity(1000, this.mCallback.getSwipeVelocityThreshold(this.mMaxSwipeVelocity));
            float xVelocity = VelocityTrackerCompat.getXVelocity(this.mVelocityTracker, this.mActivePointerId);
            float yVelocity = VelocityTrackerCompat.getYVelocity(this.mVelocityTracker, this.mActivePointerId);
            if (xVelocity > 0.0f) {
                velDirFlag = 8;
            } else {
                velDirFlag = 4;
            }
            float absXVelocity = Math.abs(xVelocity);
            if ((velDirFlag & flags) != 0 && dirFlag == velDirFlag && absXVelocity >= this.mCallback.getSwipeEscapeVelocity(this.mSwipeEscapeVelocity) && absXVelocity > Math.abs(yVelocity)) {
                return velDirFlag;
            }
        }
        float threshold = ((float) this.mRecyclerView.getWidth()) * this.mCallback.getSwipeThreshold(viewHolder);
        if ((flags & dirFlag) != 0 && Math.abs(this.mDx) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}
 
Example 13
Source File: BGAViewPager.java    From KUtils with Apache License 2.0 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception e) {
    }
    return xVelocity;
}
 
Example 14
Source File: BGAViewPager.java    From JD-Test with Apache License 2.0 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception e) {
    }
    return xVelocity;
}
 
Example 15
Source File: ViewPagerCompat.java    From android-auto-scroll-viewpager with Apache License 2.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.");
    }

    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 16
Source File: ViewPager.java    From ankihelper with GNU General Public License v3.0 5 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.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;

    int initialVelocity, totalDelta;
    float pageOffset;
    if (isOrientationHorizontal()) {
        initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int width = getClientWidth();
        final int scrollX = getScrollX();
        pageOffset = (((float) scrollX / width) - ii.offset) / ii.sizeFactor;
        totalDelta = (int) (mLastMotionX - mInitialMotionX);
    } else {
        initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int height = getClientHeight();
        final int scrollY = getScrollY();
        pageOffset = (((float) scrollY / height) - ii.offset) / ii.sizeFactor;
        totalDelta = (int) (mLastMotionY - mInitialMotionY);
    }

    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 17
Source File: ViewPager.java    From GifAssistant with Apache License 2.0 5 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.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;

    int initialVelocity, totalDelta;
    float pageOffset;
    if (isOrientationHorizontal()) {
        initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int width = getClientWidth();
        final int scrollX = getScrollX();
        pageOffset = (((float) scrollX / width) - ii.offset) / ii.sizeFactor;
        totalDelta = (int) (mLastMotionX - mInitialMotionX);
    } else {
        initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int height = getClientHeight();
        final int scrollY = getScrollY();
        pageOffset = (((float) scrollY / height) - ii.offset) / ii.sizeFactor;
        totalDelta = (int) (mLastMotionY - mInitialMotionY);
    }

    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 18
Source File: ReItemTouchHelper.java    From ReSwipeCard with Apache License 2.0 5 votes vote down vote up
private int checkHorizontalSwipe(ViewHolder viewHolder, int flags) {
    boolean isLeft = mDx < 0;
    if (mCallback.isForbidLeftSwipeOut() && isLeft) {
        return 0;
    }
    if (mCallback.isForbidRightSwipeOut() && !isLeft) {
        return 0;
    }
    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 = mRecyclerView.getWidth() * mCallback
                .getSwipeThreshold(viewHolder);

        if ((flags & dirFlag) != 0 && Math.abs(mDx) > threshold) {
            return dirFlag;
        }
    }
    return 0;
}
 
Example 19
Source File: ReItemTouchHelper.java    From ReSwipeCard with Apache License 2.0 5 votes vote down vote up
private int checkVerticalSwipe(ViewHolder viewHolder, int flags) {
    boolean isUp = mDy < 0;
    if (mCallback.isForbidUpSwipeOut() && isUp) {
        return 0;
    }
    if (mCallback.isForbidDownSwipeOut() && (!isUp)) {
        return 0;
    }
    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 20
Source File: RulerView.java    From spline with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    float dx = x - mPrevX;
    float dy = y - mPrevY;
    int index = event.getActionIndex();
    int pointerId = event.getPointerId(index);

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }

            mMode = MODE_RULER_DRAG;
            if (mIsVertical) {
                if (y - mViewportStart >= mHighlightStart &&
                        y - mViewportStart <= mHighlightStart + mHighlightSize) {
                    mMode = MODE_HIGHLIGHT_DRAG;
                }
            } else {
                if (x - mViewportStart >= mHighlightStart &&
                        x - mViewportStart <= mHighlightStart + mHighlightSize) {
                    mMode = MODE_HIGHLIGHT_DRAG;
                }
            }

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

            break;
        case MotionEvent.ACTION_MOVE:
            mVelocityTracker.addMovement(event);

            if (mMode == MODE_RULER_DRAG) {
                if (mIsVertical) {
                    setViewportStart(getViewportStart() + dy);
                } else {
                    setViewportStart(getViewportStart() + dx);
                }
            } else if (mMode == MODE_HIGHLIGHT_DRAG) {
                if (mIsVertical) {
                    // Drag highlight by whole pixels for now
                    setHighlightStart(getHighlightStart() + Math.round(dy));
                } else {
                    setHighlightStart(getHighlightStart() + Math.round(dx));
                }
            }

            break;

        case MotionEvent.ACTION_UP:
            if (mMode == MODE_RULER_DRAG) {
                mVelocityTracker.computeCurrentVelocity(1000);
                int vx = (int) (VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId));
                int vy = (int) (VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId));
                mScroller.fling((int) getViewportStart(), (int) getViewportStart(), vx, vy,
                        -10000, 10000, -10000, 10000);
                this.invalidate();
            }

            break;
    }

    mPrevX = x;
    mPrevY = y;

    return true;
}