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

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

Introduction

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

Prototype

public static int getActionIndex(MotionEvent event) 

Source Link

Document

Call MotionEvent#getAction , returning only the pointer index portion

Usage

From source file:com.appsummary.luoxf.mylibrary.swipbackhelper.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*from w  w  w  .ja v a  2 s. c o m*/
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        if (mDragState == STATE_SETTLING) {
            setDragState(STATE_DRAGGING);
        } else if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
            setDragState(STATE_JUDGING);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);
        saveInitialMotion(x, y, pointerId);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_JUDGING) {
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            }
            if (mDragState == STATE_DRAGGING)
                mVelocityTracker.addMovement(ev);
            final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId);

            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[mActivePointerId];
            final float dy = y - mInitialMotionY[mActivePointerId];

            reportNewEdgeDrags(dx, dy, mActivePointerId);

            final View toCapture = findTopChildUnder((int) x, (int) y);
            int slop = checkTouchSlop(toCapture, dx, dy);

            if (slop == -1)
                cancel();
            else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) {
                break;
            }
            saveLastMotion(ev);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        releaseViewForPointerUp();
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        dispatchViewReleased(0, 0);
        cancel();
        break;
    }

    }
    return mDragState == STATE_DRAGGING;
}

From source file:com.android.kit.swipeback.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*from  w ww  .jav a 2  s . co  m*/
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {

    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        final int edgesTouched = mInitialEdgeTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_JUDGING) {

            final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId);

            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[mActivePointerId];
            final float dy = y - mInitialMotionY[mActivePointerId];

            reportNewEdgeDrags(dx, dy, mActivePointerId);

            final View toCapture = findTopChildUnder((int) x, (int) y);
            int slop = checkTouchSlop(toCapture, dx, dy);

            if (slop == -1)
                cancel();
            else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) {
                break;
            }
            saveLastMotion(ev);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }

    }
    return mDragState == STATE_DRAGGING;
}

From source file:com.cylee.dragcontentviewpager.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's onInterceptTouchEvent should
 * cause the parent to intercept the touch event stream.
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from onInterceptTouchEvent
 *///from   w  ww .j  ava  2s  . co m
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't getInstance
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            final View toCapture = findTopChildUnder((int) x, (int) y);
            final boolean pastSlop = toCapture != null && checkTouchSlop(toCapture, dx, dy);
            if (pastSlop) {
                // check the callback's
                // getView[Horizontal|Vertical]DragRange methods to know
                // if you can move at all along an axis, then see if it
                // would clamp to the same value. If you can't move at
                // all in every dimension with a nonzero range, bail.
                final int oldLeft = toCapture.getLeft();
                final int targetLeft = oldLeft + (int) dx;
                final int newLeft = mCallback.clampViewPositionHorizontal(toCapture, targetLeft, (int) dx);
                final int oldTop = toCapture.getTop();
                final int targetTop = oldTop + (int) dy;
                final int newTop = mCallback.clampViewPositionVertical(toCapture, targetTop, (int) dy);
                final int horizontalDragRange = mCallback.getViewHorizontalDragRange(toCapture);
                final int verticalDragRange = mCallback.getViewVerticalDragRange(toCapture);
                if ((horizontalDragRange == 0 || horizontalDragRange > 0 && newLeft == oldLeft)
                        && (verticalDragRange == 0 || verticalDragRange > 0 && newTop == oldTop)) {
                    break;
                }
            }
            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            if (pastSlop && !canScroll(toCapture, true, (int) dx, (int) dy, (int) x, (int) y)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:android.wuliqing.com.lendphonesystemapp.swipeBack.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream.//from   www. ja v a2s .  c om
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgeTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also
        // report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:com.tesla.framework.ui.widget.swipeback.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*from  ww w  .jav a2 s .  com*/
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't getString
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgeTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also
        // report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:cn.zmdx.kaka.locker.widget.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream.//from www  . j  a va  2 s  .c  om
 * 
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 *         onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also
        // report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) mInitialMotionX[pointerId],
                    (int) mInitialMotionY[pointerId]);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:com.ysy.classpower_utils.swipe_back.OwnViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*from  w  w w  . ja va 2 s  . c o  m*/
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgeTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A OwnViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also
        // report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:ayushi.view.customview.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's onInterceptTouchEvent should
 * cause the parent to intercept the touch event stream.
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from onInterceptTouchEvent
 *//*  w w  w . j  a  va2s  .c o m*/
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            if (pointerId >= mInitialMotionX.length || pointerId >= mInitialMotionY.length) {
                continue;
            }
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) mInitialMotionX[pointerId],
                    (int) mInitialMotionY[pointerId]);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:com.tuniu.widget.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*  w ww .j ava2  s. c o  m*/
    }

    if (canChildScrollDown() && canChildScrollUp()) {
        return false;
    }

    if (!isEnabled() || mReturningToStart || mNestedScrollInProgress) {
        // 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: {
        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;
        Log.e("onTouchEvent", "overscrollTop==========" + overscrollTop);
        if (mIsBeingDragged) {
            boolean canMoveSpinner = ((!canChildScrollUp()) && overscrollTop > 0)
                    || ((!canChildScrollDown()) && overscrollTop < 0);
            if (canMoveSpinner) {
                moveSpinner(Math.abs(overscrollTop));
            } else {
                return false;
            }
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        pointerIndex = MotionEventCompat.getActionIndex(ev);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_POINTER_DOWN event but have an invalid action index.");
            return false;
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        break;
    }

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

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

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        finishSpinner(overscrollTop);
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    case MotionEvent.ACTION_CANCEL:
        return false;
    }

    return true;
}

From source file:com.example.administrator.demo.activity.sbackapp.SwipeBackViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream.//from www.jav  a 2  s  .  com
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgeTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also
        // report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];
            if (Math.abs(dx) < Math.abs(dy)) {
                return false;
            }
            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}