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

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

Introduction

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

Prototype

public static float getY(MotionEvent event, int pointerIndex) 

Source Link

Document

Call MotionEvent#getY(int) .

Usage

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

/**
 *  on secondary pointer up/* w  w  w .  ja  va  2  s  . c om*/
 * This was our active pointer going up. Choose a new active pointer and adjust accordingly.
 * @param ev MotionEvent
 */
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionY = MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        } /* end of if */
    } /* end of if */
}

From source file:cn.trinea.android.common.view.CycleViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from w  w w  . ja va2s .  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;
        }
        ///LEWA BEGIN
        mSetLongClickFlag = false; //zhangwei lewa
        ///LEWA END
        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.
        */
        ///LEWA BEGIN

        //zhangwei lewa add
        if (mSetLongClickFlag == true) {
            //on long click event
            return false;
        }
        //zhangwei add
        ///LEWA END

        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);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (dx != 0 && !isGutterDrag(mLastMotionX, dx) && canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mLastMotionX = x;
            mLastMotionY = y;
            mIsUnableToDrag = true;
            return false;
        }

        /// M: skipping it to child for spec rectangle and touchSlop.
        if (xDiff < mTouchSlopAdjust && yDiff < mTouchSlopAdjust && pointInRect(ev, mSpecRect)) {
            Log.d(TAG, "xDiff = " + xDiff + ", yDiff = " + yDiff + ", mTouchSlopAdj = " + mTouchSlopAdjust);
            return false;
        } else if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = 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.
            if (DEBUG)
                Log.v(TAG, "Starting 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();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }/* www  . j ava2  s. c  om*/

    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;
    }

    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 & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        mPopulatePending = false;
        populate();
        mIsBeingDragged = true;
        setScrollState(SCROLL_STATE_DRAGGING);

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                mIsBeingDragged = true;
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                mLastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            needsInvalidate |= performDrag(x);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int width = getClientWidth();
            final int scrollX = getScrollX();
            final ItemInfo ii = infoForCurrentScrollPosition();
            final int currentPage = ii.position;
            final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

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

From source file:android.support.custom.view.VerticalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }/*w  w  w  .  j  a va2 s.  c om*/

    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;
    }

    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 & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        mPopulatePending = false;
        populate();
        mIsBeingDragged = true;
        setScrollState(SCROLL_STATE_DRAGGING);

        // Remember where the motion event started
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            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 (yDiff > mTouchSlop && yDiff > xDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionY = y - mInitialMotionY > 0 ? mInitialMotionY + mTouchSlop
                        : mInitialMotionY - mTouchSlop;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            needsInvalidate |= performDrag(y);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int height = getHeight();
            final int scrollY = getScrollY();
            final ItemInfo ii = infoForCurrentScrollPosition();
            final int currentPage = ii.position;
            final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final int totalDelta = (int) (y - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float y = MotionEventCompat.getY(ev, index);
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}

From source file:com.ex.lib.ext.widget.verticalviewpager.VerticalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }/*from w  w w . j  a  va  2s . c  o  m*/

    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;
    }

    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 & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        mPopulatePending = false;
        populate();
        mIsBeingDragged = true;
        setScrollState(SCROLL_STATE_DRAGGING);

        // Remember where the motion event started
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            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 (yDiff > mTouchSlop && yDiff > xDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionY = y - mInitialMotionY > 0 ? mInitialMotionY + mTouchSlop
                        : mInitialMotionY - mTouchSlop;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        //                if (mIsBeingDragged) {
        //                    // Scroll to follow the motion event
        //                    final int activePointerIndex = MotionEventCompat.findPointerIndex(
        //                            ev, mActivePointerId);
        //                    final float y = MotionEventCompat.getY(ev, activePointerIndex);
        //                    needsInvalidate |= performDrag(y);
        //                }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            //                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            //                    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
            //                            velocityTracker, mActivePointerId);
            int minimumVelocity = (int) getContext().getResources().getDisplayMetrics().heightPixels;
            velocityTracker.computeCurrentVelocity(minimumVelocity, minimumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int height = getHeight();
            final int scrollY = getScrollY();
            final ItemInfo ii = infoForCurrentScrollPosition();
            final int currentPage = ii.position;
            final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final int totalDelta = (int) (y - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float y = MotionEventCompat.getY(ev, index);
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}

From source file:com.jit.lib.widget.verticalviewpager.VerticalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }//from   w ww . j  a  v a2s.co  m

    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;
    }

    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 & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        mPopulatePending = false;
        populate();
        mIsBeingDragged = true;
        setScrollState(SCROLL_STATE_DRAGGING);

        // Remember where the motion event started
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            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 (yDiff > mTouchSlop && yDiff > xDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionY = y - mInitialMotionY > 0 ? mInitialMotionY + mTouchSlop
                        : mInitialMotionY - mTouchSlop;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        //                if (mIsBeingDragged) {
        //                    // Scroll to follow the motion event
        //                    final int activePointerIndex = MotionEventCompat.findPointerIndex(
        //                            ev, mActivePointerId);
        //                    final float y = MotionEventCompat.getY(ev, activePointerIndex);
        //                    needsInvalidate |= performDrag(y);
        //                }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            //                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            //                    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
            //                            velocityTracker, mActivePointerId);
            int minimumVelocity = (int) DensityUtil.getHeightInPx(getContext());
            velocityTracker.computeCurrentVelocity(minimumVelocity, minimumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int height = getHeight();
            final int scrollY = getScrollY();
            final ItemInfo ii = infoForCurrentScrollPosition();
            final int currentPage = ii.position;
            final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final int totalDelta = (int) (y - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float y = MotionEventCompat.getY(ev, index);
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}

From source file:com.bennyv5.smoothviewpager.SmoothViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*  w ww .  j  a  va  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!");
        resetTouch();
        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 - mInitialMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

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

        mIsScrollStarted = true;
        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();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        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:eu.kanade.tachiyomi.ui.reader.viewer.pager.vertical.VerticalViewPagerImpl.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from   w  w w.  j av  a2s. 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!");
        resetTouch();
        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 y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastMotionY;
        final float yDiff = Math.abs(dy);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float xDiff = Math.abs(x - mInitialMotionX);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (dy != 0 && !isGutterDrag(mLastMotionY, dy) && canScroll(this, false, (int) dy, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mLastMotionX = x;
            mLastMotionY = y;
            mIsUnableToDrag = true;
            return false;
        }
        if (yDiff > mTouchSlop && yDiff * 0.5f > xDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionY = dy > 0 ? mInitialMotionY + mTouchSlop : mInitialMotionY - mTouchSlop;
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
        } else if (xDiff > 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;
        }
        // Scroll to follow the motion event
        if (mIsBeingDragged && performDrag(y)) {
            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.getFinalY() - mScroller.getCurrY()) > mCloseEnough) {
            // Let the user 'catch' the pager as it animates.
            mScroller.abortAnimation();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

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

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    default:
        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:android.support.v7.widget.RecyclerViewEx.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    if (dispatchOnItemTouch(e)) {
        cancelTouch();/*from   ww w  .  ja  va  2s . c  om*/
        return true;
    }

    final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
    final boolean canScrollVertically = mLayout.canScrollVertically();

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

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);
    }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
    }
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
        if (index < 0) {
            Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
        }

        final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
        final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
        if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int dx = x - mInitialTouchX;
            final int dy = y - mInitialTouchY;
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
                mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
                startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
                mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
                startScroll = true;
            }
            if (startScroll) {
                getParent().requestDisallowInterceptTouchEvent(true);
                setScrollState(SCROLL_STATE_DRAGGING);
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final int dx = x - mLastTouchX;
            final int dy = y - mLastTouchY;
            scrollByInternal(canScrollHorizontally ? -dx : 0, canScrollVertically ? -dy : 0);
        }
        mLastTouchX = x;
        mLastTouchY = y;
    }
        break;

    case MotionEventCompat.ACTION_POINTER_UP: {
        onPointerUp(e);
    }
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
        final float xvel = canScrollHorizontally
                ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId)
                : 0;
        final float yvel = canScrollVertically
                ? -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId)
                : 0;
        if (!((xvel != 0 || yvel != 0) && fling((int) (xvel), (int) (yvel)))) {
            adjustPosition(mLastTouchX - mInitialTouchX);
        }
        mVelocityTracker.clear();
        releaseGlows();
    }
        break;

    case MotionEvent.ACTION_CANCEL: {
        cancelTouch();
    }
        break;
    }

    return true;
}

From source file:com.bahrini.pager.pager.RtlSupportViewPager.java

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

    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 - mInitialMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (dx != 0 && !isGutterDrag(mLastMotionX, dx) && canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mLastMotionX = x;
            mLastMotionY = y;
            mIsUnableToDrag = true;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
            if (DEBUG)
                Log.v(TAG, "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.
            if (DEBUG)
                Log.v(TAG, "Starting unable to drag!");
            mIsUnableToDrag = true;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            if (performDrag(x, mBlockedDirection)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mSwipeBlockedLastMotionX = ev.getX();
        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();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

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