Java Code Examples for android.view.VelocityTracker#getXVelocity()

The following examples show how to use android.view.VelocityTracker#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: SdkCenteredViewPager.java    From Android-SDK-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) velocityTracker.getXVelocity( 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: CoolViewPager.java    From CoolViewPager 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.");
    }

    if (mAdapter != null) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);
        mPopulatePending = true;
        final int width = getClientWidth();
        final int scrollX = getScrollX();
        final CoolViewPager.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: SliderPager.java    From Android-Image-Slider 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.");
    }

    if (mAdapter != null) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) velocityTracker.getXVelocity(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: SwipeTouchHelper.java    From StackCardsView with Apache License 2.0 6 votes vote down vote up
private void onTouchRelease() {
    final StackCardsView.LayoutParams lp = (StackCardsView.LayoutParams) mTouchChild.getLayoutParams();
    if (lp.fastDismissAllowed) {
        final VelocityTracker velocityTracker2 = mVelocityTracker;
        velocityTracker2.computeCurrentVelocity(1000, mMaxVelocity);
        float xv = velocityTracker2.getXVelocity(mActivePointerId);
        float yv = velocityTracker2.getYVelocity(mActivePointerId);
        if (doFastDisappear(xv, yv)) {
            resetTouch();
            return;
        }
    }
    if (isDistanceAllowDismiss() && isDirectionAllowDismiss()) {
        doSlowDisappear();
    } else {
        animateToInitPos();
    }
    resetTouch();
    mSwipeView.onCoverStatusChanged(isCoverIdle());
}
 
Example 5
Source File: AbsHListView.java    From Klyph with MIT License 6 votes vote down vote up
@Override
public void run() {
	final int activeId = mActivePointerId;
	final VelocityTracker vt = mVelocityTracker;
	final OverScroller scroller = mScroller;
	if ( vt == null || activeId == INVALID_POINTER ) {
		return;
	}

	vt.computeCurrentVelocity( 1000, mMaximumVelocity );
	final float xvel = -vt.getXVelocity( activeId );

	if ( Math.abs( xvel ) >= mMinimumVelocity && scroller.isScrollingInDirection( xvel, 0 ) ) {
		// Keep the fling alive a little longer
		postDelayed( this, FLYWHEEL_TIMEOUT );
	} else {
		endFling();
		mTouchMode = TOUCH_MODE_SCROLL;
		reportScrollStateChange( OnScrollListener.SCROLL_STATE_TOUCH_SCROLL );
	}
}
 
Example 6
Source File: Utils.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev,
                                                              VelocityTracker tracker) {

    // Check the dot product of current velocities.
    // If the pointer that left was opposing another velocity vector, clear.
    tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
    final int upIndex = ev.getActionIndex();
    final int id1 = ev.getPointerId(upIndex);
    final float x1 = tracker.getXVelocity(id1);
    final float y1 = tracker.getYVelocity(id1);
    for (int i = 0, count = ev.getPointerCount(); i < count; i++) {
        if (i == upIndex)
            continue;

        final int id2 = ev.getPointerId(i);
        final float x = x1 * tracker.getXVelocity(id2);
        final float y = y1 * tracker.getYVelocity(id2);

        final float dot = x + y;
        if (dot < 0) {
            tracker.clear();
            break;
        }
    }
}
 
Example 7
Source File: DraggableSwipeBack.java    From SwipeBack with Apache License 2.0 5 votes vote down vote up
protected float getXVelocity(VelocityTracker velocityTracker) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
		return velocityTracker.getXVelocity(mActivePointerId);
	}

	return velocityTracker.getXVelocity();
}
 
Example 8
Source File: DraggableDrawer.java    From zhangshangwuda with Apache License 2.0 5 votes vote down vote up
protected float getXVelocity(VelocityTracker velocityTracker) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return velocityTracker.getXVelocity(mActivePointerId);
    }

    return velocityTracker.getXVelocity();
}
 
Example 9
Source File: PileLayout.java    From timecat with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            // 此处说明底层没有子View愿意消费Touch事件
            break;

        case MotionEvent.ACTION_MOVE:
            int currentX = (int) event.getX();
            int dx = (int) (currentX - lastX);
            requireScrollChange(dx);
            lastX = currentX;
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float velocity = velocityTracker.getXVelocity();
            recycleVelocityTracker();

            onRelease(event.getX(), velocity);
            break;
    }
    return true;
}
 
Example 10
Source File: ElasticPager.java    From FlowingPager with Apache License 2.0 5 votes vote down vote up
protected float getXVelocity(VelocityTracker velocityTracker) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return velocityTracker.getXVelocity(mActivePointerId);
    }

    return velocityTracker.getXVelocity();
}
 
Example 11
Source File: VelocityTrackerCompat.java    From android-recipes-app with Apache License 2.0 4 votes vote down vote up
@Override
public float getXVelocity(VelocityTracker tracker, int pointerId) {
    return tracker.getXVelocity();
}
 
Example 12
Source File: ScrollLayout.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	if (mVelocityTracker == null) {
		mVelocityTracker = VelocityTracker.obtain();
	}
	mVelocityTracker.addMovement(event);
	final int action = event.getAction();
	final float x = event.getX();
	switch (action) {
	case MotionEvent.ACTION_DOWN:
		if (!mScroller.isFinished()) {
			mScroller.abortAnimation();
		}
		mLastMotionX = x;
		break;

	case MotionEvent.ACTION_MOVE:
		int deltaX = (int) (mLastMotionX - x);
		mLastMotionX = x;
		if (!(mCurScreen == 0 && deltaX < 0 || mCurScreen == getChildCount() - 1
				&& deltaX > 0)) {
			scrollBy(deltaX, 0);
		}
		break;

	case MotionEvent.ACTION_UP:
		final VelocityTracker velocityTracker = mVelocityTracker;
		velocityTracker.computeCurrentVelocity(1000);
		int velocityX = (int) velocityTracker.getXVelocity();
		if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
			snapToScreen(mCurScreen - 1);
		} else if (velocityX < -SNAP_VELOCITY
				&& mCurScreen < getChildCount() - 1) {
			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;
}
 
Example 13
Source File: VelocityTrackerCompatHoneycomb.java    From guideshow with MIT License 4 votes vote down vote up
public static float getXVelocity(VelocityTracker tracker, int pointerId) {
    return tracker.getXVelocity(pointerId);
}
 
Example 14
Source File: VelocityTrackerCompat.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
@Override
public float getXVelocity(VelocityTracker tracker, int pointerId) {
    return tracker.getXVelocity();
}
 
Example 15
Source File: KJViewPager.java    From KJFrameForAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isScroll) {
        return false;
    }
    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;
}
 
Example 16
Source File: SlidingDrawer.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	if (mLocked) {
		return true;
	}

	if (mTracking) {
		mVelocityTracker.addMovement(event);
		final int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_MOVE:
			moveHandle((int) (event.getY()));
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL: {
			final VelocityTracker velocityTracker = mVelocityTracker;
			velocityTracker.computeCurrentVelocity(mVelocityUnits);

			float yVelocity = velocityTracker.getYVelocity();
			float xVelocity = velocityTracker.getXVelocity();
			boolean negative;

			negative = yVelocity < 0;
			if (xVelocity < 0) {
				xVelocity = -xVelocity;
			}
			if (xVelocity > mMaximumMinorVelocity) {
				xVelocity = mMaximumMinorVelocity;
			}

			float velocity = (float) Math.hypot(xVelocity, yVelocity);
			if (negative) {
				velocity = -velocity;
			}

			final int top = mHandle.getTop();

			if (Math.abs(velocity) < mMaximumTapVelocity) {
				if ((mExpanded && top < mTapThreshold + mTopOffset) || (!mExpanded && top > mBottomOffset + getBottom() - getTop() - mHandleHeight - mTapThreshold)) {

					if (mAllowSingleTap) {
						playSoundEffect(SoundEffectConstants.CLICK);

						if (mExpanded) {
							animateClose(top);
						} else {
							animateOpen(top);
						}
					} else {
						performFling(top, velocity, false);
					}

				} else {
					performFling(top, velocity, false);
				}
			} else {
				performFling(top, velocity, false);
			}
		}
			break;
		}
	}

	return mTracking || mAnimating || super.onTouchEvent(event);
}
 
Example 17
Source File: VelocityTrackerCompatHoneycomb.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public static float getXVelocity(VelocityTracker tracker, int pointerId) {
    return tracker.getXVelocity(pointerId);
}
 
Example 18
Source File: KugouLayout.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
protected float getXVelocity(VelocityTracker velocityTracker) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return velocityTracker.getXVelocity(mActivePointerId);
    }
    return velocityTracker.getXVelocity();
}
 
Example 19
Source File: MyScrollView.java    From palmsuda with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	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;
			break;
		case MotionEvent.ACTION_MOVE :
			int deltaX = (int) (mLastMotionX - x);
			mLastMotionX = x;
			scrollBy(deltaX, 0);
			updateWallpaperOffset();
			break;
		case MotionEvent.ACTION_UP :
			final VelocityTracker velocityTracker = mVelocityTracker;
			velocityTracker.computeCurrentVelocity(1000);
			int velocityX = (int) velocityTracker.getXVelocity();
			if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
				snapToScreen(mCurScreen - 1);
			} else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
				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;
}
 
Example 20
Source File: ViewFlow.java    From Roid-Library with Apache License 2.0 4 votes vote down vote up
private boolean handleTouchEvent(MotionEvent event, boolean isIntercept) {
    if (getChildCount() == 0) {
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    final int action = event.getAction();
    final float x = event.getX();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            mLastMotionX = x;
            mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
            break;
        case MotionEvent.ACTION_MOVE:
            final int deltaX = (int) (mLastMotionX - x);
            boolean xMoved = Math.abs(deltaX) > TOUCH_SLOP;
            if (xMoved) {
                mTouchState = TOUCH_STATE_SCROLLING;
            }
            if (mTouchState == TOUCH_STATE_SCROLLING) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                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, MAX_VELOCITY);
                int velocityX = (int) velocityTracker.getXVelocity();
                if (velocityX > SNAP_VELOCITY) {
                    previous();
                } else if (velocityX < -SNAP_VELOCITY) {
                    next();
                } else {
                    snapToDestination();
                }
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                }
            }
            mTouchState = TOUCH_STATE_REST;
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            mTouchState = TOUCH_STATE_REST;
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            if (!isIntercept) {
                snapToDestination();
            }
            break;
    }
    return !isIntercept;
}