public bool onTouch(MotionEvent ev) { if (velocityTracker == null) { velocityTracker = VelocityTracker.Obtain(); } velocityTracker.AddMovement(ev); if (ev.Action == MotionEventActions.Down) { if (!scroller.IsFinished) { scroller.AbortAnimation(); } isSmoothScrolling = false; } else if (ev.Action == MotionEventActions.Move) { velocityTracker.AddMovement(ev); velocityTracker.ComputeCurrentVelocity(500); } else if (ev.Action == MotionEventActions.Up) { handleHorizontalScrolling(); velocityTracker.Recycle(); velocityTracker.Clear(); velocityTracker = null; isScrolling = false; } return(false); }
public override bool OnTouchEvent(MotionEvent ev) { switch (ev.Action) { case MotionEventActions.Cancel: #region Cancel if (null != mVelocityTracker) { mVelocityTracker.Recycle(); mVelocityTracker = null; } #endregion break; case MotionEventActions.Down: #region Down mVelocityTracker = VelocityTracker.Obtain(); mVelocityTracker.AddMovement(ev); mLastTouchX = GetActiveX(ev); mLastTouchY = GetActiveY(ev); mIsDragging = false; #endregion break; case MotionEventActions.Move: #region Move float x = GetActiveX(ev); float y = GetActiveY(ev); float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { // Use Pythagoras to see if drag length is larger than // touch slop mIsDragging = Math.Sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { mListener.OnDrag(dx, dy); mLastTouchX = x; mLastTouchY = y; if (null != mVelocityTracker) { mVelocityTracker.AddMovement(ev); } } #endregion break; } return(true); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); FindViewById(Android.Resource.Id.Content).SystemUiVisibility = (StatusBarVisibility)(SystemUiFlags.LayoutStable | SystemUiFlags.LayoutFullscreen | SystemUiFlags.HideNavigation); stiffness = FindViewById <SeekBar>(Resource.Id.stiffness); damping = FindViewById <SeekBar>(Resource.Id.damping); velocityTracker = VelocityTracker.Obtain(); View box = FindViewById(Resource.Id.box); box.Touch += (sender, args) => { switch (args.Event.Action) { case MotionEventActions.Down: downX = args.Event.GetX(); downY = args.Event.GetY(); velocityTracker.AddMovement(args.Event); break; case MotionEventActions.Move: box.TranslationX = args.Event.GetX() - downX; box.TranslationY = args.Event.GetY() - downY; velocityTracker.AddMovement(args.Event); break; case MotionEventActions.Up: case MotionEventActions.Cancel: velocityTracker.ComputeCurrentVelocity(1000); if (box.TranslationX != 0) { SpringAnimation animX = new SpringAnimation(box, DynamicAnimation.TranslationX, 0); animX.Spring.SetStiffness(Stiffness); animX.Spring.SetDampingRatio(Damping); animX.SetStartVelocity(velocityTracker.XVelocity); animX.Start(); } if (box.TranslationY != 0) { SpringAnimation animY = new SpringAnimation(box, DynamicAnimation.TranslationY, 0); animY.Spring.SetStiffness(Stiffness); animY.Spring.SetDampingRatio(Damping); animY.SetStartVelocity(velocityTracker.YVelocity); animY.Start(); } velocityTracker.Clear(); break; } }; }
public override bool OnInterceptTouchEvent(MotionEvent ev) { if (!_enabled) return false; var action = (int) ev.Action & MotionEventCompat.ActionMask; #if DEBUG if (action == (int) MotionEventActions.Down) Log.Verbose(Tag, "Recieved ACTION_DOWN"); #endif if (action == (int) MotionEventActions.Cancel || action == (int) MotionEventActions.Up || (action != (int) MotionEventActions.Down && _isUnableToDrag)) { EndDrag(); return false; } switch (action) { case (int) MotionEventActions.Move: DetermineDrag(ev); break; case (int) MotionEventActions.Down: var index = MotionEventCompat.GetActionIndex(ev); ActivePointerId = MotionEventCompat.GetPointerId(ev, index); if (ActivePointerId == InvalidPointer) break; _lastMotionX = _initialMotionX = MotionEventCompat.GetX(ev, index); _lastMotionY = MotionEventCompat.GetY(ev, index); if (ThisTouchAllowed(ev)) { _isBeingDragged = false; _isUnableToDrag = false; if (IsMenuOpen && _viewBehind.MenuTouchInQuickReturn(_content, _curItem, ev.GetX() + _scrollX)) _quickReturn = true; } else _isUnableToDrag = true; break; case (int) MotionEventActions.PointerUp: OnSecondaryPointerUp(ev); break; } if (!_isBeingDragged) { if (VelocityTracker == null) VelocityTracker = VelocityTracker.Obtain(); VelocityTracker.AddMovement(ev); } return _isBeingDragged || _quickReturn; }
public override bool OnTouchEvent(MotionEvent ev) { if (ev.Action == MotionEventActions.Move) { tracker.AddMovement(ev); } if (ev.Action == MotionEventActions.Up) { tracker.ComputeCurrentVelocity(1000); var velocity = tracker.XVelocity; Console.WriteLine("Velocity: " + velocity); tracker.Clear(); if (Math.Abs(velocity) > 1000) { timer = new System.Timers.Timer(125); timer.Start(); timer.Elapsed += delegate { CallHandler(); timer.Stop(); timer.Dispose(); timer = null; }; } else { CallHandler(); } } return(base.OnTouchEvent(ev)); }
bool CaptureMovementCheck(MotionEvent ev) { if (ev.Action == MotionEventActions.Down) { startX = (int)ev.GetX(); startY = (int)ev.GetY(); // Only work if the initial touch was in the start strip when the menu is closed // When the menu is opened, anywhere will do if (!opened && (startX > Context.ToPixels(30))) { return(false); } velocityTracker = VelocityTracker.Obtain(); velocityTracker.AddMovement(ev); preTracking = true; stateBeforeTracking = opened; return(false); } if (ev.Action == MotionEventActions.Up) { preTracking = isTracking = false; } if (!preTracking) { return(false); } velocityTracker.AddMovement(ev); if (ev.Action == MotionEventActions.Move) { // Check we are going in the right direction, if not cancel the current gesture if (!MoveDirectionTest(ev)) { preTracking = false; return(false); } // If the current gesture has not gone long enough don't intercept it just yet var distance = Math.Sqrt(Math.Pow(ev.GetX() - startX, 2) + Math.Pow(ev.GetY() - startY, 2)); if (distance < pagingTouchSlop) { return(false); } } startX = (int)ev.GetX(); startY = (int)ev.GetY(); isTracking = true; return(true); }
bool CaptureMovementCheck(MotionEvent ev) { if (ev.Action == MotionEventActions.Down) { oldY = startY = (int)ev.GetY(); if (!Opened) { return(false); } velocityTracker = VelocityTracker.Obtain(); velocityTracker.AddMovement(ev); preTracking = true; stateBeforeTracking = state; return(false); } if (ev.Action == MotionEventActions.Up) { preTracking = isTracking = false; } if (!preTracking) { return(false); } velocityTracker.AddMovement(ev); if (ev.Action == MotionEventActions.Move) { // Check we are going in the right direction, if not cancel the current gesture if (!MoveDirectionTest(ev)) { preTracking = false; return(false); } // If the current gesture has not gone long enough don't intercept it just yet var distance = Math.Abs(ev.GetY() - startY); if (distance < pagingTouchSlop) { return(false); } } oldY = startY = (int)ev.GetY(); isTracking = true; return(true); }
private bool Down(MotionEvent motionEvent) { // TODO: ensure this is a finger, and set a flag _downX = motionEvent.RawX; _downY = motionEvent.RawY; if (_canDismiss(_token)) { _pauseTimer(true); _velocityTracker = VelocityTracker.Obtain(); _velocityTracker.AddMovement(motionEvent); } return(false); }
bool OnDown(MotionEvent motionEvent) { if (_paused) { return(false); } var rect = new Rect(); int childCount = _listView.ChildCount; var listViewCoords = new int[2]; _listView.GetLocationOnScreen(listViewCoords); int x = (int)motionEvent.RawX - listViewCoords [0]; int y = (int)motionEvent.RawY - listViewCoords [1]; View child; for (int i = 0; i < childCount; i++) { child = _listView.GetChildAt(i); child.GetHitRect(rect); if (rect.Contains(x, y)) { _downView = child; break; } } if (_downView != null) { mDownX = motionEvent.RawX; mDownY = motionEvent.RawY; _downPosition = _listView.GetPositionForView(_downView); if (_dismissCommand.CanExecute(_downPosition)) { _velocityTracker = VelocityTracker.Obtain(); _velocityTracker.AddMovement(motionEvent); } else { _downView = null; } } return(false); }
private bool Move(MotionEvent motionEvent) { _velocityTracker.AddMovement(motionEvent); var deltaX = motionEvent.RawX - _downX; var deltaY = motionEvent.RawY - _downY; if (Math.Abs(deltaX) > _slop && Math.Abs(deltaY) < Math.Abs(deltaX) / 2) { _swiping = true; _swipingSlop = (deltaX > 0 ? _slop : -_slop); if (_view.Parent != null) { _view.Parent.RequestDisallowInterceptTouchEvent(true); } // Cancel listview's touch var cancelEvent = MotionEvent.Obtain(motionEvent); cancelEvent.Action = (MotionEventActions) ((int)MotionEventActions.Cancel | (motionEvent.ActionIndex << (int)MotionEventActions.PointerIndexShift)); _view.OnTouchEvent(cancelEvent); cancelEvent.Recycle(); } if (_swiping) { _translationX = deltaX; _view.TranslationX = deltaX - _swipingSlop; // TODO: use an ease-out interpolator or such _view.Alpha = Math.Max(0f, Math.Min(1f, 1f - 2f * Math.Abs(deltaX) / _viewWidth)); return(true); } return(false); }
private void HandleTouchEvent(MotionEvent @event) { velocityTracker.AddMovement(@event); gestureHelper.OnTouchEvent(@event); if (@event.Action == MotionEventActions.Down) { touching = true; ResetTouchStart(@event.GetX(), @event.GetY()); if (!layerScroller.IsFinished) { layerScroller.AbortAnimation(); } } else if (@event.Action == MotionEventActions.Up || @event.Action == MotionEventActions.Cancel) { touching = false; } }
private bool handleDownEvent(View view, MotionEvent motionEvent) { if (!mSwipeEnabled) { return(false); } View downView = findDownView(motionEvent); if (downView == null) { return(false); } int downPosition = AdapterViewUtil.getPositionForView(mListViewWrapper, downView); mCanDismissCurrent = isDismissable(downPosition); /* Check if we are processing the item at this position */ if (mCurrentPosition == downPosition || downPosition >= mVirtualListCount) { return(false); } if (view != null) { view.OnTouchEvent(motionEvent); } disableHorizontalScrollContainerIfNecessary(motionEvent, downView); mDownX = motionEvent.GetX(); mDownY = motionEvent.GetY(); mCurrentView = downView; mSwipingView = getSwipeView(downView); mCurrentPosition = downPosition; mVelocityTracker = VelocityTracker.Obtain(); mVelocityTracker.AddMovement(motionEvent); return(true); }
public NodeTouchEvent(MotionEvent motion, VelocityTracker velocity) { this.velocity = velocity; velocity.AddMovement(motion); point = new Point(motion.GetX(), motion.GetY()); if (motion.Action == MotionEventActions.Move) { type = TouchType.Move; } else if (motion.Action == MotionEventActions.Down) { type = TouchType.Down; } else if (motion.Action == MotionEventActions.Up) { type = TouchType.Up; } }
public override bool OnTouchEvent(MotionEvent ev) { var hist = 0.0f; var deltaY = 0.0f; var listY = 0.0f; _historyEvent = ev; vT.AddMovement(ev); if (_overscrolling) { var firstOverscroll = _firstOverscroll; _firstOverscroll = false; if (ev.Action == MotionEventActions.Move && ev.HistorySize >= 1) { hist = ev.GetHistoricalY(0, 0); deltaY = ev.GetY() - hist; listY = _listView.GetY(); if (_header != null) { var headerY = _header.GetY(); if (deltaY > 0) { if ((int)Math.Ceiling(headerY) == (0)) { return(_listView.DispatchTouchEvent(ev)); } if (headerY + deltaY <= (0)) { UpdateHeaderPosition(deltaY); _listView.SetY(_listView.GetY() + deltaY); _listView.LayoutParameters.Height = (int)Math.Ceiling(_listView.LayoutParameters.Height + deltaY); _listView.Invalidate(); return(true); } if (headerY + deltaY > 0) { deltaY = 0 - headerY; UpdateHeaderPosition(0 - headerY); _listView.SetY(_listView.GetY() + deltaY); _listView.LayoutParameters.Height = (int)Math.Ceiling(_listView.LayoutParameters.Height + deltaY); _listView.Invalidate(); return(true); } } if (deltaY < 0) { _overscrolling = false; } } if (listY + deltaY <= 0) { deltaY = 0 - listY; _overscrolling = false; } else if (deltaY > 0) { deltaY *= 0.4f; } Log.Info("OverScrollingIntercept", "{0}", deltaY); _listView.SetY(_listView.GetY() + deltaY); _lastHistory = ev.HistorySize; } else if (ev.Action == MotionEventActions.Up) { _overscrolling = false; _lastHistory = 0; _listView.Animate().Y(_header != null ? _header.Height : 0.0f).SetDuration(250).Start(); } Log.Info("Interceptor", "Overscrolling True"); return(true); } var hitRect = new Rect(); _header.GetHitRect(hitRect); if (hitRect.Contains((int)Math.Ceiling(ev.GetX()), (int)Math.Ceiling(ev.GetY()))) { return(_header.DispatchTouchEvent(ev)); } else { if (_header != null) { var headerY = _header.GetY(); if (ev.Action == MotionEventActions.Move && ev.HistorySize >= 1) { hist = ev.GetHistoricalY(0, 0); deltaY = ev.GetY() - hist; if (deltaY < 0) { if ((int)Math.Ceiling(headerY) == (0 - _header.Height)) { return(_listView.DispatchTouchEvent(ev)); } if (headerY + deltaY >= (0 - _header.Height)) { UpdateHeaderPosition(deltaY); _listView.SetY(_listView.GetY() + deltaY); _listView.LayoutParameters.Height = (int)Math.Ceiling(_listView.LayoutParameters.Height - deltaY); _listView.RequestLayout(); return(true); } else if (headerY + deltaY < (0 - _header.Height)) { deltaY = 0 - (headerY + _header.Height); UpdateHeaderPosition(deltaY); _listView.SetY(_listView.GetY() + deltaY); _listView.LayoutParameters.Height = (int)Math.Ceiling(_listView.LayoutParameters.Height - deltaY); _listView.RequestLayout(); _listView.DispatchTouchEvent(ev); return(true); } } _listView.DispatchTouchEvent(ev); return(true); } else if (ev.Action == MotionEventActions.Up || ev.Action == MotionEventActions.Cancel) { vT.ComputeCurrentVelocity(1); long duration = Math.Abs((long)Math.Ceiling(Math.Abs(_header.Height) / vT.GetYVelocity(0))); if (duration < 750) { _header.Animate() .Y(0 - _header.Height) .SetDuration(duration) .SetInterpolator(new DecelerateInterpolator(1.0f)) .Start(); _listView.Animate() .Y(0) .SetDuration(duration) .SetInterpolator(new DecelerateInterpolator(1.0f)) .Start(); } return(_listView.DispatchTouchEvent(ev)); } } return(_listView.DispatchTouchEvent(ev)); } }
public virtual bool OnTouchEvent(Android.Views.MotionEvent ev) { switch (ev.Action) { case MotionEventActions.Down: { mVelocityTracker = VelocityTracker.Obtain(); if (null != mVelocityTracker) { mVelocityTracker.AddMovement(ev); } else { Log.Info(LOG_TAG, "Velocity tracker is null"); } mLastTouchX = GetActiveX(ev); mLastTouchY = GetActiveY(ev); mIsDragging = false; break; } case MotionEventActions.Move: { float x = GetActiveX(ev); float y = GetActiveY(ev); float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { // Use Pythagoras to see if drag length is larger than // touch slop mIsDragging = FloatMath.Sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { mListener.OnDrag(dx, dy); mLastTouchX = x; mLastTouchY = y; if (null != mVelocityTracker) { mVelocityTracker.AddMovement(ev); } } break; } case MotionEventActions.Cancel: { // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.Recycle(); mVelocityTracker = null; } break; } case MotionEventActions.Up: { if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = GetActiveX(ev); mLastTouchY = GetActiveY(ev); // Compute velocity within the last 1000ms mVelocityTracker.AddMovement(ev); mVelocityTracker.ComputeCurrentVelocity(1000); float vX = mVelocityTracker.GetXVelocity(0), vY = mVelocityTracker .GetYVelocity(0); // If the velocity is greater than minVelocity, call // listener if (Math.Max(Math.Abs(vX), Math.Abs(vY)) >= mMinimumVelocity) { mListener.OnFling(mLastTouchX, mLastTouchY, -vX, -vY); } } } // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.Recycle(); mVelocityTracker = null; } break; } } return(true); }
public override bool OnInterceptTouchEvent(MotionEvent ev) { if (!mEnabled) { return(false); } MotionEventActions action = ev.Action & (MotionEventActions)MotionEventCompat.ActionMask; if (DEBUG) { if (action == MotionEventActions.Down) { Console.WriteLine(TAG + " Received ACTION_DOWN"); } } if (action == MotionEventActions.Cancel || action == MotionEventActions.Up || (action != MotionEventActions.Down && mIsUnableToDrag)) { EndDrag(); return(false); } switch (action) { case MotionEventActions.Move: DetermineDrag(ev); break; case MotionEventActions.Down: int index = MotionEventCompat.GetActionIndex(ev); mActivePointerId = MotionEventCompat.GetPointerId(ev, index); if (mActivePointerId == INVALID_POINTER) { break; } mLastMotionX = mInitialMotionX = MotionEventCompat.GetX(ev, index); mLastMotionY = MotionEventCompat.GetY(ev, index); if (ThisTouchAllowed(ev)) { mIsBeingDragged = false; mIsUnableToDrag = false; if (IsMenuOpen && mViewBehind.MenuTouchInQuickReturn(mContent, mCurItem, ev.GetX() + mScrollX)) { mQuickReturn = true; } } else { mIsUnableToDrag = true; } break; case (MotionEventActions)MotionEventCompat.ActionPointerUp: OnSecondaryPointerUp(ev); break; } if (!mIsBeingDragged) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(ev); } return(mIsBeingDragged || mQuickReturn); }
/// <summary> /// 触发触摸事件 /// </summary> /// <param name="event">事件</param> public void OnTouchEvent(MotionEvent @event) { GestureHelper.OnTouchEvent(@event); VelocityTracker.AddMovement(@event); switch (@event.Action) { case MotionEventActions.Down: { SetStartPosition(@event.GetX(), @event.GetY()); break; } case MotionEventActions.Move: { if (CanScroll) { float rangeX = @event.GetX() - startTouchX; float rangeY = @event.GetY() - startTouchY; int dstX = (int)(startScrollX - rangeX); int dstY = (int)(startScrollY - rangeY); if (dstX < GetMinHorizontallyScroll) { dstX = 0; startTouchX = @event.GetX(); startScrollX = dstX; } else if (dstX > GetMaxHorizontallyScroll) { dstX = GetViewHorizontallyScrollSize(); startTouchX = @event.GetX(); startScrollX = dstX; } if (dstY < GetMinVerticallyScroll) { dstY = 0; startTouchY = @event.GetY(); startScrollY = dstY; } else if (dstY > GetMaxVerticallyScroll) { dstY = GetViewVerticallyScrollSize(); startTouchY = @event.GetY(); startScrollY = dstY; } ViewScrollTo(dstX, dstY); } break; } case MotionEventActions.Up: case MotionEventActions.Cancel: { VelocityTracker.ComputeCurrentVelocity(1000); if (CanScroll) { float xv = VelocityTracker.XVelocity; float yv = VelocityTracker.YVelocity; ViewFling(xv, yv); } break; } } }
public override bool OnTouchEvent(MotionEvent e) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(e); MotionEventActions action = e.Action; float x = e.GetX(); switch (action) { case MotionEventActions.Down: if (!mScroller.IsFinished) { mScroller.AbortAnimation(); } mLastMotionX = x; if (mScroller.IsFinished) { mTouchState = TOUCH_STATE_REST; } else { mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING; } break; case MotionEventActions.Move: int xDiff = (int)Math.Abs(x - mLastMotionX); bool xMoved = xDiff > mTouchSlop; if (xMoved) { mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING; } if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) { int deltaX = (int)(mLastMotionX - x); mLastMotionX = x; int scrollX = this.ScrollX; if (deltaX < 0) { if (scrollX > 0) { ScrollBy(Math.Max(-scrollX, deltaX), 0); } } else if (deltaX > 0) { if (this.ChildCount >= 1) { int avalableToScroll = this.GetChildAt(this.ChildCount - 1).Right - scrollX - Width; if (avalableToScroll > 0) { ScrollBy(Math.Min(avalableToScroll, deltaX), 0); } } } } break; case MotionEventActions.Up: if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) { VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.ComputeCurrentVelocity(VELOCITY_UNIT_PIXELS_PER_SECOND, mMaximumVelocity); int velocityX = (int)velocityTracker.XVelocity; if (velocityX > mDensityAdjustedSnapVelocity && mCurrentScreen > 0) { SnapToScreen(mCurrentScreen - 1); } else if (velocityX < -mDensityAdjustedSnapVelocity && mCurrentScreen < this.ChildCount - 1) { SnapToScreen(mCurrentScreen + 1); } else { SnapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.Recycle(); mVelocityTracker = null; } mTouchState = TOUCH_STATE_REST; } break; case MotionEventActions.Cancel: mTouchState = TOUCH_STATE_REST; break; default: break; } return(true); }
public override bool OnTouchEvent(MotionEvent ev) { if (_velocityTracker == null) { _velocityTracker = VelocityTracker.Obtain(); } _velocityTracker.AddMovement(ev); var action = ev.Action; var x = ev.GetX(); switch (action) { case MotionEventActions.Down: /* * If being flinged and user touches, stop the fling. isFinished will be false if being flinged. */ if (!_scroller.IsFinished) { _scroller.AbortAnimation(); } // Remember where the motion event started _lastMotionX = x; _touchState = _scroller.IsFinished ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; case MotionEventActions.Move: var xDiff = (int)Math.Abs(x - _lastMotionX); var xMoved = xDiff > _touchSlop; if (xMoved) { // Scroll if the user moved far enough along the X axis _touchState = TOUCH_STATE_SCROLLING; } if (_touchState == TOUCH_STATE_SCROLLING) { // Scroll to follow the motion event var deltaX = (int)(_lastMotionX - x); _lastMotionX = x; var scrollX = ScrollX; if (deltaX < 0) { if (scrollX > 0) { ScrollBy(Math.Max(-scrollX, deltaX), 0); } } else if (deltaX > 0) { var availableToScroll = GetChildAt(ChildCount - 1).Right - scrollX - Width; if (availableToScroll > 0) { ScrollBy(Math.Min(availableToScroll, deltaX), 0); } } } break; case MotionEventActions.Up: if (_touchState == TOUCH_STATE_SCROLLING) { var velocityTracker = _velocityTracker; velocityTracker.ComputeCurrentVelocity(1000, _maximumVelocity); var velocityX = (int)velocityTracker.XVelocity; if (velocityX > SNAP_VELOCITY && _currentScreen > 0) { // Fling hard enough to move left SnapToScreen(_currentScreen - 1); } else if (velocityX < -SNAP_VELOCITY && _currentScreen < ChildCount - 1) { // Fling hard enough to move right SnapToScreen(_currentScreen + 1); } else { SnapToDestination(); } if (_velocityTracker != null) { _velocityTracker.Recycle(); _velocityTracker = null; } } _touchState = TOUCH_STATE_REST; break; case MotionEventActions.Cancel: _touchState = TOUCH_STATE_REST; break; } return(true); }
public override bool OnTouchEvent(MotionEvent ev) { if (_mVelocityTracker == null) { _mVelocityTracker = VelocityTracker.Obtain(); } _mVelocityTracker.AddMovement(ev); MotionEventActions action = ev.Action; float x = ev.GetX(); switch (action) { case MotionEventActions.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_HORIZONTAL_SCROLLING; break; case MotionEventActions.Move: var xDiff = (int)Math.Abs(x - _mLastMotionX); bool xMoved = xDiff > _mTouchSlop; if (xMoved) { // Scroll if the user moved far enough along the X axis _mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING; } if (_mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) { // Scroll to follow the motion event var deltaX = (int)(_mLastMotionX - x); _mLastMotionX = x; int scrollX = ScrollX; if (deltaX < 0) { if (scrollX > 0) { ScrollBy(Math.Max(-scrollX, deltaX), 0); } } else if (deltaX > 0) { int availableToScroll = GetChildAt(ChildCount - 1).Right - scrollX - Width; if (availableToScroll > 0) { ScrollBy(Math.Min(availableToScroll, deltaX), 0); } } } break; case MotionEventActions.Up: if (_mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) { VelocityTracker velocityTracker = _mVelocityTracker; velocityTracker.ComputeCurrentVelocity(VELOCITY_UNIT_PIXELS_PER_SECOND, _mMaximumVelocity); var velocityX = (int)velocityTracker.XVelocity; if (velocityX > mDensityAdjustedSnapVelocity && _mCurrentScreen > 0) { // Fling hard enough to move left SnapToScreen(_mCurrentScreen - 1); } else if (velocityX < -mDensityAdjustedSnapVelocity && _mCurrentScreen < ChildCount - 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 MotionEventActions.Cancel: _mTouchState = TOUCH_STATE_REST; break; } return(true); }
public bool OnTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.Width; } switch (motionEvent.ActionMasked) { case MotionEventActions.Down: if (mPaused) { return(false); } // TODO: ensure this is a finger, and set a flag // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mListView.ChildCount; int[] listViewCoords = new int[2]; mListView.GetLocationOnScreen(listViewCoords); int x = (int)motionEvent.RawX - listViewCoords [0]; int y = (int)motionEvent.RawY - listViewCoords [1]; View child; for (int i = 0; i < childCount; i++) { child = mListView.GetChildAt(i); child.GetHitRect(rect); if (rect.Contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.RawX; mDownY = motionEvent.RawY; mDownPosition = mListView.GetPositionForView(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.Obtain(); mVelocityTracker.AddMovement(motionEvent); } else { mDownView = null; } } return(false); case MotionEventActions.Cancel: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel mDownView.Animate() .TranslationX(0) .Alpha(1) .SetDuration(mAnimationTime) .SetListener(null); } mVelocityTracker.Recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.InvalidPosition; mSwiping = false; break; } case MotionEventActions.Up: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.RawX - mDownX; mVelocityTracker.AddMovement(motionEvent); mVelocityTracker.ComputeCurrentVelocity(1000); float velocityX = mVelocityTracker.XVelocity; float absVelocityX = Math.Abs(velocityX); float absVelocityY = Math.Abs(mVelocityTracker.YVelocity); bool dismiss = false; bool dismissRight = false; if (Math.Abs(deltaX) > mViewWidth / 2) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.XVelocity > 0; } if (dismiss && mDownPosition != ListView.InvalidPosition) { // dismiss View downView = mDownView; // mDownView gets null'd before animation ends int downPosition = mDownPosition; ++mDismissAnimationRefCount; var anim = mDownView.Animate() .TranslationX(dismissRight ? mViewWidth : -mViewWidth) .Alpha(0) .SetDuration(mAnimationTime) .SetListener(new DownAnimatorListenerAdapter(this, downView, downPosition)); } else { // cancel mDownView.Animate() .TranslationX(0) .Alpha(1) .SetDuration(mAnimationTime) .SetListener(null); } mVelocityTracker.Recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.InvalidPosition; mSwiping = false; break; } case MotionEventActions.Move: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.AddMovement(motionEvent); float deltaX = motionEvent.RawX - mDownX; float deltaY = motionEvent.RawY - mDownY; if (Math.Abs(deltaX) > mSlop && Math.Abs(deltaY) < Math.Abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mListView.RequestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.Obtain(motionEvent); cancelEvent.Action = (Android.Views.MotionEventActions)((int)MotionEventActions.Cancel | ((int)motionEvent.ActionIndex << (int)MotionEventActions.PointerIndexShift)); mListView.OnTouchEvent(cancelEvent); cancelEvent.Recycle(); } if (mSwiping) { mDownView.TranslationX = deltaX - mSwipingSlop; mDownView.Alpha = Math.Max(0f, Math.Min(1f, 1f - 2f * Math.Abs(deltaX) / mViewWidth)); return(true); } break; } } return(false); }
public override bool DispatchTouchEvent(MotionEvent e) { var index = e.ActionIndex; var action = e.ActionMasked; var pointerId = e.GetPointerId(index); switch (action & MotionEventActions.Mask) { case MotionEventActions.PointerDown: MultiTouchGesture?.Invoke(this, EventArgs.Empty); break; case MotionEventActions.Up: if ((DateTime.Now - _downTime).TotalMilliseconds < 200) { SingleTapGesture?.Invoke(this, EventArgs.Empty); } break; } switch (action) { case MotionEventActions.Down: _downTime = DateTime.Now; if (_velocityTracker == null) { _velocityTracker = VelocityTracker.Obtain(); } else { // Reset the velocity tracker back to its initial state. _velocityTracker.Clear(); } if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.AddMovement(e); break; case MotionEventActions.Move: if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.AddMovement(e); _velocityTracker.ComputeCurrentVelocity(Sensitivity); TryExportVelocity(_velocityTracker.GetXVelocity(pointerId), _velocityTracker.GetYVelocity(pointerId)); break; case MotionEventActions.Up: case MotionEventActions.Cancel: if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.Recycle(); _velocityTracker = null; break; } return(true); }
public override bool OnTouchEvent(MotionEvent ev) { if (ev.Action == MotionEventActions.Down && ev.EdgeFlags != 0) { // Don't handle edge touches immediately -- they may actually belong to one of our // descendants. return(false); } if (!CanScroll) { return(false); } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(ev); MotionEventActions action = ev.Action; float y = ev.GetY(); float x = ev.GetX(); switch (action) { case MotionEventActions.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 mLastMotionY = y; mLastMotionX = x; break; case MotionEventActions.Move: // Scroll to follow the motion event int deltaX = (int)(mLastMotionX - x); int deltaY = (int)(mLastMotionY - y); mLastMotionX = x; mLastMotionY = y; if (deltaX < 0) { if (ScrollX < 0) { deltaX = 0; } } else if (deltaX > 0) { int rightEdge = Width - PaddingRight; int availableToScroll = GetChildAt(0).Right - ScrollX - rightEdge; if (availableToScroll > 0) { deltaX = System.Math.Min(availableToScroll, deltaX); } else { deltaX = 0; } } if (deltaY < 0) { if (ScrollY < 0) { deltaY = 0; } } else if (deltaY > 0) { int bottomEdge = Height - PaddingBottom; int availableToScroll = GetChildAt(0).Bottom - ScrollY - bottomEdge; if (availableToScroll > 0) { deltaY = System.Math.Min(availableToScroll, deltaY); } else { deltaY = 0; } } if (deltaY != 0 || deltaX != 0) { ScrollBy(deltaX, deltaY); } break; case MotionEventActions.Up: VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity); int initialXVelocity = (int)velocityTracker.XVelocity; int initialYVelocity = (int)velocityTracker.YVelocity; if ((System.Math.Abs(initialXVelocity) + System.Math.Abs(initialYVelocity) > mMinimumVelocity) && ChildCount > 0) { Fling(-initialXVelocity, -initialYVelocity); } if (mVelocityTracker != null) { mVelocityTracker.Recycle(); mVelocityTracker = null; } break; } return(true); }
public override bool OnInterceptTouchEvent(MotionEvent ev) { /* * This method JUST determines whether we want to intercept the motion. * If we return true, onTouchEvent will be called and we do the actual * scrolling there. */ // Begin tracking velocity even before we have intercepted touch events. if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(ev); /* * Shortcut the most recurring case: the user is in the dragging * state and he is moving his finger. We want to intercept this * motion. */ var action = ev.Action; if (mIsVerbose) { Log.Verbose(TAG, "onInterceptTouchEvent: " + (ev.Action & MotionEventActions.Mask)); } if (((action & MotionEventActions.Mask) == MotionEventActions.Move) && (mTouchState == TOUCH_STATE_SCROLLING)) { if (mIsVerbose) { Log.Verbose(TAG, "Intercepting touch events"); } return(true); } switch (action & MotionEventActions.Mask) { case MotionEventActions.Move: { if (mLocked) { // we're locked on the current screen, don't allow moving break; } /* * Locally do absolute value. mDownMotionX is set to the y value * of the down event. */ int pointerIndex = MotionEventCompat.FindPointerIndex(ev, mActivePointerId); float x = MotionEventCompat.GetX(ev, pointerIndex); float y = MotionEventCompat.GetY(ev, pointerIndex); int xDiff = (int)Java.Lang.Math.Abs(x - mDownMotionX); int yDiff = (int)Java.Lang.Math.Abs(y - mDownMotionY); bool xPaged = xDiff > mPagingTouchSlop; bool xMoved = xDiff > mTouchSlop; bool yMoved = yDiff > mTouchSlop; if (xMoved || yMoved) { if (xPaged) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; } // Either way, cancel any pending longpress if (mAllowLongPress) { mAllowLongPress = false; // Try canceling the long press. It could also have been scheduled // by a distant descendant, so use the mAllowLongPress flag to block // everything View currentScreen = GetScreenAt(mCurrentScreen); if (currentScreen != null) { currentScreen.CancelLongPress(); } } } break; } case MotionEventActions.Down: { float x = ev.GetX(); float y = ev.GetY(); // Remember location of down touch mDownMotionX = x; mDownMotionY = y; mDownScrollX = ScrollX; mActivePointerId = MotionEventCompat.GetPointerId(ev, 0); mAllowLongPress = true; /* * If being flinged and user touches the screen, initiate drag; * otherwise don't. mScroller.isFinished should be false when * being flinged. */ mTouchState = mScroller.IsFinished ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; } case MotionEventActions.Cancel: case MotionEventActions.Up: // Release the drag mTouchState = TOUCH_STATE_REST; mAllowLongPress = false; mActivePointerId = INVALID_POINTER; if (mVelocityTracker == null) { mVelocityTracker.Recycle(); mVelocityTracker = null; } break; case MotionEventActions.PointerUp: OnSecondaryPointerUp(ev); break; } /* * The only time we want to intercept motion events is if we are in the * drag mode. */ bool intercept = mTouchState != TOUCH_STATE_REST; if (mIsVerbose) { Log.Verbose(TAG, "Intercepting touch events: " + intercept); } return(intercept); }
public override bool OnTouchEvent(MotionEvent e) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.Obtain(); } mVelocityTracker.AddMovement(e); MotionEventActions action = e.Action; if (action == MotionEventActions.Down) { x = e.GetX(); if (IsSlided()) { dispatched = DispatchTouchEventToView(GetChildAt(0), e); } else { dispatched = DispatchTouchEventToView(GetChildAt(1), e); } } else if (action == MotionEventActions.Move) { if (dispatched) { if (IsSlided()) { DispatchTouchEventToView(GetChildAt(0), e); } else { DispatchTouchEventToView(GetChildAt(1), e); } } else { float dx = e.GetX() - x; View view = this.GetChildAt(1); int left = (int)(view.Left + dx); if (left >= 0) { view.Layout(left, view.Top, view.Width + left, view.Top + view.Height); } } x = e.GetX(); } else if (action == MotionEventActions.Cancel || action == MotionEventActions.Up) { if (dispatched) { if (IsSlided()) { DispatchTouchEventToView(GetChildAt(0), e); } else { DispatchTouchEventToView(GetChildAt(1), e); } } else { mVelocityTracker.ComputeCurrentVelocity(1000); int velocityX = (int)mVelocityTracker.GetXVelocity(0); if (velocityX > VELOCITY_X_SPEED) { SetSlided(true); } else if (velocityX < -VELOCITY_X_SPEED) { SetSlided(false); } else { View view = GetChildAt(1); if (view.Left >= view.Width / 2) { SetSlided(true); } else { SetSlided(false); } } if (mVelocityTracker != null) { try { mVelocityTracker.Recycle(); } catch { } } } } else if (!IsSlided()) { DispatchTouchEventToView(GetChildAt(1), e); } return(true); }