android.view.VelocityTracker Java Examples

The following examples show how to use android.view.VelocityTracker. 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: VerticalViewPager.java    From ankihelper with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <p>
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * <p>
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #2
Source File: ViewPager.java    From android-movies-demo 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 #3
Source File: ViewPager.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #4
Source File: ViewPager.java    From letv with Apache License 2.0 6 votes vote down vote up
public boolean beginFakeDrag() {
    if (this.mIsBeingDragged) {
        return false;
    }
    this.mFakeDragging = true;
    setScrollState(1);
    this.mLastMotionX = 0.0f;
    this.mInitialMotionX = 0.0f;
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    } else {
        this.mVelocityTracker.clear();
    }
    long time = SystemClock.uptimeMillis();
    MotionEvent ev = MotionEvent.obtain(time, time, 0, 0.0f, 0.0f, 0);
    this.mVelocityTracker.addMovement(ev);
    ev.recycle();
    this.mFakeDragBeginTime = time;
    return true;
}
 
Example #5
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 #6
Source File: VerticalViewPager.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <p>
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * <p>
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #7
Source File: LinkagePager.java    From Prodigal with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #8
Source File: SdkCenteredViewPager.java    From Android-SDK-Demo with MIT License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <p/>
 * <p>
 * A fake drag can be useful if you want to synchronize the motion of the ViewPager with the touch scrolling of another view, while still letting the ViewPager control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) Call {@link #fakeDragBy(float)} to simulate the actual
 * drag motion. Call {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * <p/>
 * <p>
 * During a fake drag the ViewPager will ignore all touch events. If a real drag is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag()
{
    if ( mIsBeingDragged )
    {
        return false;
    }
    mFakeDragging = true;
    setScrollState( SCROLL_STATE_DRAGGING );
    mInitialMotionX = mLastMotionX = 0;
    if ( mVelocityTracker == null )
    {
        mVelocityTracker = VelocityTracker.obtain();
    }
    else
    {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain( time, time, MotionEvent.ACTION_DOWN, 0, 0, 0 );
    mVelocityTracker.addMovement( ev );
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #9
Source File: ViewPagerEx.java    From ImageSliderWithSwipes with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #10
Source File: LazyViewPager.java    From AndroidBase 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.getYVelocity(
            velocityTracker, mActivePointerId);
    mPopulatePending = true;
    final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
    final int scrollX = getScrollX();
    final int widthWithMargin = getWidth() + mPageMargin;
    final int currentPage = scrollX / widthWithMargin;
    final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example #11
Source File: ViewPagerEx.java    From UltimateAndroid 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 #12
Source File: ViewPagerEx.java    From LoyalNativeSlider with MIT License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * /
 * A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * /
 * During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #13
Source File: VerticalViewPager.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public boolean beginFakeDrag()
{
    if (C)
    {
        return false;
    }
    O = true;
    a(1);
    G = 0.0F;
    H = 0.0F;
    long l1;
    MotionEvent motionevent;
    if (K == null)
    {
        K = VelocityTracker.obtain();
    } else
    {
        K.clear();
    }
    l1 = SystemClock.uptimeMillis();
    motionevent = MotionEvent.obtain(l1, l1, 0, 0.0F, 0.0F, 0);
    K.addMovement(motionevent);
    motionevent.recycle();
    P = l1;
    return true;
}
 
Example #14
Source File: ViewPager.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #15
Source File: ViewPager.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #16
Source File: NineOldViewPager.java    From ncalc 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 #17
Source File: ViewPagerEx.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example #18
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 #19
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 #20
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 #21
Source File: ViewPager.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public boolean beginFakeDrag()
{
    if (I)
    {
        return false;
    }
    ac = true;
    c(1);
    O = 0.0F;
    Q = 0.0F;
    long l1;
    MotionEvent motionevent;
    if (U == null)
    {
        U = VelocityTracker.obtain();
    } else
    {
        U.clear();
    }
    l1 = SystemClock.uptimeMillis();
    motionevent = MotionEvent.obtain(l1, l1, 0, 0.0F, 0.0F, 0);
    U.addMovement(motionevent);
    motionevent.recycle();
    ad = l1;
    return true;
}
 
Example #22
Source File: ViewPagerEx.java    From UltimateAndroid 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 #23
Source File: BaseMainAdapter.java    From ClassifyView with Apache License 2.0 5 votes vote down vote up
@Override
public int getCurrentState(View selectedView, View targetView, int x, int y,
                           VelocityTracker velocityTracker, int selectedPosition,
                           int targetPosition) {
    if (velocityTracker == null) return ClassifyView.MOVE_STATE_NONE;
    int left = x;
    int top = y;
    int right = left + selectedView.getWidth();
    int bottom = top + selectedView.getHeight();
    if (canMergeItem(selectedPosition, targetPosition)) {
        if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
                Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
                < (targetView.getWidth() + targetView.getHeight()
        ) / 3) {
            return ClassifyView.MOVE_STATE_MERGE;
        }
    }
    if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
            Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
            < (targetView.getWidth() + targetView.getHeight()
    ) / 2) {
        velocityTracker.computeCurrentVelocity(100);
        float xVelocity = velocityTracker.getXVelocity();
        float yVelocity = velocityTracker.getYVelocity();
        float limit = getVelocity(targetView.getContext());
        if (xVelocity < limit && yVelocity < limit) {
            return ClassifyView.MOVE_STATE_MOVE;
        }
    }
    return ClassifyView.MOVE_STATE_NONE;
}
 
Example #24
Source File: TowPageViewPager.java    From Android_UE with Apache License 2.0 5 votes vote down vote up
private void init() {
    velocityTracker = VelocityTracker.obtain();
    viewConfiguration = ViewConfiguration.get(getContext());
    maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
    minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    overScroller = new OverScroller(getContext());
}
 
Example #25
Source File: NestedScrollView.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
private void initOrResetVelocityTracker() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
}
 
Example #26
Source File: RefreshLayout.java    From SimpleProject with MIT License 5 votes vote down vote up
private void initView(Context context) {
	setOrientation(VERTICAL);
	mRefreshStatus = REFRESH_STATUS_NONE;
	mScroller = new Scroller(context);
	mScreenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
	mVelocityTracker = VelocityTracker.obtain();
	ViewConfiguration configuration = ViewConfiguration.get(context);
	mMinVelocity = configuration.getScaledMinimumFlingVelocity();
}
 
Example #27
Source File: FanLayout.java    From FanLayout with Apache License 2.0 5 votes vote down vote up
public FanLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    initAttrs(context, attrs, defStyleAttr);

    mScrollAvailabilityRatio = .3F;
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    mTouchSlop = configuration.getScaledTouchSlop();
    mScroller = new Scroller(getContext());
    mVelocityTracker = VelocityTracker.obtain();
}
 
Example #28
Source File: CalendarLayout.java    From CalendarView with Apache License 2.0 5 votes vote down vote up
public CalendarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(LinearLayout.VERTICAL);
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CalendarLayout);
    mContentViewId = array.getResourceId(R.styleable.CalendarLayout_calendar_content_view_id, 0);
    mDefaultStatus = array.getInt(R.styleable.CalendarLayout_default_status, STATUS_EXPAND);
    mCalendarShowMode = array.getInt(R.styleable.CalendarLayout_calendar_show_mode, CALENDAR_SHOW_MODE_BOTH_MONTH_WEEK_VIEW);
    mGestureMode = array.getInt(R.styleable.CalendarLayout_gesture_mode, GESTURE_MODE_DEFAULT);
    array.recycle();
    mVelocityTracker = VelocityTracker.obtain();
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    int mTouchSlop = configuration.getScaledTouchSlop();
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
 
Example #29
Source File: DraggableDrawer.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
protected float getYVelocity(VelocityTracker velocityTracker) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return velocityTracker.getYVelocity(mActivePointerId);
    }

    return velocityTracker.getYVelocity();
}
 
Example #30
Source File: SwipeActivity.java    From imsdk-android with MIT License 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (swipeAnyWhere) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                currentX = downX;
                currentY = downY;
                lastX = downX;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = ev.getX() - downX;
                float dy = ev.getY() - downY;
                if ((dy == 0f || Math.abs(dx / dy) > 1) && (dx * dx + dy * dy > touchSlop * touchSlop)) {
                    downX = ev.getX();
                    downY = ev.getY();
                    currentX = downX;
                    currentY = downY;
                    lastX = downX;
                    canSwipe = true;
                    tracker = VelocityTracker.obtain();
                    return true;
                }
                break;
        }
    } else if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getX() < sideWidth) {
        canSwipe = true;
        tracker = VelocityTracker.obtain();
        return true;
    }
    return super.onInterceptTouchEvent(ev);
}