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);
        }
Example #2
0
        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));
        }
        private void Up(MotionEvent motionEvent)
        {
            if (_velocityTracker == null)
            {
                return;
            }

            _pauseTimer(false);
            var deltaX = motionEvent.RawX - _downX;

            _velocityTracker.AddMovement(motionEvent);
            _velocityTracker.ComputeCurrentVelocity(1000);
            var velocityX    = _velocityTracker.XVelocity;
            var absVelocityX = Math.Abs(velocityX);
            var absVelocityY = Math.Abs(_velocityTracker.YVelocity);
            var dismiss      = false;
            var dismissRight = false;

            if (Math.Abs(deltaX) > _viewWidth / 2 && _swiping)
            {
                dismiss      = true;
                dismissRight = deltaX > 0;
            }
            else if (_minFlingVelocity <= absVelocityX && absVelocityX <= _maxFlingVelocity &&
                     absVelocityY < absVelocityX &&
                     absVelocityY < absVelocityX && _swiping)
            {
                // dismiss only if flinging in the same direction as dragging
                dismiss      = (velocityX < 0) == (deltaX < 0);
                dismissRight = _velocityTracker.XVelocity > 0;
            }
            if (dismiss)
            {
                // dismiss

                _view.Animate()
                .TranslationX(dismissRight ? _viewWidth : -_viewWidth)
                .Alpha(0)
                .SetDuration(_animationTime)
                .SetListener(this);
            }
            else if (_swiping)
            {
                // cancel
                _view.Animate()
                .TranslationX(0)
                .Alpha(1)
                .SetDuration(_animationTime)
                .SetListener(null);
            }
            if (_velocityTracker != null)
            {
                _velocityTracker.Recycle();
                _velocityTracker = null;
            }
            _translationX = 0;
            _downX        = 0;
            _downY        = 0;
            _swiping      = false;
        }
Example #4
0
 private void ListViewOnOnOverscrolled(object sender, OverscrollListenerListViewEventArgs overscrollListenerListViewEventArgs)
 {
     if (overscrollListenerListViewEventArgs.DeltaY < 0)
     {
         _overscrolling = true;
         vT.ComputeCurrentVelocity(1);
         if (_header != null && (int)Math.Ceiling(_header.GetY()) != 0 && _historyEvent.Action == MotionEventActions.Up || _historyEvent.Action == MotionEventActions.Cancel)
         {
             long duration = (long)Math.Ceiling(Math.Abs(_header.Height) / vT.GetYVelocity(0));
             _header.Animate().Y(0).SetDuration(duration).SetInterpolator(new DecelerateInterpolator(1.0f)).Start();
             _listView.Animate().Y(_header.Height).SetDuration(duration).SetInterpolator(new DecelerateInterpolator(1.0f)).Start();
         }
     }
     else
     {
         _overscrolling = false;
     }
 }
Example #5
0
        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;
                }
            };
        }
Example #6
0
        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 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 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 (mIsVerbose)
            {
                Log.Verbose(TAG, "onTouchEvent: " + ((int)ev.Action & MotionEventCompat.ActionMask));
            }

            if (mVelocityTracker == null)
            {
                mVelocityTracker = VelocityTracker.Obtain();
            }
            mVelocityTracker.AddMovement(ev);

            var action = ev.Action;

            switch ((int)action & MotionEventCompat.ActionMask)
            {
            case (int)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
                mDownMotionX     = ev.GetX();
                mDownMotionY     = ev.GetY();
                mDownScrollX     = ScrollX;
                mActivePointerId = MotionEventCompat.GetPointerId(ev, 0);
                break;

            case (int)MotionEventActions.Move:
                if (mIsVerbose)
                {
                    Log.Verbose(TAG, "mTouchState=" + mTouchState);
                }

                if (mTouchState == TOUCH_STATE_SCROLLING)
                {
                    // Scroll to follow the motion event
                    int   pointerIndex = MotionEventCompat.FindPointerIndex(ev, mActivePointerId);
                    float x            = MotionEventCompat.GetX(ev, pointerIndex);

                    View lastChild  = GetChildAt(ChildCount - 1);
                    int  maxScrollX = lastChild.Right - Width;
                    ScrollTo(Math.Max(0, Math.Min(maxScrollX,
                                                  (int)(mDownScrollX + mDownMotionX - x
                                                        ))), 0);
                    if (mOnScrollListener != null)
                    {
                        mOnScrollListener.OnScroll(GetCurrentScreenFraction());
                    }
                }
                else if (mTouchState == TOUCH_STATE_REST)
                {
                    if (mLocked)
                    {
                        // we're locked on the current screen, don't allow moving
                        break;
                    }

                    /*
                     * Locally do absolute value. mLastMotionX 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)Math.Abs(x - mDownMotionX);
                    int   yDiff        = (int)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 (int)MotionEventActions.Up:
                if (mTouchState == TOUCH_STATE_SCROLLING)
                {
                    int             activePointerId = mActivePointerId;
                    int             pointerIndex    = MotionEventCompat.FindPointerIndex(ev, activePointerId);
                    float           x = MotionEventCompat.GetX(ev, pointerIndex);
                    VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity);
                    //TODO(minsdk8): int velocityX = (int) MotionEventUtils.getXVelocity(velocityTracker, activePointerId);
                    int  velocityX = (int)velocityTracker.XVelocity;
                    bool isFling   = Math.Abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;

                    float scrolledPos = GetCurrentScreenFraction();
                    int   whichScreen = Math.Round(scrolledPos);

                    if (isFling && mIsVerbose)
                    {
                        Log.Verbose(TAG, "isFling, whichScreen=" + whichScreen
                                    + " scrolledPos=" + scrolledPos
                                    + " mCurrentScreen=" + mCurrentScreen
                                    + " velocityX=" + velocityX);
                    }
                    if (isFling && velocityX > SNAP_VELOCITY && mCurrentScreen > 0)
                    {
                        // Fling hard enough to move left
                        // Don't fling across more than one screen at a time.
                        int bound = scrolledPos <= whichScreen ?
                                    mCurrentScreen - 1 : mCurrentScreen;
                        SnapToScreen(Math.Min(whichScreen, bound));
                    }
                    else if (isFling && velocityX < -SNAP_VELOCITY &&
                             mCurrentScreen < ChildCount - 1)
                    {
                        // Fling hard enough to move right
                        // Don't fling across more than one screen at a time.
                        int bound = scrolledPos >= whichScreen ?
                                    mCurrentScreen + 1 : mCurrentScreen;
                        SnapToScreen(Math.Max(whichScreen, bound));
                    }
                    else
                    {
                        SnapToDestination();
                    }
                }
                else
                {
                    PerformClick();
                }
                mTouchState      = TOUCH_STATE_REST;
                mActivePointerId = INVALID_POINTER;
                // Can't do this -> // Intentially fall through to cancel
                mTouchState      = TOUCH_STATE_REST;
                mActivePointerId = INVALID_POINTER;
                if (mVelocityTracker != null)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;

            case (int)MotionEventActions.Cancel:
                mTouchState      = TOUCH_STATE_REST;
                mActivePointerId = INVALID_POINTER;
                if (mVelocityTracker != null)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;


            case (int)MotionEventCompat.ActionPointerUp:
                OnSecondaryPointerUp(ev);
                break;
            }

            return(true);
        }
Example #10
0
        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);
        }
Example #11
0
        public override bool OnTouchEvent(MotionEvent @event)
        {
            if (@event.Action == MotionEventActions.Up)
            {
                PerformClick();
            }
            HandleTouchEvent(@event);
            var resetTouch = false;
            var vertical   = false;
            var horizontal = false;

            switch (gestureHelper.Gesture)
            {
            case GestureHelper.GESTURE_RIGHT:
            case GestureHelper.GESTURE_LEFT:
                if (LayerScrollY != 0)
                {
                    vertical = true;
                }
                else
                {
                    horizontal = true;
                }
                break;

            case GestureHelper.GESTURE_DOWN:
            case GestureHelper.GESTURE_UP:
                if (LayerScrollX != 0)
                {
                    horizontal = true;
                }
                else
                {
                    vertical = true;
                }
                break;

            default:
                if (@event.Action == MotionEventActions.Up || @event.Action == MotionEventActions.Cancel)
                {
                    ReleaseTouch();
                }
                break;
            }
            if (vertical)
            {
                float rangeY = @event.GetY() - touchStartY;
                int   dy     = (int)(touchScrollStartY - rangeY);
                if (!CanVerticalScrollTo(dy))
                {
                    resetTouch = true;
                    if (dy < 0)
                    {
                        dy = VerticalLayerScrollMin;
                    }
                    else if (dy > 0)
                    {
                        dy = VerticalLayerScrollMax;
                    }
                }
                if (touchScrollable)
                {
                    LayerScrollTo(LayerScrollX, dy);
                }
                if (@event.Action == MotionEventActions.Up || @event.Action == MotionEventActions.Cancel)
                {
                    velocityTracker.ComputeCurrentVelocity(1000);
                    if (LayerScrollY < 0)
                    {
                        if (velocityTracker.YVelocity > 0)
                        {
                            if (CanOpenTop(velocityTracker.XVelocity, velocityTracker.YVelocity))
                            {
                                SmoothLayerScrollTo(LayerScrollX, VerticalLayerScrollMin);
                            }
                            else
                            {
                                SmoothLayerScrollTo(0, 0);
                            }
                        }
                        else
                        {
                            SmoothLayerScrollTo(0, 0);
                        }
                    }
                    else if (LayerScrollY > 0)
                    {
                        if (velocityTracker.YVelocity < 0)
                        {
                            if (CanOpenBottom(velocityTracker.XVelocity, velocityTracker.YVelocity))
                            {
                                SmoothLayerScrollTo(LayerScrollX, VerticalLayerScrollMax);
                            }
                            else
                            {
                                SmoothLayerScrollTo(0, 0);
                            }
                        }
                        else
                        {
                            SmoothLayerScrollTo(0, 0);
                        }
                    }
                }
            }
            else if (horizontal)
            {
                float rangeX = @event.GetX() - touchStartX;
                int   dx     = (int)(touchScrollStartX - rangeX);
                if (!CanHorizontalScrollTo(dx))
                {
                    resetTouch = true;
                    if (dx < 0)
                    {
                        dx = HorizontalLayerScrollMin;
                    }
                    else if (dx > 0)
                    {
                        dx = HorizontalLayerScrollMax;
                    }
                }
                if (touchScrollable)
                {
                    LayerScrollTo(dx, ScrollY);
                }
                if (@event.Action == MotionEventActions.Up ||
                    @event.Action == MotionEventActions.Cancel)
                {
                    velocityTracker.ComputeCurrentVelocity(1000);
                    if (LayerScrollX < 0)
                    {
                        if (velocityTracker.XVelocity > 0)
                        {
                            if (CanOpenLeft(velocityTracker.XVelocity, velocityTracker.YVelocity))
                            {
                                SmoothLayerScrollTo(HorizontalLayerScrollMin, LayerScrollY);
                            }
                            else
                            {
                                SmoothLayerScrollTo(0, 0);
                            }
                        }
                        else
                        {
                            SmoothLayerScrollTo(0, 0);
                        }
                    }
                    else if (LayerScrollX > 0)
                    {
                        if (velocityTracker.XVelocity < 0)
                        {
                            if (CanOpenRight(velocityTracker.XVelocity, velocityTracker.YVelocity))
                            {
                                SmoothLayerScrollTo(HorizontalLayerScrollMax, LayerScrollY);
                            }
                            else
                            {
                                SmoothLayerScrollTo(0, 0);
                            }
                        }
                        else
                        {
                            SmoothLayerScrollTo(0, 0);
                        }
                    }
                }
            }
            if (resetTouch)
            {
                ResetTouchStart(@event.GetX(), @event.GetY());
            }
            return(true);
        }
Example #12
0
        /// <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;
            }
            }
        }
Example #13
0
        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);
        }
Example #14
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (mLocked)
            {
                return(true);
            }

            if (mTracking)
            {
                mVelocityTracker.AddMovement(e);
                MotionEventActions action = e.Action;
                switch (action)
                {
                case MotionEventActions.Move:
                    moveHandle((int)(mVertical ? e.GetY() : e.GetX()) - mTouchDelta);
                    break;

                case MotionEventActions.Up:
                case MotionEventActions.Cancel: {
                    VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.ComputeCurrentVelocity(mVelocityUnits);

                    float   yVelocity = velocityTracker.YVelocity;
                    float   xVelocity = velocityTracker.XVelocity;
                    Boolean negative;

                    Boolean vertical = mVertical;
                    if (vertical)
                    {
                        negative = yVelocity < 0;
                        if (xVelocity < 0)
                        {
                            xVelocity = -xVelocity;
                        }
                        // fix by Maciej Ciemięga.
                        if ((!mInvert && xVelocity > mMaximumMinorVelocity) || (mInvert && xVelocity < mMaximumMinorVelocity))
                        {
                            xVelocity = mMaximumMinorVelocity;
                        }
                    }
                    else
                    {
                        negative = xVelocity < 0;
                        if (yVelocity < 0)
                        {
                            yVelocity = -yVelocity;
                        }
                        // fix by Maciej Ciemięga.
                        if ((!mInvert && yVelocity > mMaximumMinorVelocity) || (mInvert && yVelocity < mMaximumMinorVelocity))
                        {
                            yVelocity = mMaximumMinorVelocity;
                        }
                    }

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

                    int handleTop    = mHandle.Top;
                    int handleLeft   = mHandle.Left;
                    int handleBottom = mHandle.Bottom;
                    int handleRight  = mHandle.Right;

                    if (Math.Abs(velocity) < mMaximumTapVelocity)
                    {
                        Boolean c1;
                        Boolean c2;
                        Boolean c3;
                        Boolean c4;

                        if (mInvert)
                        {
                            c1 = (mExpanded && (Bottom - handleBottom) < mTapThreshold + mBottomOffset);
                            c2 = (!mExpanded && handleTop < mTopOffset + mHandleHeight - mTapThreshold);
                            c3 = (mExpanded && (Right - handleRight) < mTapThreshold + mBottomOffset);
                            c4 = (!mExpanded && handleLeft > mTopOffset + mHandleWidth + mTapThreshold);
                        }
                        else
                        {
                            c1 = (mExpanded && handleTop < mTapThreshold + mTopOffset);
                            c2 = (!mExpanded && handleTop > mBottomOffset + Bottom - Top - mHandleHeight - mTapThreshold);
                            c3 = (mExpanded && handleLeft < mTapThreshold + mTopOffset);
                            c4 = (!mExpanded && handleLeft > mBottomOffset + Right - Left - mHandleWidth - mTapThreshold);
                        }

                        if (vertical ? c1 || c2 : c3 || c4)
                        {
                            if (mAllowSingleTap)
                            {
                                PlaySoundEffect(SoundEffects.Click);

                                if (mExpanded)
                                {
                                    animateClose(vertical ? handleTop : handleLeft);
                                }
                                else
                                {
                                    animateOpen(vertical ? handleTop : handleLeft);
                                }
                            }
                            else
                            {
                                performFling(vertical ? handleTop : handleLeft, velocity, false);
                            }
                        }
                        else
                        {
                            performFling(vertical ? handleTop : handleLeft, velocity, false);
                        }
                    }
                    else
                    {
                        performFling(vertical ? handleTop : handleLeft, velocity, false);
                    }
                }
                break;
                }
            }

            return(mTracking || mAnimating || base.OnTouchEvent(e));
        }
Example #15
0
        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);
        }
Example #16
0
        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);
        }
Example #17
0
        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);
        }