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

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

Introduction

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

Prototype

public static float getX(MotionEvent event, int pointerIndex) 

Source Link

Document

Call MotionEvent#getX(int) .

Usage

From source file:com.lowworker.android.views.custom_views.photo.PhotoViewPager.java

/**
 * {@inheritDoc}/*from   w ww  .  j  av a  2 s  .com*/
 * <p/>
 * We intercept touch event intercepts so we can prevent switching views when the
 * current view is internally scrollable.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final InterceptType intercept = (mListener != null) ? mListener.onTouchIntercept(mActivatedX, mActivatedY)
            : InterceptType.NONE;

    final boolean ignoreScrollLeft = (intercept == InterceptType.BOTH || intercept == InterceptType.LEFT);
    final boolean ignoreScrollRight = (intercept == InterceptType.BOTH || intercept == InterceptType.RIGHT);

    // Only check ability to page if we can't scroll in one / both directions
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mActivePointerId = INVALID_POINTER;
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        if (ignoreScrollLeft || ignoreScrollRight) {
            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);

            if (ignoreScrollLeft && ignoreScrollRight) {
                mLastMotionX = x;
                return false;
            } else if (ignoreScrollLeft && (x > mLastMotionX)) {
                mLastMotionX = x;
                return false;
            } else if (ignoreScrollRight && (x < mLastMotionX)) {
                mLastMotionX = x;
                return false;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        mLastMotionX = ev.getX();
        // Use the raw x/y as the children can be located anywhere and there isn't a
        // single offset that would be meaningful
        mActivatedX = ev.getRawX();
        mActivatedY = ev.getRawY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            // Our active pointer going up; select a new active pointer
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        break;
    }
    }

    return super.onInterceptTouchEvent(ev);
}

From source file:com.lee.sdk.widget.viewpager.DrawablePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//w ww  . ja v a  2s  . c o m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            //                    final int count = mViewPager.getAdapter().getCount();
            //                    final int width = getWidth();
            //                    final float halfWidth = width / 2f;
            //                    final float sixthWidth = width / 6f;
            //
            //                    if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
            //                        if (action != MotionEvent.ACTION_CANCEL) {
            //                            mViewPager.setCurrentItem(mCurrentPage - 1);
            //                        }
            //                        return true;
            //                    } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
            //                        if (action != MotionEvent.ACTION_CANCEL) {
            //                            mViewPager.setCurrentItem(mCurrentPage + 1);
            //                        }
            //                        return true;
            //                    }
            if (action != MotionEvent.ACTION_CANCEL) {
                final int count = mViewPager.getAdapter().getCount();
                final int width = getWidth();
                final int pageWidth = width / count;
                int downPage = (int) (ev.getX() / pageWidth);
                if (downPage != mCurrentPage) {
                    mViewPager.setCurrentItem(downPage);
                    return true;
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

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

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

    return true;
}

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

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

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

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

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

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

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

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

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

            }

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

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

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

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

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

From source file:com.tweetlanes.android.core.widget.viewpagerindicator.UnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from  www  . j  av  a  2 s.  c om*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage - 1);
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage + 1);
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

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

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

    return true;
}

From source file:cc.solart.turbo.TurboRecyclerView.java

private int getMotionEventX(MotionEvent e, int pointerIndex) {
    return (int) (MotionEventCompat.getX(e, pointerIndex) + 0.5f);
}

From source file:com.shafiq.mytwittle.widget.viewpagerindicator.UnderlinePageIndicator.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from  w w w  . j  a v  a2s  . co m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage - 1);
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage + 1);
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

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

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

    return true;
}

From source file:com.student.oclass.view.pagerindicator.UnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/* ww  w . j av a2 s.com*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage - 1);
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                mViewPager.setCurrentItem(mCurrentPage + 1);
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

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

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

    return true;
}

From source file:de.uni_weimar.mheinz.androidtouchscope.display.MeasurementsView.java

private boolean hitTest(MotionEvent event) {
    final int pointerIndex = MotionEventCompat.getActionIndex(event);
    final float x = MotionEventCompat.getX(event, pointerIndex);
    final float y = MotionEventCompat.getY(event, pointerIndex);

    boolean selected = false;
    if (x >= mTopLeft.x && x <= mTopLeft.x + BOX_WIDTH && y >= mTopLeft.y && y <= mTopLeft.y + BOX_HEIGHT) {
        selected = true;/*from   w  w w.j av  a 2s . c  o  m*/
    }

    return selected;
}

From source file:com.lyric.indicator.UnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w  w  w .ja v  a 2 s  .c  o m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;
        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }
        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }
        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    return true;
}

From source file:de.vanita5.twittnuker.view.LinePageIndicator.java

@Override
public boolean onTouchEvent(final android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev))
        return true;
    if (mViewPager == null || mViewPager.getAdapter().getCount() == 0)
        return false;

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();//from   w w w .j  a v a 2 s.  c om
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if (mCurrentPage > 0 && ev.getX() < halfWidth - sixthWidth) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if (mCurrentPage < count - 1 && ev.getX() > halfWidth + sixthWidth) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            mViewPager.endFakeDrag();
        }
        break;

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

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

    return true;
}