public override bool OnTouchEvent(MotionEvent e) { var index = e.ActionIndex; var action = (MotionEventActions)e.ActionMasked; var pointerId = e.GetPointerId(index); switch (action) { case MotionEventActions.Down: if (this._velocityTracket == null) { this._velocityTracket = VelocityTracker.Obtain(); } else { this._velocityTracket.Clear(); } this._velocityTracket.AddMovement(e); break; case MotionEventActions.Move: this._velocityTracket.AddMovement(e); this._velocityTracket.ComputeCurrentVelocity(1000); Log.Debug(DebugTag, "X velocity: " + VelocityTrackerCompat.GetXVelocity(this._velocityTracket, pointerId)); Log.Debug(DebugTag, "Y velocity: " + VelocityTrackerCompat.GetYVelocity(this._velocityTracket, pointerId)); break; case MotionEventActions.Cancel: case MotionEventActions.Up: this._velocityTracket.Recycle(); break; default: break; } return(true); }
public override bool OnTouchEvent(MotionEvent ev) { if (!mEnabled) { return(false); } if (!mIsBeingDragged && !ThisTouchAllowed(ev)) { return(false); } // if (!mIsBeingDragged && !mQuickReturn) // return false; MotionEventActions action = ev.Action; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(ev); switch (action & (MotionEventActions)MotionEventCompat.ActionMask) { case MotionEventActions.Down: /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ CompleteScroll(); // Remember where the motion event started int index = MotionEventCompat.GetActionIndex(ev); mActivePointerId = MotionEventCompat.GetPointerId(ev, index); mLastMotionX = mInitialMotionX = ev.GetX(); break; case MotionEventActions.Move: if (!mIsBeingDragged) { DetermineDrag(ev); if (mIsUnableToDrag) { return(false); } } if (mIsBeingDragged) { // Scroll to follow the motion event int activePointerIndex = GetPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER) { break; } float x = MotionEventCompat.GetX(ev, activePointerIndex); float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = ScrollX; float scrollX = oldScrollX + deltaX; float leftBound = LeftBound; float rightBound = RightBound; if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { scrollX = rightBound; } // Don't lose the rounded component mLastMotionX += scrollX - (int)scrollX; ScrollTo((int)scrollX, ScrollY); PageScrolled((int)scrollX); } break; case MotionEventActions.Up: if (mIsBeingDragged) { VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int)VelocityTrackerCompat.GetXVelocity(velocityTracker, mActivePointerId); int scrollX = ScrollX; float pageOffset = (float)(scrollX - GetDestScrollX(mCurItem)) / BehindWidth; int activePointerIndex = GetPointerIndex(ev, mActivePointerId); if (mActivePointerId != INVALID_POINTER) { float x = MotionEventCompat.GetX(ev, activePointerIndex); int totalDelta = (int)(x - mInitialMotionX); int nextPage = DetermineTargetPage(pageOffset, initialVelocity, totalDelta); SetCurrentItemInternal(nextPage, true, true, initialVelocity); } else { SetCurrentItemInternal(mCurItem, true, true, initialVelocity); } mActivePointerId = INVALID_POINTER; EndDrag(); } else if (mQuickReturn && mViewBehind.MenuTouchInQuickReturn(mContent, mCurItem, ev.GetX() + mScrollX)) { // close the menu CurrentItem = 1; EndDrag(); } break; case MotionEventActions.Cancel: if (mIsBeingDragged) { SetCurrentItemInternal(mCurItem, true, true); mActivePointerId = INVALID_POINTER; EndDrag(); } break; case (MotionEventActions)MotionEventCompat.ActionPointerDown: { int indexx = MotionEventCompat.GetActionIndex(ev); mLastMotionX = MotionEventCompat.GetX(ev, indexx); mActivePointerId = MotionEventCompat.GetPointerId(ev, indexx); break; } case (MotionEventActions)MotionEventCompat.ActionPointerUp: OnSecondaryPointerUp(ev); int pointerIndex = GetPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER) { break; } mLastMotionX = MotionEventCompat.GetX(ev, pointerIndex); break; } return(true); }
public override bool OnTouchEvent(MotionEvent ev) { if (!_enabled) return false; if (!_isBeingDragged && !ThisTouchAllowed(ev)) return false; if (VelocityTracker == null) VelocityTracker = VelocityTracker.Obtain(); VelocityTracker.AddMovement(ev); var action = (int)ev.Action & MotionEventCompat.ActionMask; switch (action) { case (int) MotionEventActions.Down: CompleteScroll(); var index = MotionEventCompat.GetActionIndex(ev); ActivePointerId = MotionEventCompat.GetPointerId(ev, index); _lastMotionX = _initialMotionX = ev.GetX(); break; case (int) MotionEventActions.Move: if (!_isBeingDragged) { DetermineDrag(ev); if (_isUnableToDrag) return false; } if (_isBeingDragged) { var activePointerIndex = GetPointerIndex(ev, ActivePointerId); if (ActivePointerId == InvalidPointer) break; var x = MotionEventCompat.GetX(ev, activePointerIndex); var deltaX = _lastMotionX - x; _lastMotionX = x; var oldScrollX = ScrollX; var scrollX = oldScrollX + deltaX; var leftBound = LeftBound; var rightBound = RightBound; if (scrollX < leftBound) scrollX = leftBound; else if (scrollX > rightBound) scrollX = rightBound; _lastMotionX += scrollX - (int) scrollX; ScrollTo((int) scrollX, ScrollY); OnPageScrolled((int)scrollX); } break; case (int) MotionEventActions.Up: if (_isBeingDragged) { var velocityTracker = VelocityTracker; velocityTracker.ComputeCurrentVelocity(1000, MaximumVelocity); var initialVelocity = (int) VelocityTrackerCompat.GetXVelocity(velocityTracker, ActivePointerId); var scrollX = ScrollX; var pageOffset = (float) (scrollX - GetDestScrollX(_curItem)) / BehindWidth; var activePointerIndex = GetPointerIndex(ev, ActivePointerId); if (ActivePointerId != InvalidPointer) { var x = MotionEventCompat.GetX(ev, activePointerIndex); var totalDelta = (int) (x - _initialMotionX); var nextPage = DetermineTargetPage(pageOffset, initialVelocity, totalDelta); SetCurrentItemInternal(nextPage, true, true, initialVelocity); } else SetCurrentItemInternal(_curItem, true, true, initialVelocity); ActivePointerId = InvalidPointer; EndDrag(); } else if (_quickReturn && _viewBehind.MenuTouchInQuickReturn(_content, _curItem, ev.GetX() + _scrollX)) { SetCurrentItem(1); EndDrag(); } break; case (int) MotionEventActions.Cancel: if (_isBeingDragged) { SetCurrentItemInternal(_curItem, true, true); ActivePointerId = InvalidPointer; EndDrag(); } break; case MotionEventCompat.ActionPointerDown: var indexx = MotionEventCompat.GetActionIndex(ev); _lastMotionX = MotionEventCompat.GetX(ev, indexx); ActivePointerId = MotionEventCompat.GetPointerId(ev, indexx); break; case MotionEventCompat.ActionPointerUp: OnSecondaryPointerUp(ev); var pointerIndex = GetPointerIndex(ev, ActivePointerId); if (ActivePointerId == InvalidPointer) break; _lastMotionX = MotionEventCompat.GetX(ev, pointerIndex); break; } return true; }
private float getYVelocity() { mVelocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity); return(VelocityTrackerCompat.GetYVelocity(mVelocityTracker, mActivePointerId)); }