android.view.VelocityTracker#computeCurrentVelocity ( )源码实例Demo

下面列出了android.view.VelocityTracker#computeCurrentVelocity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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;
}
 
源代码2 项目: XBanner   文件: XBannerViewPager.java
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;
}
 
源代码3 项目: Android-SDK-Demo   文件: SdkCenteredViewPager.java
/**
 * 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;
}
 
源代码4 项目: ankihelper   文件: VerticalViewPager.java
/**
 * 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 height = getClientHeight();
    final int scrollY = getScrollY();
    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;
    final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
    final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
源代码5 项目: RxJavaApp   文件: ViewPager.java
/**
 * 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;
}
 
源代码6 项目: StackCardsView   文件: SwipeTouchHelper.java
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());
}
 
源代码7 项目: StackOverView   文件: SwipeHelper.java
private void endSwipe(VelocityTracker velocityTracker) {
    float maxVelocity = MAX_DISMISS_VELOCITY * mDensityScale;
    velocityTracker.computeCurrentVelocity(1000 /* px/sec */, maxVelocity);
    float velocity = getVelocity(velocityTracker);
    float perpendicularVelocity = getPerpendicularVelocity(velocityTracker);
    float escapeVelocity = SWIPE_ESCAPE_VELOCITY * mDensityScale;
    float translation = getTranslation(mCurrView);
    // Decide whether to dismiss the current view
    boolean childSwipedFarEnough = Math.abs(translation) > 0.6 * getSize(mCurrView);
    boolean childSwipedFastEnough = (Math.abs(velocity) > escapeVelocity) &&
            (Math.abs(velocity) > Math.abs(perpendicularVelocity)) &&
            (velocity > 0) == (translation > 0);

    boolean dismissChild = mCallback.canChildBeDismissed(mCurrView)
            && isValidSwipeDirection(translation)
            && (childSwipedFastEnough || childSwipedFarEnough);

    if (dismissChild) {
        // flingadingy
        dismissChild(mCurrView, childSwipedFastEnough ? velocity : 0f);
    } else {
        // snappity
        mCallback.onDragCancelled(mCurrView);
        snapChild(mCurrView, velocity);
    }
}
 
源代码8 项目: RxZhihuDaily   文件: ViewPagerCompact.java
/**
 * 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;
}
 
源代码9 项目: ticdesign   文件: VerticalViewPager.java
/**
 * 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 height = getClientHeight();
    final int scrollY = getScrollY();
    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;
    final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
    final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
源代码10 项目: Ticket-Analysis   文件: Utils.java
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;
        }
    }
}
 
源代码11 项目: KUtils-master   文件: BGAViewPager.java
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;
}
 
源代码12 项目: Overchan-Android   文件: VerticalViewPager.java
/**
 * 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 height = getClientHeight();
    final int scrollY = getScrollY();
    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;
    final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
    final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
源代码13 项目: AsteroidOSSync   文件: CustomViewPager.java
/**
 * 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;
}
 
源代码14 项目: iMoney   文件: Utils.java
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;
        }
    }
}
 
源代码15 项目: YCCardView   文件: CardViewLayout.java
@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);
            int velocity = (int) velocityTracker.getXVelocity();
            recycleVelocityTracker();

            onRelease(event.getX(), velocity);
            break;
        default:
            break;
    }
    return true;
}
 
源代码16 项目: flickr-uploader   文件: SlidingDrawer.java
@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);
}
 
源代码17 项目: NewXmPluginSDK   文件: ViewFlow.java
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
	if (getChildCount() == 0)
		return false;

	if (mVelocityTracker == null) {
		mVelocityTracker = VelocityTracker.obtain();
	}
	mVelocityTracker.addMovement(ev);
	
	mHandler.removeMessages(CANCLE_MSG);
	
	final int action = ev.getAction();
	final float x = ev.getX();

	switch (action) {
	case MotionEvent.ACTION_DOWN:
		/*
		 * If being flinged and user touches, stop the fling. isFinished
		 * will be false if being flinged.
		 */
		if (!mScroller.isFinished()) {
			mScroller.abortAnimation();
		}

		// Remember where the motion event started
		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) > mTouchSlop;

		if (xMoved) {
			// Scroll if the user moved far enough along the X axis
			mTouchState = TOUCH_STATE_SCROLLING;

			if (mViewInitializeListener != null)
				initializeView(deltaX);
		}

		if (mTouchState == TOUCH_STATE_SCROLLING) {
			// Scroll to follow the motion event

			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()
						- getPaddingRight() - getHorizontalFadingEdgeLength()
						- 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, mMaximumVelocity);
			int velocityX = (int) velocityTracker.getXVelocity();

			if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
				// Fling hard enough to move left
				snapToScreen(mCurrentScreen - 1);
			} else if (velocityX < -SNAP_VELOCITY
					&& mCurrentScreen < getChildCount() - 1) {
				// Fling hard enough to move right
				snapToScreen(mCurrentScreen + 1);
			} else {
				snapToDestination();
			}

			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
		}

		mTouchState = TOUCH_STATE_REST;

		break;
	case MotionEvent.ACTION_CANCEL:
		mTouchState = TOUCH_STATE_REST;
	}
	return false;
}
 
@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) (mVertical ? event.getY() : event.getX()) - mTouchDelta);
                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;

                final boolean vertical = mVertical;
                if (vertical) {
                    negative = yVelocity < 0;
                    if (xVelocity < 0) {
                        xVelocity = -xVelocity;
                    }
                    if (xVelocity > mMaximumMinorVelocity) {
                        xVelocity = mMaximumMinorVelocity;
                    }
                } else {
                    negative = xVelocity < 0;
                    if (yVelocity < 0) {
                        yVelocity = -yVelocity;
                    }
                    if (yVelocity > mMaximumMinorVelocity) {
                        yVelocity = mMaximumMinorVelocity;
                    }
                }

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

                final int top = mHandle.getTop();
                final int left = mHandle.getLeft();

                if (Math.abs(velocity) < mMaximumTapVelocity) {

                    if (inThreshold(top, left)) {
                        if (mAllowSingleTap) {
                            playSoundEffect(SoundEffectConstants.CLICK);

                            if (mExpanded) {
                                //animateClose(vertical ? top : left);
                                animateClose(getSide());
                            } else {
                                animateOpen(getSide());
                                //animateOpen(vertical ? top : left);
                            }
                        } else {
                            performFling(vertical ? top : left, velocity, false);
                        }

                    } else {
                        performFling(vertical ? top : left, velocity, false);
                    }
                } else {
                    performFling(vertical ? top : left, velocity, false);
                }
            }
            break;
        }
    }

    return mTracking || mAnimating || super.onTouchEvent(event);
}
 
源代码19 项目: Conquer   文件: JellyViewPager.java
@Override
public boolean onTouchEvent(MotionEvent event) {
	if (vTracker == null) {
		vTracker = VelocityTracker.obtain();
	}
	vTracker.addMovement(event);
	float currentY = event.getY();
	switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			downY = event.getY();
			downX = event.getX();
			if (!currentViewRect.contains((int) downX, (int) downY)) {
				return false;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			distanceY = currentY - downY;
			if (Math.abs(distanceY) > mTouchSlop) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrolled(currentItem, (int) Math.abs(distanceY) / getHeight(), (int) distanceY);
					pageChangeListener.onPageScrollStateChanged(SCROLL_STATE_DRAGGING);
				}
				tranSpring.setEndValue(distanceY);
				float degree = MAX_DEGREE * distanceY / mHeight;
				if (downX < mWidth / 2) {
					degree = -degree;
				}
				rotateSpring.setEndValue(degree);
			}
			break;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			if (Math.abs(distanceY) > mTouchSlop) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrollStateChanged(SCROLL_STATE_SETTLING);
				}
				final VelocityTracker tracker = vTracker;
				tracker.computeCurrentVelocity(UNIT);
				float velocityY = tracker.getYVelocity();
				animOutIfNeeded(currentY - downY, velocityY);
				if (vTracker != null) {
					vTracker.recycle();
					vTracker = null;
				}
			}
			break;
	}
	return true;
}
 
源代码20 项目: letv   文件: ViewPager.java
public boolean onTouchEvent(MotionEvent ev) {
    if (this.mFakeDragging) {
        return true;
    }
    if (ev.getAction() == 0 && ev.getEdgeFlags() != 0) {
        return false;
    }
    if (this.mAdapter == null || this.mAdapter.getCount() == 0) {
        return false;
    }
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    }
    this.mVelocityTracker.addMovement(ev);
    boolean needsInvalidate = false;
    float x;
    switch (ev.getAction() & 255) {
        case 0:
            this.mScroller.abortAnimation();
            this.mPopulatePending = false;
            populate();
            x = ev.getX();
            this.mInitialMotionX = x;
            this.mLastMotionX = x;
            x = ev.getY();
            this.mInitialMotionY = x;
            this.mLastMotionY = x;
            this.mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            break;
        case 1:
            if (this.mIsBeingDragged) {
                VelocityTracker velocityTracker = this.mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, (float) this.mMaximumVelocity);
                int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, this.mActivePointerId);
                this.mPopulatePending = true;
                int width = getClientWidth();
                int scrollX = getScrollX();
                ItemInfo ii = infoForCurrentScrollPosition();
                setCurrentItemInternal(determineTargetPage(ii.position, ((((float) scrollX) / ((float) width)) - ii.offset) / ii.widthFactor, initialVelocity, (int) (MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, this.mActivePointerId)) - this.mInitialMotionX)), true, true, initialVelocity);
                needsInvalidate = resetTouch();
                break;
            }
            break;
        case 2:
            if (!this.mIsBeingDragged) {
                int pointerIndex = MotionEventCompat.findPointerIndex(ev, this.mActivePointerId);
                if (pointerIndex == -1) {
                    needsInvalidate = resetTouch();
                    break;
                }
                float x2 = MotionEventCompat.getX(ev, pointerIndex);
                float xDiff = Math.abs(x2 - this.mLastMotionX);
                float y = MotionEventCompat.getY(ev, pointerIndex);
                float yDiff = Math.abs(y - this.mLastMotionY);
                if (xDiff > ((float) this.mTouchSlop) && xDiff > yDiff) {
                    this.mIsBeingDragged = true;
                    requestParentDisallowInterceptTouchEvent(true);
                    if (x2 - this.mInitialMotionX > 0.0f) {
                        x = this.mInitialMotionX + ((float) this.mTouchSlop);
                    } else {
                        x = this.mInitialMotionX - ((float) this.mTouchSlop);
                    }
                    this.mLastMotionX = x;
                    this.mLastMotionY = y;
                    setScrollState(1);
                    setScrollingCacheEnabled(true);
                    ViewParent parent = getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
            }
            if (this.mIsBeingDragged) {
                needsInvalidate = false | performDrag(MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, this.mActivePointerId)));
                break;
            }
            break;
        case 3:
            if (this.mIsBeingDragged) {
                scrollToItem(this.mCurItem, true, 0, false);
                needsInvalidate = resetTouch();
                break;
            }
            break;
        case 5:
            int index = MotionEventCompat.getActionIndex(ev);
            this.mLastMotionX = MotionEventCompat.getX(ev, index);
            this.mActivePointerId = MotionEventCompat.getPointerId(ev, index);
            break;
        case 6:
            onSecondaryPointerUp(ev);
            this.mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, this.mActivePointerId));
            break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}