Code example for MotionEvent

Methods: getActionIndexgetActionMaskedgetRawXobtainoffsetLocation

0
    } 
  
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) {
        // offset because the view is translated during swipe 
        motionEvent.offsetLocation(mTranslationX, 0);
  
        if (mViewWidth < 2) {
            mViewWidth = mView.getWidth();
        } 
  
        switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: {
                // TODO: ensure this is a finger, and set a flag 
                mDownX = motionEvent.getRawX();
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
                view.onTouchEvent(motionEvent);
                return false; 
            } 
  
            case MotionEvent.ACTION_UP: {
                if (mVelocityTracker == null) {
                    break; 
                } 
  
                float deltaX = motionEvent.getRawX() - mDownX;
                mVelocityTracker.addMovement(motionEvent);
                mVelocityTracker.computeCurrentVelocity(1000);
                float velocityX = Math.abs(mVelocityTracker.getXVelocity());
                float velocityY = Math.abs(mVelocityTracker.getYVelocity());
                boolean dismiss = false;
                boolean dismissRight = false;
                if (Math.abs(deltaX) > mViewWidth / 2) {
                    dismiss = true;
                    dismissRight = deltaX > 0;
                } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
                        && velocityY < velocityX) {
                    dismiss = true;
                    dismissRight = mVelocityTracker.getXVelocity() > 0;
                } 
                if (dismiss) {
                    // dismiss 
                    mView.animate()
                            .translationX(dismissRight ? mViewWidth : -mViewWidth)
                            .alpha(0)
                            .setDuration(mAnimationTime)
                            .setListener(new AnimatorListenerAdapter() {
                                @Override 
                                public void onAnimationEnd(Animator animation) {
                                    performDismiss(); 
                                } 
                            }); 
                } else { 
                    // cancel 
                    mView.animate()
                            .translationX(0)
                            .alpha(1)
                            .setDuration(mAnimationTime)
                            .setListener(null);
                } 
                mVelocityTracker = null;
                mTranslationX = 0;
                mDownX = 0;
                mSwiping = false;
                break; 
            } 
  
            case MotionEvent.ACTION_MOVE: {
                if (mVelocityTracker == null) {
                    break; 
                } 
  
                mVelocityTracker.addMovement(motionEvent);
                float deltaX = motionEvent.getRawX() - mDownX;
                if (Math.abs(deltaX) > mSlop) {
                    mSwiping = true;
                    mView.getParent().requestDisallowInterceptTouchEvent(true);
  
                    // Cancel listview's touch 
                    MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
                        (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    mView.onTouchEvent(cancelEvent);
                } 
  
                if (mSwiping) {
                    mTranslationX = deltaX;