コード例 #1
0
ファイル: TrackingView.cs プロジェクト: taori/PCRemote2
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
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);
        }