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

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

Introduction

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

Prototype

public static int findPointerIndex(MotionEvent event, int pointerId) 

Source Link

Document

Call MotionEvent#findPointerIndex(int) .

Usage

From source file:com.peerless2012.twowaynestedscrollview.TwoWayNestedScrollView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from   w  w w. ja  v a2  s  . 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 (getScrollY() == 0 && !ViewCompat.canScrollVertically(this, 1)) {
        return false;
    }

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

        final int x = (int) MotionEventCompat.getX(ev, pointerIndex);
        final int xDiff = Math.abs(x - mLastMotionX);
        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);
            }
        } else if (xDiff > mTouchSlop && (getNestedScrollAxes() & ViewCompat.SCROLL_AXIS_VERTICAL) == 0) {
            mIsBeingDragged = true;
            mLastMotionX = x;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            mNestedXOffset = 0;
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }
        break;
    }

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

        /*
         * Remember location of down touch. ACTION_DOWN always refers to
         * pointer index 0.
         */
        mLastMotionY = y;
        mLastMotionX = x;
        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:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        stopNestedScroll();
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

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

From source file:com.limxing.library.PullToRefresh.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    try {/*from w w w. j  a va2s .c  o m*/
        ensureTarget();

        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;
        }
        if (isRefreshing()) {
            return false;
        }

        if (mLoading) {
            return false;
        }

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

            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 x = MotionEventCompat.getX(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = y - mInitialMotionY;

            final float adx = Math.abs(x - mInitialMotionX);
            final float ady = Math.abs(y - mInitialMotionY);

            if (up) { // ??
                if (down) { // ??
                    return false;
                } else {
                    if (pull2load) {
                        double angle = Math.atan(adx / ady);
                        if (-yDiff > mTouchSlop && (angle < trigger_angle)) { // 
                            direction = 1;
                            mIsBeingDragged = true;
                        }
                    }
                }
            } else {
                if (pull2refresh) {
                    double angle = Math.atan(adx / ady);
                    if (yDiff > mTouchSlop && (angle < trigger_angle)) {
                        direction = 0;
                        mIsBeingDragged = true;
                    }
                }

            }

            break;

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

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

        return mIsBeingDragged;
    } catch (IllegalArgumentException e) {
        Log.e("error", e.getMessage());
        return false;
    }

}

From source file:com.android.hcframe.DraggableGridViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from   www .  jav a2 s. 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.
     */

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged || mLastDragged >= 0) {
            return true;
        }
        if (mIsUnableToDrag) {
            return false;
        }
    }

    switch (action) {
    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 = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = Math.abs(y - mInitialMotionY);
        HcLog.D("***Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
            HcLog.D("***Starting drag!");
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else if (yDiff > mTouchSlop) {
            // The finger has moved enough in the vertical
            // direction to be counted as a drag... abort
            // any attempt to drag horizontally, to work correctly
            // with children that have scrolling containers.
            HcLog.D("***Unable to drag!");
            mIsUnableToDrag = true;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            if (performDrag(x)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch. ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsUnableToDrag = false;

        mScroller.computeScrollOffset();
        if (mScrollState == SCROLL_STATE_SETTLING
                && Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
            // Let the user 'catch' the pager as it animates.
            mScroller.abortAnimation();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

        HcLog.D("***Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);
        mLastDragged = -1;
        break;
    }

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

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

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

From source file:com.gmy.widget.swiperefresh.SwipeRefreshLayout.java

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

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

    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 = canChildScrollUp();
        down = canChildScrollDown();
        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;  
        final float yDiff = y - mStartPoint;

        if ((mLastDirection == Mode.PULL_FROM_START && yDiff < 0)
                || (mLastDirection == Mode.PULL_FROM_END && yDiff > 0)) {
            return true;
        }

        if (!mIsBeingDragged && (yDiff > 0 && mLastDirection == Mode.PULL_FROM_START)
                || (yDiff < 0 && mLastDirection == Mode.PULL_FROM_END)) {
            mIsBeingDragged = true;
        }

        if (mIsBeingDragged) {
            // User velocity passed min velocity; trigger a refresh  
            if (yDiff > mDistanceToTriggerSync) {
                // User movement passed distance; trigger a refresh  
                if (mLastDirection == Mode.PULL_FROM_END) {
                    return true;

                }
                if ((mMode == Mode.PULL_FROM_START) || (mMode == Mode.BOTH)) {
                    mLastDirection = Mode.PULL_FROM_START;
                    startRefresh();
                }
            } else if (-yDiff > mDistanceToTriggerSync) {
                if ((!up && !down && !loadNoFull) || mLastDirection == Mode.PULL_FROM_START) {
                    return true;
                }
                if ((mMode == Mode.PULL_FROM_END) || (mMode == Mode.BOTH)) {
                    mLastDirection = Mode.PULL_FROM_END;
                    startLoad();
                }
            } else {
                if (!up && !down && yDiff < 0 && !loadNoFull) {
                    return true;
                }
                // Just track the user's movement  
                //  
                setTriggerPercentage(
                        mAccelerateInterpolator.getInterpolation(Math.abs(yDiff) / mDistanceToTriggerSync));
                updateContentOffsetTop((int) yDiff);
                if (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);
                    mLastDirection = Mode.DISABLED;
                } else {
                    mDirection = (yDiff > 0 ? 1 : -1);
                    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:
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mLastDirection = Mode.DISABLED;
        return false;
    }

    return true;
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (mFastScroller != null) {
        boolean intercepted = mFastScroller.onInterceptTouchEvent(ev);
        if (intercepted) {
            return true;
        }//  w  ww  .  j  ava 2s .c o m
    }

    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;
        if (mTouchMode == TOUCH_MODE_FLINGING) {
            // Catch!
            mTouchMode = TOUCH_MODE_DRAGGING;
            return true;
        }
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, index);
        final float dy = y - mLastTouchY + mTouchRemainderY;
        final int deltaY = (int) dy;
        mTouchRemainderY = dy - deltaY;

        if (Math.abs(dy) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            return true;
        }
    }
    }

    return false;
}

From source file:com.edgar.banner.BannerPagerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionX = ev.getX();/*from  w  w w  .  ja v  a 2 s .c  o m*/
        mLastMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        downPauseAutoPlay();
        break;
    case MotionEvent.ACTION_MOVE:
        if (isBannerLessThanOne())
            break;
        //?ViewPager.
        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);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = Math.abs(y - mLastMotionY);
        if (BuildConfig.DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
            ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }
        break;
    default:
        break;
    }
    return super.onInterceptTouchEvent(ev);
}

From source file:com.example.adminstrator.pullrefresh.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    ensureTarget();//ww  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.hxqc.mall.core.views.CustomScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/* w  w w.j  a v a  2 s.  co  m*/

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);

    switch (actionMasked) {
    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
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            DebugLog.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
        int deltaY = mLastMotionY - y;
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
            } else {
                deltaY += mTouchSlop;
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];

            final int oldY = getScrollY();
            final int range = getScrollRange();
            final int overscrollMode = ViewCompat.getOverScrollMode(this);
            boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS
                    || (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

            // Calling overScrollByCompat will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0, 0, true)
                    && !hasNestedScrollingParent()) {
                // Break our velocity if we hit a scroll barrier.
                mVelocityTracker.clear();
            }

            final int scrolledDeltaY = getScrollY() - oldY;
            final int unconsumedY = deltaY - scrolledDeltaY;
            if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            } else if (canOverscroll) {
                ensureGlows();
                final int pulledToY = oldY + deltaY;
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight(),
                            MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > range) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
                            1.f - MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);

            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                flingWithNestedDispatch(-initialVelocity);
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mIsSlidable) {
        return super.onTouchEvent(ev);
    }/*from   w ww. j av  a2s .c  om*/

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

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

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mInitialMotionX = x;
        mInitialMotionY = y;

        if (isSlideablePaneUnder(x, y)) {
            mScroller.abortAnimation();
            wantTouchEvents = true;
            mLastMotionX = x;
            setScrollState(SCROLL_STATE_DRAGGING);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float dx = Math.abs(x - mLastMotionX);
            final float dy = Math.abs(y - mLastMotionY);
            if (dx > mTouchSlop && dx > dy && isSlideablePaneUnder(x, y)) {
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                setScrollState(SCROLL_STATE_DRAGGING);
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            needsInvalidate |= performDrag(x, y);
        }
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (isDimmed(getCoverView())) {
            final int pi = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pi);
            final float y = MotionEventCompat.getY(ev, pi);
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            if (dx * dx + dy * dy < mTouchSlop * mTouchSlop && isSlideablePaneUnder(x, y)) {
                // Taps close a dimmed open pane.
                closePane(getCoverView(), 0);
                mActivePointerId = INVALID_POINTER;
                break;
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final VelocityTracker vt = mVelocityTracker;
            vt.computeCurrentVelocity(1000, mMaxVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(vt, mActivePointerId);

            boolean isPerformClose = false;
            switch (getViewSpec(mCoveredView)) {
            case LEFT:
                isPerformClose = initialVelocity < 0 || (initialVelocity == 0 && mSlideOffset < 0.5f);
                break;
            case RIGHT:
                isPerformClose = initialVelocity > 0 || (initialVelocity == 0 && mSlideOffset < 0.5f);
                break;
            default:
                break;
            }

            if (isPerformClose) {
                closePane(getCoverView(), initialVelocity);
            } else {
                openPane(getCoverView(), initialVelocity);
            }

            mActivePointerId = INVALID_POINTER;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            mActivePointerId = INVALID_POINTER;
            if (mSlideOffset < 0.5f) {
                closePane(getCoverView(), 0);
            } else {
                openPane(getCoverView(), 0);
            }
        }
        break;
    }

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

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

    if (needsInvalidate) {
        invalidate();
    }
    return wantTouchEvents;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();//from  w  ww  .j a v  a 2 s  . co m

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);

    switch (actionMasked) {
    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
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            DebugLog.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
        int deltaY = mLastMotionY - y;
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
            } else {
                deltaY += mTouchSlop;
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];

            final int oldY = getScrollY();
            final int range = getScrollRange();
            final int overscrollMode = ViewCompat.getOverScrollMode(this);
            boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS
                    || (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

            // Calling overScrollByCompat will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0, 0, true)
                    && !hasNestedScrollingParent()) {
                // Break our velocity if we hit a scroll barrier.
                mVelocityTracker.clear();
            }

            final int scrolledDeltaY = getScrollY() - oldY;
            final int unconsumedY = deltaY - scrolledDeltaY;
            if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            } else if (canOverscroll) {
                ensureGlows();
                final int pulledToY = oldY + deltaY;
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight(),
                            MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > range) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
                            1.f - MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);

            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                flingWithNestedDispatch(-initialVelocity);
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    default:
        break;
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}