Example usage for android.support.v4.view MotionEventCompat ACTION_POINTER_UP

List of usage examples for android.support.v4.view MotionEventCompat ACTION_POINTER_UP

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat ACTION_POINTER_UP.

Prototype

int ACTION_POINTER_UP

To view the source code for android.support.v4.view MotionEventCompat ACTION_POINTER_UP.

Click Source Link

Document

Synonym for MotionEvent#ACTION_POINTER_UP .

Usage

From source file:com.hxqc.mall.core.views.CustomScrollView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/* w w w  .ja v a  2s  .  c om*/
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        DebugLog.d("CustomScrollView", "one" + ev.getX());
        return true;
    }

    /*
     * Don't try to intercept touch if we can't scroll anyway.
     */
    if (getScrollY() == 0 && !ViewCompat.canScrollVertically(this, 1)) {
        DebugLog.d("CustomScrollView", "two" + ev.getX());
        return false;
    }

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_MOVE" + ev.getX());

        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
            DebugLog.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop && (getNestedScrollAxes() & ViewCompat.SCROLL_AXIS_VERTICAL) == 0) {
            mIsBeingDragged = true;
            mLastMotionY = y;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            mNestedYOffset = 0;
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_DOWN" + ev.getX());
        mDownX = ev.getX();
        final int y = (int) ev.getY();
        if (!inChild((int) ev.getX(), y)) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
        }

        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        /*
        * If being flinged and user touches the screen, initiate drag;
        * otherwise don't.  mScroller.isFinished should be false when
        * being flinged.
        */
        //                mIsBeingDragged = !mScroller.isFinished();
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        moveX = ev.getX() - mDownX;
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_UP" + ev.getX());
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        stopNestedScroll();
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_POINTER_UP" + ev.getX());
        onSecondaryPointerUp(ev);
        break;
    }

    if (Math.abs(moveX) < 10 && ev.getAction() == MotionEvent.ACTION_UP) {
        DebugLog.d("CustomScrollView", "ON" + "\nmoveX" + moveX + "\nmD" + mDownX);
        moveX = 0.1f;
        return false;
    }

    DebugLog.d("CustomScrollView", mIsBeingDragged + "");
    /*
    * The only time we want to intercept motion events is if we are in the
    * drag mode.
    */
    return mIsBeingDragged;
}

From source file:com.hxqc.mall.thirdshop.views.CustomScrollView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from ww w .  j a  v  a  2s. c  o m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        DebugLog.d("CustomScrollView", "one" + ev.getX());
        return true;
    }

    /*
     * Don't try to intercept touch if we can't scroll anyway.
     */
    if (getScrollY() == 0 && !ViewCompat.canScrollVertically(this, 1)) {
        DebugLog.d("CustomScrollView", "two" + ev.getX());
        return false;
    }

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_MOVE" + ev.getX());

        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
            DebugLog.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop && (getNestedScrollAxes() & ViewCompat.SCROLL_AXIS_VERTICAL) == 0) {
            mIsBeingDragged = true;
            mLastMotionY = y;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            mNestedYOffset = 0;
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_DOWN" + ev.getX());
        mDownX = ev.getX();
        final int y = (int) ev.getY();
        if (!inChild((int) ev.getX(), y)) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
        }

        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        /*
        * If being flinged and user touches the screen, initiate drag;
        * otherwise don't.  mScroller.isFinished should be false when
        * being flinged.
        */
        //                mIsBeingDragged = !mScroller.isFinished();
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        moveX = ev.getX() - mDownX;
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_UP" + ev.getX());
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        stopNestedScroll();
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        DebugLog.d("CustomScrollView", mIsBeingDragged + "ACTION_POINTER_UP" + ev.getX());
        onSecondaryPointerUp(ev);
        break;
    default:
        break;
    }

    if (Math.abs(moveX) < 10 && ev.getAction() == MotionEvent.ACTION_UP) {
        DebugLog.d("CustomScrollView", "ON" + "\nmoveX" + moveX + "\nmD" + mDownX);
        moveX = 0.1f;
        return false;
    }

    DebugLog.d("CustomScrollView", mIsBeingDragged + "");
    /*
    * The only time we want to intercept motion events is if we are in the
    * drag mode.
    */
    return mIsBeingDragged;
}

From source file:com.example.harris.onionweather.ui.swipe.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from  www.ja va  2 s  .  c o  m*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mIsPulling = false;
        mCanRefreshing = false;
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;

        if (!mIsBeingDragged && yDiff > mTouchSlop) {
            mIsBeingDragged = true;
        }

        if (mIsBeingDragged) {
            // User velocity passed min velocity; trigger a refresh
            // User movement passed distance; trigger a refresh
            //                        startRefresh();
            mCanStartRefresh = yDiff / 2 > mDistanceToTriggerSync;
            //                    else {
            // Just track the user's movement
            setTriggerPercentage(mAccelerateInterpolator
                    .getInterpolation(yDiff / mDistanceToTriggerSync > 1 ? 1 : yDiff / mDistanceToTriggerSync));
            updateContentOffsetTop((int) (yDiff / 2));
            if (mLastMotionY > y && mTarget.getTop() == getPaddingTop()) {
                // If the user puts the view back at the top, we
                // don't need to. This shouldn't be considered
                // cancelling the gesture as the user can restart from the top.
                removeCallbacks(mCancel);
            }
            //                        else {
            //                            updatePositionTimeout();
            //                        }
            //                    }
            mLastMotionY = y;
        }
        break;

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

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mCanStartRefresh) {
            startRefresh();
        } else {
            removeCallbacks(mCancel);
            post(mCancel);
        }

        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    return true;
}

From source file:com.digreamon.android.widget.SlidingPaneLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (!mIsSlidable || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        return super.onInterceptTouchEvent(ev);
    }/*from   w w w  .j  a v a2s  .  c  o m*/

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // No valid pointer = no valid drag. Ignore.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float yDiff = Math.abs(y - mLastMotionY);

        if (dx != 0 && !isGutterDrag(mLastMotionX, dx) && canScroll(this, false, (int) dx, (int) x, (int) y)) {
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            mIsUnableToDrag = true;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff && isSlideablePaneUnder(x, y)) {
            mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else if (yDiff > mTouchSlop) {
            mIsUnableToDrag = true;
            return false;
        }
        if (mScrollState == SCROLL_STATE_DRAGGING && performDrag(x, y)) {
            invalidate();
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        if (isSlideablePaneUnder(x, y)) {
            mLastMotionX = x;
            mLastMotionY = y;
            if (mScrollState == SCROLL_STATE_SETTLING || mScrollState == SCROLL_STATE_IDLE) {
                // Start dragging immediately. "Catch"
                setScrollState(SCROLL_STATE_DRAGGING);
            } else if (isDimmed(getCoverView())) {
                interceptTap = true;
            }
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);
    return mScrollState == SCROLL_STATE_DRAGGING || interceptTap;
}

From source file:com.hakerjack.experiments.CustomSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from w w  w.j a v a 2s.  c o m*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        if (mIsBeingDragged) {
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                    : mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            // where 1.0f is a full circle
            if (mSpinner.getVisibility() != View.VISIBLE) {
                mSpinner.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mSpinner, 1f);
                ViewCompat.setScaleY(mSpinner, 1f);
            }
            // Animate spinner's alpha based on y position relative to max distance
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                float overScrollTopToShow = overscrollTop - mHideSpinnerDistance;
                float progress;
                if (overScrollTopToShow > 0) {
                    progress = overScrollTopToShow / (mTotalDragDistance - mHideSpinnerDistance);
                } else {
                    progress = 0;
                }
                mSpinner.setSpinnerAlpha((int) (progress * MAX_ALPHA));
                mSpinner.animateUnderDraggableDistance(dragPercent, tensionPercent);
            }
            mSpinner.animateUnderDrag(dragPercent, tensionPercent);

            int offset = targetY - mCurrentTargetOffsetTop;
            setTargetOffsetTopAndBottom(offset, true /* requires update */);

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

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mSpinner.animateUnderDrag(0f, 0f);
            Animation.AnimationListener listener = null;
            if (!mScale) {
                listener = new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (!mScale) {
                            startScaleDownAnimation(null);
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                };
            }
            animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}

From source file:com.hybunion.common.view.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/* w w  w.  ja v a 2s.co  m*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mIsPulling = false;
        mCanRefreshing = false;
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;

        if (!mIsBeingDragged && yDiff > mTouchSlop) {
            mIsBeingDragged = true;
        }

        if (mIsBeingDragged) {
            // User velocity passed min velocity; trigger a refresh
            if (yDiff / 2 > mDistanceToTriggerSync) {
                // User movement passed distance; trigger a refresh
                //                        startRefresh();
                mCanStartRefresh = true;
            } else {
                mCanStartRefresh = false;
            }
            //                    else {
            // Just track the user's movement
            setTriggerPercentage(mAccelerateInterpolator
                    .getInterpolation(yDiff / mDistanceToTriggerSync > 1 ? 1 : yDiff / mDistanceToTriggerSync));
            updateContentOffsetTop((int) (yDiff / 2));
            if (mLastMotionY > y && mTarget.getTop() == getPaddingTop()) {
                // If the user puts the view back at the top, we
                // don't need to. This shouldn't be considered
                // cancelling the gesture as the user can restart from the top.
                removeCallbacks(mCancel);
            }
            //                        else {
            //                            updatePositionTimeout();
            //                        }
            //                    }
            mLastMotionY = y;
        }
        break;

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

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mCanStartRefresh) {
            startRefresh();
        } else {
            removeCallbacks(mCancel);
            post(mCancel);
        }

        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    return true;
}

From source file:com.ushahidi.platform.mobile.app.presentation.view.ui.widget.TitlePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*www  .  j a  va2s. c  o  m*/
    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;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        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;
    default:
        break;
    }

    return true;
}

From source file:co.vn.e_alarm.customwiget.SlidingLayer.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHereX(ev, mInitialX)
            && !allowSlidingFromHereY(ev, mInitialY)) {
        return false;
    }//from   w ww .  j  ava  2  s.c o m

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
            || action == MotionEvent.ACTION_OUTSIDE) {
        mLastTouchAllowed = false;
    } else {
        mLastTouchAllowed = true;
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        completeScroll();
        // Remember where the motion event started
        mLastX = mInitialX = ev.getX();
        mLastY = mInitialY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mIsDragging) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex == -1) {
                mActivePointerId = INVALID_POINTER;
                break;
            }
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastY);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                mIsDragging = true;
                mLastX = x;
                setDrawingCacheEnabled(true);
            } else if (yDiff > mTouchSlop && yDiff > xDiff) {
                mIsDragging = true;
                mLastY = y;
                setDrawingCacheEnabled(true);
            }
        }
        if (mIsDragging) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (activePointerIndex == -1) {
                mActivePointerId = INVALID_POINTER;
                break;
            }
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final float deltaX = mLastX - x;
            final float deltaY = mLastY - y;
            mLastX = x;
            mLastY = y;
            final float oldScrollX = getScrollX();
            final float oldScrollY = getScrollY();
            float scrollX = oldScrollX + deltaX;
            float scrollY = oldScrollY + deltaY;

            /*
             * Not need change : Done
             */
            final float leftBound, rightBound;
            final float bottomBound, topBound;
            switch (mScreenSide) {
            case STICK_TO_LEFT:
                topBound = bottomBound = rightBound = 0;
                leftBound = getWidth(); // How far left we can scroll
                break;
            case STICK_TO_MIDDLE:
                topBound = getHeight();
                bottomBound = -getHeight();
                leftBound = getWidth();
                rightBound = -getWidth();
                break;
            case STICK_TO_RIGHT:
                rightBound = -getWidth();
                topBound = bottomBound = leftBound = 0;
                break;
            case STICK_TO_TOP:
                topBound = getHeight();
                bottomBound = rightBound = leftBound = 0;
                break;
            case STICK_TO_BOTTOM:
                topBound = rightBound = leftBound = 0;
                bottomBound = -getHeight();
                break;
            default:
                topBound = bottomBound = rightBound = leftBound = 0;
                break;
            }
            if (scrollX > leftBound) {
                scrollX = leftBound;
            } else if (scrollX < rightBound) {
                scrollX = rightBound;
            }
            if (scrollY > topBound) {
                scrollY = topBound;
            } else if (scrollY < bottomBound) {
                scrollY = bottomBound;
            }

            // TODO top/bottom bounds
            // Keep the precision
            mLastX += scrollX - (int) scrollX;
            mLastY += scrollY - (int) scrollY;
            scrollTo((int) scrollX, (int) scrollY);
        }
        break;
    //--- Doan nay dang kiem tra
    case MotionEvent.ACTION_UP:
        if (mIsDragging) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocityX = (int) VelocityTrackerCompat.getXVelocity(velocityTracker,
                    mActivePointerId);
            final int initialVelocityY = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                    mActivePointerId);
            final int scrollX = getScrollX();
            final int scrollY = getScrollY();
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final int totalDeltaX = (int) (x - mInitialX);
            final int totalDeltaY = (int) (y - mInitialY);

            /*
            * Here is condition to check when to change state of layer. open / close.
            */
            boolean nextStateOpened = determineNextStateOpened(mIsOpen, scrollX, scrollY, initialVelocityX,
                    initialVelocityY, totalDeltaX, totalDeltaY);
            switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY);

            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mIsOpen && closeOnTapEnabled) {
            closeLayer(true);
        } else if (!mIsOpen && openOnTapEnabled) {
            openLayer(true);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
            switchLayer(mIsOpen, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastX = MotionEventCompat.getX(ev, index);
        mLastY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        mLastY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
        mLastTouchAllowed = false;
    }
    return true;
}

From source file:com.lynx.lib.widget.swipelayout.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*from  w w w .j  a v  a 2  s. c o m*/

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    if (!isEnabled() || mReturningToStart) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mStartPoint = mInitialMotionY;

        // up/down????????
        // ????canChildScrollUp/canChildScrollDown
        // ????????
        up = canChildScrollUp();
        down = canChildScrollDown();
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        // final float yDiff = y - mInitialMotionY;
        final float yDiff = y - mStartPoint;
        // ????
        if ((mLastDirection == Mode.PULL_FROM_START && yDiff < 0)
                || (mLastDirection == Mode.PULL_FROM_END && yDiff > 0)) {
            return false;
        }
        // ???
        // mStartPoint
        if ((canChildScrollUp() && yDiff > 0) || (canChildScrollDown() && yDiff < 0)) {
            mStartPoint = y;
        }

        // 
        if (yDiff > mTouchSlop) {
            // ???
            if (canChildScrollUp() || mLastDirection == Mode.PULL_FROM_END) {
                mIsBeingDragged = false;
                return false;
            }
            if ((mMode == Mode.PULL_FROM_START) || (mMode == Mode.BOTH)) {
                mLastMotionY = y;
                mIsBeingDragged = true;
                mLastDirection = Mode.PULL_FROM_START;
            }
        }
        // 
        else if (-yDiff > mTouchSlop) {
            // ???
            if (canChildScrollDown() || mLastDirection == Mode.PULL_FROM_START) {
                mIsBeingDragged = false;
                return false;
            }
            // ????????
            if (!up && !down && !loadNoFull) {
                mIsBeingDragged = false;
                return false;
            }
            if ((mMode == Mode.PULL_FROM_END) || (mMode == Mode.BOTH)) {
                mLastMotionY = y;
                mIsBeingDragged = true;
                mLastDirection = Mode.PULL_FROM_END;
            }
        }
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mLastDirection = Mode.DISABLED;
        break;
    }

    return mIsBeingDragged;
}

From source file:com.cdwx.moka.widget.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();//  w  w  w  . j  ava  2s.co m

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    if (!isEnabled() || mReturningToStart) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mStartPoint = mInitialMotionY;

        //up/down????????
        //????canChildScrollUp/canChildScrollDown
        //????????
        up = canChildScrollUp();
        down = canChildScrollDown();
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        //                final float yDiff = y - mInitialMotionY;
        final float yDiff = y - mStartPoint;
        //????
        if ((mLastDirection == Mode.PULL_FROM_START && yDiff < 0)
                || (mLastDirection == Mode.PULL_FROM_END && yDiff > 0)) {
            return false;
        }
        //???
        //mStartPoint
        if ((canChildScrollUp() && yDiff > 0) || (canChildScrollDown() && yDiff < 0)) {
            mStartPoint = y;
        }

        //
        if (yDiff > mTouchSlop) {
            //???
            if (canChildScrollUp() || mLastDirection == Mode.PULL_FROM_END) {
                mIsBeingDragged = false;
                return false;
            }
            if ((mMode == Mode.PULL_FROM_START) || (mMode == Mode.BOTH)) {
                mLastMotionY = y;
                mIsBeingDragged = true;
                mLastDirection = Mode.PULL_FROM_START;
            }
        }
        //
        else if (-yDiff > mTouchSlop) {
            //???
            if (canChildScrollDown() || mLastDirection == Mode.PULL_FROM_START) {
                mIsBeingDragged = false;
                return false;
            }
            //????????
            if (!up && !down && !loadNoFull) {
                mIsBeingDragged = false;
                return false;
            }
            if ((mMode == Mode.PULL_FROM_END) || (mMode == Mode.BOTH)) {
                mLastMotionY = y;
                mIsBeingDragged = true;
                mLastDirection = Mode.PULL_FROM_END;
            }
        }
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mLastDirection = Mode.DISABLED;
        break;
    }

    return mIsBeingDragged;
}