Example usage for android.view MotionEvent getX

List of usage examples for android.view MotionEvent getX

Introduction

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

Prototype

public final float getX() 

Source Link

Document

#getX(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:cc.flydev.launcher.Page.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (DISABLE_TOUCH_INTERACTION) {
        return false;
    }//from  www  .jav a  2s .  c om

    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onTouchEvent will be called and we do the actual
     * scrolling there.
     */
    acquireVelocityTrackerAndAddMovement(ev);

    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onInterceptTouchEvent(ev);

    /*
     * 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) && (mTouchState == TOUCH_STATE_SCROLLING)) {
        return true;
    }

    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.
         */
        if (mActivePointerId != INVALID_POINTER) {
            determineScrollingStart(ev);
        }
        // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
        // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
        // i.e. fall through to the next case (don't break)
        // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
        // while it's small- this was causing a crash before we checked for INVALID_POINTER)
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mDownMotionX = x;
        mDownMotionY = y;
        mDownScrollX = getScrollX();
        mLastMotionX = x;
        mLastMotionY = y;
        float[] p = mapPointFromViewToParent(this, x, y);
        mParentDownMotionX = p[0];
        mParentDownMotionY = p[1];
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);

        /*
         * If being flinged and user touches the screen, initiate drag;
         * otherwise don't.  mScroller.isFinished should be false when
         * being flinged.
         */
        final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
        final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
        if (finishedScrolling) {
            mTouchState = TOUCH_STATE_REST;
            mScroller.abortAnimation();
        } else {
            if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
                mTouchState = TOUCH_STATE_SCROLLING;
            } else {
                mTouchState = TOUCH_STATE_REST;
            }
        }

        // check if this can be the beginning of a tap on the side of the pages
        // to scroll the current page
        if (!DISABLE_TOUCH_SIDE_PAGES) {
            if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
                if (getChildCount() > 0) {
                    if (hitsPreviousPage(x, y)) {
                        mTouchState = TOUCH_STATE_PREV_PAGE;
                    } else if (hitsNextPage(x, y)) {
                        mTouchState = TOUCH_STATE_NEXT_PAGE;
                    }
                }
            }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        resetTouchState();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        releaseVelocityTracker();
        break;
    }

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

From source file:com.skytree.epubtest.BookViewActivity.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.isReversed) {
        event.setLocation(this.getWidth() - event.getX(), event.getY());
    }/*from w w w  .  j  a v  a 2s  .  c  om*/
    return super.onTouchEvent(event);
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from  w  ww  .j av a 2 s .  co  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.skytree.epubtest.BookViewActivity.java

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    try {/* w  ww. j a  v  a 2  s.  c  o  m*/
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        // right to left swipe
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //                Toast.makeText(getContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
            if (this.skyLayoutListener != null) {
                skyLayoutListener.onSwipeToLeft(this);
            }
        }
        // left to right swipe
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //                Toast.makeText(getContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
            if (this.skyLayoutListener != null) {
                skyLayoutListener.onSwipeToRight(this);
            }
        }
        // down to up swipe
        else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //                Toast.makeText(getContext(), "Swipe up", Toast.LENGTH_SHORT).show();
        }
        // up to down swipe
        else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //                Toast.makeText(getContext(), "Swipe down", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {

    }
    return true;
}

From source file:cn.com.zzwfang.view.directionalviewpager.DirectionalViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from   w ww  .  ja va 2s.  com*/
     * 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.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            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 && Build.VERSION.SDK_INT > Build.VERSION_CODES.DONUT) {
            // 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 y = MotionEventCompat.getY(ev, pointerIndex);
        final float xDiff = Math.abs(x - mLastMotionX);
        final float yDiff = Math.abs(y - mLastMotionY);
        float primaryDiff;
        float secondaryDiff;

        if (mOrientation == HORIZONTAL) {
            primaryDiff = xDiff;
            secondaryDiff = yDiff;
        } else {
            primaryDiff = yDiff;
            secondaryDiff = xDiff;
        }

        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (primaryDiff > mTouchSlop && primaryDiff > secondaryDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            if (mOrientation == HORIZONTAL) {
                mLastMotionX = x;
            } else {
                mLastMotionY = y;
            }
            setScrollingCacheEnabled(true);
        } else {
            if (secondaryDiff > 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.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = mInitialMotion = ev.getX();
            mLastMotionY = ev.getY();
        } else {
            mLastMotionX = ev.getX();
            mLastMotionY = mInitialMotion = ev.getY();
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        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.android.launcher3.Workspace.java

@Override
protected void determineScrollingStart(MotionEvent ev) {
    if (!isFinishedSwitchingState())
        return;//from w w w. java2s. c o m

    float deltaX = ev.getX() - mXDown;
    float absDeltaX = Math.abs(deltaX);
    float absDeltaY = Math.abs(ev.getY() - mYDown);

    if (Float.compare(absDeltaX, 0f) == 0)
        return;

    float slope = absDeltaY / absDeltaX;
    float theta = (float) Math.atan(slope);

    if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) {
        cancelCurrentPageLongPress();
    }

    boolean passRightSwipesToCustomContent = (mTouchDownTime
            - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY;

    boolean swipeInIgnoreDirection = isLayoutRtl() ? deltaX < 0 : deltaX > 0;
    boolean onCustomContentScreen = getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID;
    if (swipeInIgnoreDirection && onCustomContentScreen && passRightSwipesToCustomContent) {
        // Pass swipes to the right to the custom content page.
        return;
    }

    if (onCustomContentScreen && (mCustomContentCallbacks != null)
            && !mCustomContentCallbacks.isScrollingAllowed()) {
        // Don't allow workspace scrolling if the current custom content screen doesn't allow
        // scrolling.
        return;
    }

    if (theta > MAX_SWIPE_ANGLE) {
        // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
        return;
    } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
        // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
        // increase the touch slop to make it harder to begin scrolling the workspace. This
        // results in vertically scrolling widgets to more easily. The higher the angle, the
        // more we increase touch slop.
        theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
        float extraRatio = (float) Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
        super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
    } else {
        // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
        super.determineScrollingStart(ev);
    }
}

From source file:com.android.launcher3.Workspace.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mXDown = ev.getX();
        mYDown = ev.getY();/*www.  j  av  a 2  s  . c om*/
        mTouchDownTime = System.currentTimeMillis();
        break;
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_REST) {
            final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
            if (currentPage != null && !currentPage.lastDownOnOccupiedCell()) {
                onWallpaperTap(ev);
            }
        }
    }
    return super.onInterceptTouchEvent(ev);
}

From source file:cn.d.fesa.wuf.ui.view.LazyViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*  w ww. j  av  a 2  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.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            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 - mLastMotionY);
        final int scrollX = getScrollX();
        final boolean atEdge = (dx > 0 && scrollX == 0)
                || (dx < 0 && mAdapter != null && scrollX >= (mAdapter.getCount() - 1) * getWidth() - 1);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            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.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

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

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        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:bw.com.yunifangstore.view.OrderLazyViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    /*/*from   w w  w  . j a  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.
     */

    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.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            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 - mLastMotionY);
        final int scrollX = getScrollX();
        final boolean atEdge = (dx > 0 && scrollX == 0)
                || (dx < 0 && mAdapter != null && scrollX >= (mAdapter.getCount() - 1) * getWidth() - 1);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            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.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

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

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        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.baidu.zhuanche.view.LazyViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from ww w.j a  v  a 2 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.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        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) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            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 - mLastMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            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.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

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

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

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

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        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;
}