Example usage for android.view MotionEvent findPointerIndex

List of usage examples for android.view MotionEvent findPointerIndex

Introduction

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

Prototype

public final int findPointerIndex(int pointerId) 

Source Link

Document

Given a pointer identifier, find the index of its data in the event.

Usage

From source file:com.hippo.widget.BothScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/*ww w.j  a v  a 2 s.  c om*/
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = (int) ev.getX();
        mLastMotionY = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }
        final int x = (int) ev.getX(activePointerIndex);
        final int y = (int) ev.getY(activePointerIndex);
        int deltaX = mLastMotionX - x;
        int deltaY = mLastMotionY - y;
        if (!mIsBeingDragged && (Math.abs(deltaX) > mTouchSlop || Math.abs(deltaY) > mTouchSlop)) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaX > 0) {
                deltaX -= mTouchSlop;
                deltaX = Math.max(0, deltaX);
            } else {
                deltaX += mTouchSlop;
                deltaX = Math.min(0, deltaX);
            }
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
                deltaY = Math.max(0, deltaY);
            } else {
                deltaY += mTouchSlop;
                deltaY = Math.min(0, deltaY);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionX = x;
            mLastMotionY = y;

            final int oldX = getScrollX();
            final int oldY = getScrollY();
            final int horizontalRange = getHorizontalScrollRange();
            final int verticalRange = getVerticalScrollRange();
            final int overscrollMode = getOverScrollMode();
            final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS
                    || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS
                            && (horizontalRange > 0 || verticalRange > 0));

            // Calling overScrollBy will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            overScrollBy(deltaX, deltaY, oldX, oldY, horizontalRange, verticalRange, mOverscrollDistance,
                    mOverscrollDistance, true);

            if (canOverscroll) {
                final int pulledToX = oldX + deltaX;
                final int pulledToY = oldY + deltaY;
                if (pulledToX < 0) {
                    mEdgeGlowLeft.onPull((float) deltaX / getWidth());
                    if (!mEdgeGlowRight.isFinished()) {
                        mEdgeGlowRight.onRelease();
                    }
                } else if (pulledToX > horizontalRange) {
                    mEdgeGlowRight.onPull((float) deltaX / getWidth());
                    if (!mEdgeGlowLeft.isFinished()) {
                        mEdgeGlowLeft.onRelease();
                    }
                }
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > verticalRange) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowLeft != null && (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()
                        || !mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
            int initialVelocityY = (int) velocityTracker.getYVelocity(mActivePointerId);

            if (getChildCount() > 0) {
                if (Math.abs(initialVelocityX) > mMinimumVelocity
                        || Math.abs(initialVelocityY) > mMinimumVelocity) {
                    fling(-initialVelocityX, -initialVelocityY);
                } else {
                    if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                            getVerticalScrollRange())) {
                        ViewCompat.postInvalidateOnAnimation(this);
                    }
                }
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                    getVerticalScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = ev.findPointerIndex(mActivePointerId);
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        break;
    }
    return true;
}

From source file:com.ecom.consumer.customviews.HorizontalListView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    String move = "";
    final int index = getChildIndex((int) ev.getX(), (int) ev.getY());
    // Log.d("onsingleTapclicked", "" + index);
    // If the tap is inside one of the child views, and we are not
    // blocking touches
    ///*from w  ww.  ja  v  a2  s.co  m*/
    mViewBeingTouched = getChildAt(index);

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        isTouch = true;
        mIsBeingDragged = super.onInterceptTouchEvent(ev);
        mIsDragged = mIsBeingDragged;
        mLastMotionX = ev.getX();
        mActivePointerId = ev.getPointerId(0);
        // final int index = getChildIndex((int) ev.getX(), (int)
        // ev.getY());

        /**
         * show blue
         */
        mGestureListener.showSelected(true);
        if (index >= 0) {
            // Save off view being touched so it can later be released
            mViewBeingTouched = getChildAt(index);
            // System.out.println("HorizontalListView"+" "+mViewBeingTouched.getHeight());
            if (mViewBeingTouched != null) {
                // Set the view as pressed
                mViewBeingTouched.setPressed(true);
                // invalidate();
                refreshDrawableState();
            }
        }
        move = "DOWN" + index;

        break;
    case MotionEvent.ACTION_MOVE:
        final int activePointerId = mActivePointerId;
        if (activePointerId == -1) {
            // If we don't have a valid id, the touch down wasn't on
            // content.
            break;
        }

        unpressTouchedChild();

        final int pointerIndex = ev.findPointerIndex(activePointerId);
        final float x = ev.getX(pointerIndex);
        final int xDiff = (int) Math.abs(x - mLastMotionX);
        if (xDiff > mTouchSlop) {
            mIsBeingDragged = true;

            mLastMotionX = x;
        }
        move = "MOVE";
        // mGestureListener.showSelected(false);
        break;
    case MotionEvent.ACTION_UP:
        mIsBeingDragged = super.onInterceptTouchEvent(ev);
        move = "UP";
        unpressTouchedChild();
        mGestureListener.showSelected(false);
        break;
    case MotionEvent.ACTION_CANCEL:
        move = "CANCEL";
        // mGestureListener.showSelected(false);
        break;
    }
    // System.out.println("HorizontalListView.onInterceptTouchEvent()" +
    // move
    // + mIsBeingDragged);
    return mIsBeingDragged;
}

From source file:com.farukcankaya.rtlviewpagerindicator.CircleIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w  w w.ja  v a2s .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;

            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 = ev.getX(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }

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

    return true;
}

From source file:com.hippo.widget.BothScrollView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/* w  w 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)) {
        return true;
    }

    /*
     * Don't try to intercept touch if we can't scroll anyway.
     */
    if (getScrollX() == 0 && !canScrollHorizontally(1) && getScrollY() == 0 && !canScrollVertically(1)) {
        return false;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * 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 = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
        }

        final int x = (int) ev.getX(pointerIndex);
        final int y = (int) ev.getY(pointerIndex);
        final int xDiff = Math.abs(x - mLastMotionX);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (xDiff > mTouchSlop || yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionX = x;
            mLastMotionY = y;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (!inChild(x, y)) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
        }

        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = ev.getPointerId(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();
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                getVerticalScrollRange())) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = ev.findPointerIndex(mActivePointerId);
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        break;
    }

    /*
    * The only time we want to intercept motion events is if we are in the
    * drag mode.
    */
    return mIsBeingDragged;
}

From source file:com.android.internal.widget.ViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }/*from  w w  w. jav  a 2s  . co m*/

    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

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

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        mPopulatePending = false;
        populate();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = ev.getPointerId(0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = ev.getY(pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (DEBUG)
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                requestParentDisallowInterceptTouchEvent(true);
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                mLastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);

                // Disallow Parent Intercept, just in case
                ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(activePointerIndex);
            needsInvalidate |= performDrag(x);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);

            mPopulatePending = true;

            final float scrollStart = getScrollStart();
            final float scrolledPages = scrollStart / getPaddedWidth();
            final ItemInfo ii = infoForFirstVisiblePage();
            final int currentPage = ii.position;
            final float nextPageOffset;
            if (isLayoutRtl()) {
                nextPageOffset = (ii.offset - scrolledPages) / ii.widthFactor;
            } else {
                nextPageOffset = (scrolledPages - ii.offset) / ii.widthFactor;
            }

            final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            final int nextPage = determineTargetPage(currentPage, nextPageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            mLeftEdge.onRelease();
            mRightEdge.onRelease();
            needsInvalidate = true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            mLeftEdge.onRelease();
            mRightEdge.onRelease();
            needsInvalidate = true;
        }
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        final float x = ev.getX(index);
        mLastMotionX = x;
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        postInvalidateOnAnimation();
    }
    return true;
}

From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *///from  www  .  j a va  2 s  . c o  m
public void processTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    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 = ev.getPointerId(0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

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

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

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal down event.

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

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            final int index = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(index);
            final float y = ev.getY(index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = ev.getPointerId(i);
                final float x = ev.getX(i);
                final float y = ev.getY(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 (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int id = ev.getPointerId(i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = ev.getX(i);
                final float y = ev.getY(i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}

From source file:com.lovebridge.library.view.staggeredGridView.ExtendableListView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    if (!mIsAttached) {
        // Something isn't right.
        // Since we rely on being attached to get data set change
        // notifications,
        // don't risk doing anything where we might try to resync and find
        // things
        // in a bogus state.
        return false;
    }/*from w w  w. j a  v a2s. com*/
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        int touchMode = mTouchMode;
        // TODO : overscroll
        // if (touchMode == TOUCH_MODE_OVERFLING || touchMode ==
        // TOUCH_MODE_OVERSCROLL) {
        // mMotionCorrection = 0;
        // return true;
        // }
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);
        int motionPosition = findMotionRow(y);
        if (touchMode != TOUCH_MODE_FLINGING && motionPosition >= 0) {
            // User clicked on an actual view (and was not stopping a
            // fling).
            // Remember where the motion event started
            mMotionX = x;
            mMotionY = y;
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
        }
        mLastY = Integer.MIN_VALUE;
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        if (touchMode == TOUCH_MODE_FLINGING) {
            return true;
        }
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
            int pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex == -1) {
                pointerIndex = 0;
                mActivePointerId = ev.getPointerId(pointerIndex);
            }
            final int y = (int) ev.getY(pointerIndex);
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            if (startScrollIfNeeded(y)) {
                return true;
            }
            break;
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mTouchMode = TOUCH_MODE_IDLE;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        break;
    }
    }
    return false;
}

From source file:jackson.com.slidingmenulib.MyViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *///w ww .  j  a  v  a  2  s.  c o m
public void processTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    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 = ev.getPointerId(0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

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

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

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal down event.

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

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            // If pointer is invalid then skip the ACTION_MOVE.
            if (!isValidPointerForActionMove(mActivePointerId))
                break;

            final int index = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(index);
            final float y = ev.getY(index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = ev.getPointerId(i);

                // If pointer is invalid then skip the ACTION_MOVE.
                if (!isValidPointerForActionMove(pointerId))
                    continue;

                final float x = ev.getX(i);
                final float y = ev.getY(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 (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int id = ev.getPointerId(i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = ev.getX(i);
                final float y = ev.getY(i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}

From source file:com.iangclifton.auid.horizontaliconview.HorizontalIconView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }/*  w w w . j  a v a2 s.c o m*/
    mVelocityTracker.addMovement(ev);

    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mPreviousX = (int) MotionEventCompat.getX(ev, 0);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == INVALID_POINTER) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

        final int x = (int) MotionEventCompat.getX(ev, 0);
        int deltaX = (int) (mPreviousX - x);
        if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) {
            mIsBeingDragged = true;
            if (deltaX > 0) {
                deltaX -= mTouchSlop;
            } else {
                deltaX += mTouchSlop;
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mPreviousX = x;

            final int oldX = getScrollX();
            final int range = mScrollRange;

            if (overScrollBy(deltaX, 0, oldX, 0, range, 0, mOverscrollDistance, 0, true)) {
                // Break our velocity if we hit a scroll barrier.
                mVelocityTracker.clear();
            }

            if (mEdgeEffectLeft != null) {
                final int pulledToX = oldX + deltaX;
                if (pulledToX < 0) {
                    mEdgeEffectLeft.onPull((float) deltaX / getWidth());
                    if (!mEdgeEffectRight.isFinished()) {
                        mEdgeEffectRight.onRelease();
                    }
                } else if (pulledToX > range) {
                    mEdgeEffectRight.onPull((float) deltaX / getWidth());
                    if (!mEdgeEffectLeft.isFinished()) {
                        mEdgeEffectLeft.onRelease();
                    }
                }
                if (!mEdgeEffectLeft.isFinished() || !mEdgeEffectRight.isFinished()) {
                    postInvalidateOnAnimation();
                }

            }

        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        if (mIsBeingDragged) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) mVelocityTracker.getXVelocity(mActivePointerId);

            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                fling(-initialVelocity);
            } else {
                if (mScroller.springBack(getScrollX(), 0, 0, mScrollRange, 0, 0)) {
                    postInvalidateOnAnimation();
                }
            }

            mActivePointerId = INVALID_POINTER;
            mIsBeingDragged = false;
            mVelocityTracker.recycle();
            mVelocityTracker = null;

            if (mEdgeEffectLeft != null) {
                mEdgeEffectLeft.onRelease();
                mEdgeEffectRight.onRelease();
            }
        } else {
            // Was not being dragged, was this a press on an icon?
            final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
            if (activePointerIndex == INVALID_POINTER) {
                return false;
            }
            final int x = (int) ev.getX(activePointerIndex) + getScrollX();
            final int y = (int) ev.getY(activePointerIndex);
            int i = 0;
            for (Rect rect : mIconPositions) {
                if (rect.contains(x, y)) {
                    final int position = i + mSkippedIconCount;
                    Toast.makeText(getContext(),
                            "Pressed icon " + position + "; rect count: " + mIconPositions.size(),
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                i++;
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        if (mIsBeingDragged) {
            if (mScroller.springBack(getScrollX(), 0, 0, mScrollRange, 0, 0)) {
                postInvalidateOnAnimation();
            }
            mActivePointerId = INVALID_POINTER;
            mIsBeingDragged = false;
            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }

            if (mEdgeEffectLeft != null) {
                mEdgeEffectLeft.onRelease();
                mEdgeEffectRight.onRelease();
            }
        }
        break;
    }
    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        break;
    }
    }
    return true;
}

From source file:wdwd.com.booklib.util.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *///w  w w  .j  a  v a 2 s  .c om
public void processTouchEvent(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);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        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) {
            // If we're idle we can do anything! Treat it like a normal down event.

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

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            final int index = ev.findPointerIndex(mActivePointerId);
            if (index == -1) {
                break;
            }
            final float x = ev.getX(index);
            final float y = ev.getY(index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            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 (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int id = MotionEventCompat.getPointerId(ev, i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}