List of usage examples for android.view VelocityTracker getXVelocity
public float getXVelocity()
From source file:com.bitflake.counter.HorizontalPicker.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; }//from w w w .j av a 2s . c o m if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_MOVE: float currentMoveX = event.getX(); int deltaMoveX = (int) (lastDownEventX - currentMoveX); if (scrollingX || (Math.abs(deltaMoveX) > touchSlop) && values != null && values.length > 0) { if (!scrollingX) { deltaMoveX = 0; pressedItem = -1; scrollingX = true; getParent().requestDisallowInterceptTouchEvent(true); stopMarqueeIfNeeded(); } final int range = getScrollRange(); if (overScrollBy(deltaMoveX, 0, getScrollX(), 0, range, 0, overscrollDistance, 0, true)) { mVelocityTracker.clear(); } final float pulledToX = getScrollX() + deltaMoveX; if (pulledToX < 0) { leftEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!rightEdgeEffect.isFinished()) { rightEdgeEffect.onRelease(); } } else if (pulledToX > range) { rightEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!leftEdgeEffect.isFinished()) { leftEdgeEffect.onRelease(); } } lastDownEventX = currentMoveX; invalidate(); } break; case MotionEvent.ACTION_DOWN: if (!adjustScrollerX.isFinished()) { adjustScrollerX.forceFinished(true); } else if (!flingScrollerX.isFinished()) { flingScrollerX.forceFinished(true); } else { scrollingX = false; } lastDownEventX = event.getX(); if (!scrollingX) { pressedItem = getPositionFromTouch(event.getX()); } invalidate(); break; case MotionEvent.ACTION_UP: VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity); int initialVelocityX = (int) velocityTracker.getXVelocity(); if (scrollingX && Math.abs(initialVelocityX) > mMinimumFlingVelocity) { flingX(initialVelocityX); } else if (values != null) { float positionX = event.getX(); if (!scrollingX) { int itemPos = getPositionOnScreen(positionX); int relativePos = itemPos - sideItems; if (relativePos == 0) { selectItem(); } else { smoothScrollBy(relativePos); } } else if (scrollingX) { finishScrolling(); } } mVelocityTracker.recycle(); mVelocityTracker = null; if (leftEdgeEffect != null) { leftEdgeEffect.onRelease(); rightEdgeEffect.onRelease(); } case MotionEvent.ACTION_CANCEL: pressedItem = -1; invalidate(); if (leftEdgeEffect != null) { leftEdgeEffect.onRelease(); rightEdgeEffect.onRelease(); } break; } return true; }
From source file:kr.selfcontrol.selflocklauncher.picker.HorizontalPicker.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; }/*from w w w. jav a2 s . c om*/ if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_MOVE: float currentMoveX = event.getX(); int deltaMoveX = (int) (mLastDownEventX - currentMoveX); if (mScrollingX || (Math.abs(deltaMoveX) > mTouchSlop) && mValues != null && mValues.length > 0) { if (!mScrollingX) { deltaMoveX = 0; mPressedItem = -1; mScrollingX = true; stopMarqueeIfNeeded(); } final int range = getScrollRange(); if (overScrollBy(deltaMoveX, 0, getScrollX(), 0, range, 0, mOverscrollDistance, 0, true)) { mVelocityTracker.clear(); } final float pulledToX = getScrollX() + deltaMoveX; if (pulledToX < 0) { mLeftEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!mRightEdgeEffect.isFinished()) { mRightEdgeEffect.onRelease(); } } else if (pulledToX > range) { mRightEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!mLeftEdgeEffect.isFinished()) { mLeftEdgeEffect.onRelease(); } } mLastDownEventX = currentMoveX; invalidate(); } break; case MotionEvent.ACTION_DOWN: if (!mAdjustScrollerX.isFinished()) { mAdjustScrollerX.forceFinished(true); } else if (!mFlingScrollerX.isFinished()) { mFlingScrollerX.forceFinished(true); } else { mScrollingX = false; } mLastDownEventX = event.getX(); if (!mScrollingX) { mPressedItem = getPositionFromTouch(event.getX()); } invalidate(); break; case MotionEvent.ACTION_UP: VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); int initialVelocityX = (int) velocityTracker.getXVelocity(); if (mScrollingX && Math.abs(initialVelocityX) > mMinimumFlingVelocity) { flingX(initialVelocityX); } else if (mValues != null) { float positionX = event.getX(); if (!mScrollingX) { int itemPos = getPositionOnScreen(positionX); int relativePos = itemPos - mSideItems; if (relativePos == 0) { selectItem(); } else { smoothScrollBy(relativePos); } } else if (mScrollingX) { finishScrolling(); } } mVelocityTracker.recycle(); mVelocityTracker = null; if (mLeftEdgeEffect != null) { mLeftEdgeEffect.onRelease(); mRightEdgeEffect.onRelease(); } case MotionEvent.ACTION_CANCEL: mPressedItem = -1; invalidate(); if (mLeftEdgeEffect != null) { mLeftEdgeEffect.onRelease(); mRightEdgeEffect.onRelease(); } break; } return true; }
From source file:jmri.enginedriver.throttle.java
public void gestureMove(MotionEvent event) { // Log.d("Engine_Driver", "gestureMove action " + event.getAction()); if (gestureInProgress) { // stop the gesture timeout timer mainapp.throttle_msg_handler.removeCallbacks(gestureStopped); mVelocityTracker.addMovement(event); if ((event.getEventTime() - gestureLastCheckTime) > gestureCheckRate) { // monitor velocity and fail gesture if it is too low gestureLastCheckTime = event.getEventTime(); final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); int velocityY = (int) velocityTracker.getYVelocity(); // Log.d("Engine_Driver", "gestureVelocity vel " + velocityX); if ((Math.abs(velocityX) < threaded_application.min_fling_velocity) && (Math.abs(velocityY) < threaded_application.min_fling_velocity)) { gestureFailed(event);/*from w w w .j av a 2 s . c om*/ } } if (gestureInProgress) { // restart the gesture timeout timer mainapp.throttle_msg_handler.postDelayed(gestureStopped, gestureCheckRate); } } }