Example usage for android.view MotionEvent ACTION_CANCEL

List of usage examples for android.view MotionEvent ACTION_CANCEL

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_CANCEL.

Prototype

int ACTION_CANCEL

To view the source code for android.view MotionEvent ACTION_CANCEL.

Click Source Link

Document

Constant for #getActionMasked : The current gesture has been aborted.

Usage

From source file:com.dpg.kodimote.view.SwipeableRecyclerViewTouchListener.java

private boolean handleTouchEvent(MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }/*from  w w w  . j  a va  2  s .co m*/

    Log.d("Swipe", motionEvent.getActionMasked() + "");
    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            break;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null && mAnimatingPosition != mRecyclerView.getChildLayoutPosition(mDownView)) {
            mAlpha = ViewCompat.getAlpha(mDownView);
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView);
            mSwipingLeft = mSwipeListener.canSwipeLeft(mDownPosition);
            mSwipingRight = mSwipeListener.canSwipeRight(mDownPosition);
            if (mSwipingLeft || mSwipingRight) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        mFinalDelta = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(mFinalDelta) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = mFinalDelta > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (mFinalDelta < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mAnimatingPosition = mDownPosition;
            ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListener() {
                        @Override
                        public void onAnimationStart(View view) {
                            // Do nothing.
                        }

                        @Override
                        public void onAnimationEnd(View view) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                performDismiss(downView, downPosition);
                            }
                        }

                        @Override
                        public void onAnimationCancel(View view) {
                            // Do nothing.
                        }
                    });
        } else {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        }

        if (deltaX < 0 && !mSwipingLeft)
            mSwiping = false;
        if (deltaX > 0 && !mSwipingRight)
            mSwiping = false;

        if (mSwiping) {
            ViewCompat.setTranslationX(mDownView, deltaX - mSwipingSlop);
            ViewCompat.setAlpha(mDownView,
                    Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth))));
            return true;
        }
        break;
    }
    }

    return false;
}

From source file:com.baoyz.dribble.widget.SwipeHoverLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean flag = mGestureDetector.onTouchEvent(ev);
    int actionMasked = MotionEventCompat.getActionMasked(ev);
    switch (actionMasked) {
    case MotionEvent.ACTION_CANCEL:
        startSmoothAnimation(mHoverState);
        mDistanceShow = false;//from  w  w w  .  j  av a2 s.c o m
        mFlingShow = false;
        break;
    case MotionEvent.ACTION_UP:
        if (mDistanceShow || mFlingShow) {
            startSmoothAnimation(~mHoverState);
        } else {
            startSmoothAnimation(mHoverState);
        }
        mDistanceShow = false;
        mFlingShow = false;
        break;
    }
    return flag;
}

From source file:com.app.gongza.libs.view.scrollablelayout.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX = (int) Math.abs(currentX - mDownX);
    int shiftY = (int) Math.abs(currentY - mDownY);
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDisallowIntercept = false;//from  www. j av a 2s  . c o m
        needCheckUpdown = true;
        updown = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastY = currentY;
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        if (needCheckUpdown) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                needCheckUpdown = false;
                updown = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                needCheckUpdown = false;
                updown = true;
            }
        }

        if (updown && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (updown && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            boolean dislowChild = false;
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if ((mDirection == DIRECTION.UP && isSticked())
                        || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
                    dislowChild = true;
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    invalidate();
                }
            }
            if (!dislowChild && (isClickHead || !isSticked())) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dispathResult = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dispathResult;
            }
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.chengmeng.tools.views.slidepage.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX = (int) Math.abs(currentX - mDownX);
    int shiftY = (int) Math.abs(currentY - mDownY);
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        is_touching = 0;//from   w w  w  .  j  a  v  a 2s.  c  o m
        mDisallowIntercept = false;
        needCheckUpdown = true;
        updown = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastY = currentY;
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        is_touching++;
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        if (needCheckUpdown) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                needCheckUpdown = false;
                updown = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                needCheckUpdown = false;
                updown = true;
            }
        }
        if (updown && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (updown && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            boolean dislowChild = false;
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if ((mDirection == DIRECTION.UP && isSticked())
                        || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
                    dislowChild = true;
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    invalidate();
                }
            }
            if (!dislowChild && (isClickHead || !isSticked())) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dispathResult = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dispathResult;
            }
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.android.hiparker.lierda_light.view.viewpagerindicator.LinePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   ww w  . ja  v  a2s . c  om*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }

        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:android.support.design.widget.SheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//from  w  w  w. j  a va  2  s.c  o  m
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        mInitialY = (int) event.getY();
        int initialX = (int) event.getX();
        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }
        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.DayPickerViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!mCanPickRange) {
        return super.onInterceptTouchEvent(ev);
    }/*from www. j a v a2  s . c  o  m*/

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: DOWN");
        }

        mInitialDownX = ev.getX();
        mInitialDownY = ev.getY();

        if (mCheckForLongPress == null) {
            mCheckForLongPress = new CheckForLongPress();
        }

        postDelayed(mCheckForLongPress, ViewConfiguration.getLongPressTimeout());
    } else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: (UP || CANCEL)");
        }

        if (mCheckForLongPress != null) {
            removeCallbacks(mCheckForLongPress);
        }

        mIsLongPressed = false;
        mInitialDownX = -1;
        mInitialDownY = -1;
    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: MOVE");
        }

        if (!isStillALongPress((int) ev.getX(), (int) ev.getY())) {
            if (Config.DEBUG) {
                Log.i(TAG, "OITE: MOVED TOO MUCH, CANCELLING CheckForLongPress Runnable");
            }

            if (mCheckForLongPress != null) {
                removeCallbacks(mCheckForLongPress);
            }
        }
    }

    return mIsLongPressed || super.onInterceptTouchEvent(ev);
}

From source file:cn.bingoogolapple.refreshlayout.BGAStickyNavLayout.java

private boolean resetDispatchTouchEvent(MotionEvent ev) {
    MotionEvent newEvent = MotionEvent.obtain(ev);

    ev.setAction(MotionEvent.ACTION_CANCEL);
    dispatchTouchEvent(ev);//  w  w  w. ja  va2 s  . c  om

    newEvent.setAction(MotionEvent.ACTION_DOWN);
    return dispatchTouchEvent(newEvent);
}

From source file:com.audionote.widget.SlideSwitch.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (slideable == false)
        return super.onTouchEvent(event);
    int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        eventStartX = (int) event.getRawX();
        break;//  w  w  w  .j av a  2s .c om
    case MotionEvent.ACTION_MOVE:
        eventLastX = (int) event.getRawX();
        diffX = eventLastX - eventStartX;
        int tempX = diffX + frontRect_left_begin;
        tempX = (tempX > max_left ? max_left : tempX);
        tempX = (tempX < min_left ? min_left : tempX);
        if (tempX >= min_left && tempX <= max_left) {
            frontRect_left = tempX;
            alpha = (int) (255 * (float) tempX / (float) max_left);
            invalidateView();
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        int wholeX = (int) (event.getRawX() - eventStartX);
        frontRect_left_begin = frontRect_left;
        boolean toRight;
        toRight = (frontRect_left_begin > max_left / 2 ? true : false);
        if (Math.abs(wholeX) < 3) {
            toRight = !toRight;
        }
        moveToDest(toRight);
        break;
    default:
        Log.d("motion", action + "");
        break;
    }
    return true;
}

From source file:chinanurse.cn.nurse.list.WaveSwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(@NonNull MotionEvent event) {
    ensureTarget();/* ww w .  ja  va2  s. c  o m*/

    if (!isEnabled() || canChildScrollUp() || isRefreshing()) {
        return false;
    }

    final int action = MotionEventCompat.getActionMasked(event);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(event, 0);
        mFirstTouchDownPointY = getMotionEventY(event, mActivePointerId);
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float currentY = getMotionEventY(event, mActivePointerId);

        if (currentY == -1) {
            return false;
        }

        if (mFirstTouchDownPointY == -1) {
            mFirstTouchDownPointY = currentY;
        }

        final float yDiff = currentY - mFirstTouchDownPointY;

        // State is changed to drag if over slop
        if (yDiff > ViewConfiguration.get(getContext()).getScaledTouchSlop() && !isRefreshing()) {
            mCircleView.makeProgressTransparent();
            return true;
        }

        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return false;
}