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

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

Introduction

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

Prototype

public static int getPointerId(MotionEvent event, int pointerIndex) 

Source Link

Document

Call MotionEvent#getPointerId(int) .

Usage

From source file:com.coleman.demo.view.page.DirectionalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

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

    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();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = mInitialMotion = ev.getX();
        } else {
            mLastMotionY = mInitialMotion = 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 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;
                if (mOrientation == HORIZONTAL) {
                    mLastMotionX = x;
                } else {
                    mLastMotionY = y;
                }
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);

            int size;
            float scroll;

            if (mOrientation == HORIZONTAL) {
                size = getWidth();
                scroll = getScrollX() + (mLastMotionX - x);
                mLastMotionX = x;
            } else {
                size = getHeight();
                scroll = getScrollY() + (mLastMotionY - y);
                mLastMotionY = y;
            }

            final float lowerBound = Math.max(0, (mCurItem - 1) * size);
            final float upperBound = Math.min(mCurItem + 1, mAdapter.getCount() - 1) * size;
            if (scroll < lowerBound) {
                scroll = lowerBound;
            } else if (scroll > upperBound) {
                scroll = upperBound;
            }
            if (mOrientation == HORIZONTAL) {
                // Don't lose the rounded component
                mLastMotionX += scroll - (int) scroll;
                scrollTo((int) scroll, getScrollY());
            } else {
                // Don't lose the rounded component
                mLastMotionY += scroll - (int) scroll;
                scrollTo(getScrollX(), (int) scroll);
            }
            if (mOnPageChangeListener != null) {
                final int position = (int) scroll / size;
                final int positionOffsetPixels = (int) scroll % size;
                final float positionOffset = (float) positionOffsetPixels / size;
                mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity;
            float lastMotion;
            int sizeOverThree;

            if (mOrientation == HORIZONTAL) {
                initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionX;
                sizeOverThree = getWidth() / 3;
            } else {
                initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionY;
                sizeOverThree = getHeight() / 3;
            }

            mPopulatePending = true;
            if ((Math.abs(initialVelocity) > mMinimumVelocity)
                    || Math.abs(mInitialMotion - lastMotion) >= sizeOverThree) {
                if (lastMotion > mInitialMotion) {
                    setCurrentItemInternal(mCurItem - 1, true, true);
                } else {
                    setCurrentItemInternal(mCurItem + 1, true, true);
                }
            } else {
                setCurrentItemInternal(mCurItem, true, true);
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = MotionEventCompat.getX(ev, index);
        } else {
            mLastMotionY = MotionEventCompat.getY(ev, index);
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = MotionEventCompat.getX(ev, index);
        } else {
            mLastMotionY = MotionEventCompat.getY(ev, index);
        }
        break;
    }
    return true;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }/*from  w  w  w . ja  va2 s. c o  m*/

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

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

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = mInitialMotion = ev.getX();
        } else {
            mLastMotionY = mInitialMotion = 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 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;
                if (mOrientation == HORIZONTAL) {
                    mLastMotionX = x;
                } else {
                    mLastMotionY = y;
                }
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);

            int size;
            float scroll;

            if (mOrientation == HORIZONTAL) {
                size = getWidth();
                scroll = getScrollX() + (mLastMotionX - x);
                mLastMotionX = x;
            } else {
                size = getHeight();
                scroll = getScrollY() + (mLastMotionY - y);
                mLastMotionY = y;
            }

            final float lowerBound = Math.max(0, (mCurItem - 1) * size);
            final float upperBound = Math.min(mCurItem + 1, mAdapter.getCount() - 1) * size;
            if (scroll < lowerBound) {
                scroll = lowerBound;
            } else if (scroll > upperBound) {
                scroll = upperBound;
            }
            if (mOrientation == HORIZONTAL) {
                // Don't lose the rounded component
                mLastMotionX += scroll - (int) scroll;
                scrollTo((int) scroll, getScrollY());
            } else {
                // Don't lose the rounded component
                mLastMotionY += scroll - (int) scroll;
                scrollTo(getScrollX(), (int) scroll);
            }
            if (mOnPageChangeListener != null) {
                final int position = (int) scroll / size;
                final int positionOffsetPixels = (int) scroll % size;
                final float positionOffset = (float) positionOffsetPixels / size;
                mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity;
            float lastMotion;
            int sizeOverThree;

            if (mOrientation == HORIZONTAL) {
                initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionX;
                sizeOverThree = getWidth() / 3;
            } else {
                initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionY;
                sizeOverThree = getHeight() / 3;
            }

            mPopulatePending = true;
            if ((Math.abs(initialVelocity) > mMinimumVelocity)
                    || Math.abs(mInitialMotion - lastMotion) >= sizeOverThree) {
                if (lastMotion > mInitialMotion) {
                    setCurrentItemInternal(mCurItem - 1, true, true);
                } else {
                    setCurrentItemInternal(mCurItem + 1, true, true);
                }
            } else {
                setCurrentItemInternal(mCurItem, true, true);
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = MotionEventCompat.getX(ev, index);
        } else {
            mLastMotionY = MotionEventCompat.getY(ev, index);
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = MotionEventCompat.getX(ev, index);
        } else {
            mLastMotionY = MotionEventCompat.getY(ev, index);
        }
        break;
    }
    return true;
}

From source file:com.kectech.android.wyslink.thirdparty.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;//from  www. j  av a 2  s.co m
    }

    switch (mDirection) {
    case BOTTOM:
        if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    case NONE:
        return false;
    case TOP:
    default:
        if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    }

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

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);

        float overscrollTop;
        switch (mDirection) {
        case BOTTOM:
            overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
            break;
        case TOP:
        default:
            overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            break;
        }
        if (mIsBeingDragged) {
            mProgress.showArrow(true);
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                    : mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            // int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            int targetY;
            if (mDirection == SwipeRefreshLayoutDirection.TOP) {
                targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            } else {
                targetY = mOriginalOffsetTop - (int) ((slingshotDist * dragPercent) + extraMove);
            }
            // where 1.0f is a full circle
            if (mCircleView.getVisibility() != View.VISIBLE) {
                mCircleView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mCircleView, 1f);
                ViewCompat.setScaleY(mCircleView, 1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
                float strokeStart = (float) (adjustedPercent * .8f);
                mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                mProgress.setArrowScale(Math.min(1f, adjustedPercent));
            } else {
                if (mProgress.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                    // Animate the alpha
                    startProgressAlphaMaxAnimation();
                }
            }
            float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
            mProgress.setProgressRotation(rotation);
            setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

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

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        float overscrollTop;
        switch (mDirection) {
        case BOTTOM:
            overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
            break;
        case TOP:
        default:
            overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            break;
        }
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mProgress.setStartEndTrim(0f, 0f);
            AnimationListener listener = null;
            if (!mScale) {
                listener = new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (!mScale) {
                            startScaleDownAnimation(null);
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                };
            }
            animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
            mProgress.showArrow(false);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}

From source file:com.bison.app.ui.refresh.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/* w  ww  . ja  va2 s  .  co  m*/
    }

    switch (mDirection) {
    case BOTTOM:
        if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    case TOP:
    default:
        if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    }

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

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);

        float overscrollTop;
        switch (mDirection) {
        case BOTTOM:
            overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
            break;
        case TOP:
        default:
            overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            break;
        }
        if (mIsBeingDragged) {
            mProgress.showArrow(true);
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                    : mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            // int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            int targetY;
            if (mDirection == SwipeMode.TOP) {
                targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            } else {
                targetY = mOriginalOffsetTop - (int) ((slingshotDist * dragPercent) + extraMove);
            }
            // where 1.0f is a full circle
            if (mCircleView.getVisibility() != View.VISIBLE) {
                mCircleView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mCircleView, 1f);
                ViewCompat.setScaleY(mCircleView, 1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
                float strokeStart = (float) (adjustedPercent * .8f);
                mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                mProgress.setArrowScale(Math.min(1f, adjustedPercent));
            } else {
                if (mProgress.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                    // Animate the alpha
                    startProgressAlphaMaxAnimation();
                }
            }
            float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
            mProgress.setProgressRotation(rotation);
            setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

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

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        float overscrollTop;
        switch (mDirection) {
        case BOTTOM:
            overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
            break;
        case TOP:
        default:
            overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            break;
        }
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mProgress.setStartEndTrim(0f, 0f);
            AnimationListener listener = null;
            if (!mScale) {
                listener = new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (!mScale) {
                            startScaleDownAnimation(null);
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                };
            }
            animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
            mProgress.showArrow(false);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}

From source file:cn.ieclipse.af.view.refresh.SwipyRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    try {//from  www  .  j  a  v a  2  s.c  o m
        final int action = MotionEventCompat.getActionMasked(ev);

        if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
            mReturningToStart = false;
        }

        switch (mDirection) {
        case BOTTOM:
            if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing) {
                // Fail fast if we're not in a state where a swipe is possible
                return false;
            }
            break;
        case TOP:
        default:
            if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
                // Fail fast if we're not in a state where a swipe is possible
                return false;
            }
            break;
        }

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

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }

            final float y = MotionEventCompat.getY(ev, pointerIndex);

            float overscrollTop;
            switch (mDirection) {
            case BOTTOM:
                overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
                break;
            case TOP:
            default:
                overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
                break;
            }
            if (mIsBeingDragged) {
                mProgress.showArrow(true);
                float originalDragPercent = overscrollTop / mTotalDragDistance;
                if (originalDragPercent < 0) {
                    return false;
                }
                float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
                float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
                float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
                float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                        : mSpinnerFinalOffset;
                float tensionSlingshotPercent = Math.max(0,
                        Math.min(extraOS, slingshotDist * 2) / slingshotDist);
                float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                        - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
                float extraMove = (slingshotDist) * tensionPercent * 2;

                // int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
                int targetY;
                if (mDirection == SwipyRefreshLayoutDirection.TOP) {
                    targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
                } else {
                    targetY = mOriginalOffsetTop - (int) ((slingshotDist * dragPercent) + extraMove);
                }
                // where 1.0f is a full circle
                if (mCircleView.getVisibility() != View.VISIBLE) {
                    mCircleView.setVisibility(View.VISIBLE);
                }
                if (!mScale) {
                    ViewCompat.setScaleX(mCircleView, 1f);
                    ViewCompat.setScaleY(mCircleView, 1f);
                }
                if (overscrollTop < mTotalDragDistance) {
                    if (mScale) {
                        setAnimationProgress(overscrollTop / mTotalDragDistance);
                    }
                    if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                            && !isAnimationRunning(mAlphaStartAnimation)) {
                        // Animate the alpha
                        startProgressAlphaStartAnimation();
                    }
                    float strokeStart = (float) (adjustedPercent * .8f);
                    mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                    mProgress.setArrowScale(Math.min(1f, adjustedPercent));
                } else {
                    if (mProgress.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                        // Animate the alpha
                        startProgressAlphaMaxAnimation();
                    }
                }
                float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
                mProgress.setProgressRotation(rotation);
                setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
            }
            break;
        }
        case MotionEventCompat.ACTION_POINTER_DOWN: {
            final int index = MotionEventCompat.getActionIndex(ev);
            mActivePointerId = MotionEventCompat.getPointerId(ev, index);
            break;
        }

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

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            if (mActivePointerId == INVALID_POINTER) {
                if (action == MotionEvent.ACTION_UP) {
                }
                return false;
            }
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, pointerIndex);

            float overscrollTop;
            switch (mDirection) {
            case BOTTOM:
                overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
                break;
            case TOP:
            default:
                overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
                break;
            }
            mIsBeingDragged = false;
            if (overscrollTop > mTotalDragDistance) {
                setRefreshing(true, true /* notify */);
            } else {
                // cancel refresh
                mRefreshing = false;
                mProgress.setStartEndTrim(0f, 0f);
                AnimationListener listener = null;
                if (!mScale) {
                    listener = new AnimationListener() {

                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (!mScale) {
                                startScaleDownAnimation(null);
                            }
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                    };
                }
                animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
                mProgress.showArrow(false);
            }
            mActivePointerId = INVALID_POINTER;
            return false;
        }
        }
    } catch (Exception e) {
        Log.e(TAG, "An exception occured during SwipyRefreshLayout onTouchEvent " + e.toString());
    }

    return true;
}

From source file:com.open.ui.viewpager.view.TabViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//w w  w. ja  va2s.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.
     */

    // If mDisableIntercept true, pass the event to child view
    //Log.i("setOnTouchListener","view_pager mDisableIntercept"+mDisableIntercept);
    if (mDisableIntercept) {
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

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

        if (xDiff > mTouchSlop && xDiff > yDiff) {
            //if (DEBUG) Log.v(TAG, "onInterceptTouchEvent 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.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> MotionEventCompat.ACTION_POINTER_INDEX_SHIFT;
    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.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();//from w  w w  . j a  va 2 s  .co m
        }
    }
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override/*from w w w  .ja  va 2  s .  com*/
public boolean onGenericMotionEvent(MotionEvent event) {
    if (event.getEventTime() + EVENT_THRESHOLD_DECAY < SystemClock.uptimeMillis()) {
        //Log.i("DosBoxTurbo","eventtime: "+event.getEventTime() + " systemtime: "+SystemClock.uptimeMillis());
        return true; // get rid of old events
    }
    final int pointerIndex = MotionEventCompat.getActionIndex(event);
    final int pointerId = MotionEventCompat.getPointerId(event, pointerIndex);

    if ((MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_MOVE) && ((mWrap.getSource(event)
            & TouchEventWrapper.SOURCE_CLASS_MASK) == TouchEventWrapper.SOURCE_CLASS_JOYSTICK)) {
        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) && (mAnalogStickPref < 3)) {
            // use new 3.1 API to handle joystick movements
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                processJoystickInput(event, i);
            }

            processJoystickInput(event, -1);
            return true;
        } else {
            // use older 2.2+ API to handle joystick movements
            if (mInputMode == INPUT_MODE_REAL_JOYSTICK) {
                x[pointerId] = mWrap.getX(event, pointerId);
                y[pointerId] = mWrap.getY(event, pointerId);
                DosBoxControl.nativeJoystick((int) ((x[pointerId] * 256f) + mJoyCenterX),
                        (int) ((y[pointerId] * 256f) + mJoyCenterY), ACTION_MOVE, -1);
                return true;
            }
        }
    } else if ((MotionEventCompat.getActionMasked(event) == MotionEventCompat.ACTION_HOVER_MOVE)
            && ((mWrap.getSource(event)
                    & TouchEventWrapper.SOURCE_CLASS_MASK) == TouchEventWrapper.SOURCE_CLASS_POINTER)) {
        if (mInputMode == INPUT_MODE_REAL_MOUSE) {
            x_last[pointerId] = x[pointerId];
            y_last[pointerId] = y[pointerId];
            x[pointerId] = mWrap.getX(event, pointerId);
            y[pointerId] = mWrap.getY(event, pointerId);
            if (mAbsolute) {
                DosBoxControl.nativeMouseWarp(x[pointerId], y[pointerId], mRenderer.x, mRenderer.y,
                        mRenderer.width, mRenderer.height);
            } else {
                DosBoxControl.nativeMouse((int) (x[pointerId] * mMouseSensitivityX),
                        (int) (y[pointerId] * mMouseSensitivityY),
                        (int) (x_last[pointerId] * mMouseSensitivityX),
                        (int) (y_last[pointerId] * mMouseSensitivityY), 2, -1);
            }

            int buttonState = mWrap.getButtonState(event);
            if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) != 0) && !mSPenButton) {
                // Handle Samsung SPen Button (RMB) - DOWN
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_DOWN, BTN_B);
                mSPenButton = true;
            } else if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) == 0) && mSPenButton) {
                // Handle Samsung SPen Button (RMB) - UP
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_UP, BTN_B);
                mSPenButton = false;
            }

            if (mDebug)
                Log.d("DosBoxTurbo", "onGenericMotionEvent() INPUT_MODE_REAL_MOUSE x: " + x[pointerId] + "  y: "
                        + y[pointerId] + "  |  xL: " + x_last[pointerId] + "  yL: " + y_last[pointerId]);
            try {
                if (!mInputLowLatency)
                    Thread.sleep(95);
                else
                    Thread.sleep(65);
            } catch (InterruptedException e) {
            }
            return true;
        }
    } else if (MotionEventCompat.getActionMasked(event) == MotionEventCompat.ACTION_HOVER_EXIT) {
        if (mInputMode == INPUT_MODE_REAL_MOUSE) {
            // hover exit
            int buttonState = mWrap.getButtonState(event);
            if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) == 0) && mSPenButton) {
                // Handle Samsung SPen Button (RMB) - UP
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_UP, BTN_B);
                mSPenButton = false;
                return true;
            }
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        return super.onGenericMotionEvent(event);
    } else {
        return false;
    }
}

From source file:com.harry.refresh.SwipyRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    try {//w  w  w  .ja v  a  2s.c  om
        final int action = MotionEventCompat.getActionMasked(ev);

        if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
            mReturningToStart = false;
        }

        switch (mDirection) {
            case BOTTOM:
                if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing) {
                    // Fail fast if we're not in a state where a swipe is possible
                    return false;
                }
                break;
            case TOP:
            default:
                if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
                    // Fail fast if we're not in a state where a swipe is possible
                    return false;
                }
                break;
        }

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

            case MotionEvent.ACTION_MOVE: {
                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                if (pointerIndex < 0) {
                    return false;
                }

                final float y = MotionEventCompat.getY(ev, pointerIndex);

                float overscrollTop;
                switch (mDirection) {
                    case BOTTOM:
                        overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
                        break;
                    case TOP:
                    default:
                        overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
                        break;
                }
                if (mIsBeingDragged) {
                    mProgress.showArrow(true);
                    float originalDragPercent = overscrollTop / mTotalDragDistance;
                    if (originalDragPercent < 0) {
                        return false;
                    }
                    float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
                    float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
                    float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
                    float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset
                            - mOriginalOffsetTop : mSpinnerFinalOffset;
                    float tensionSlingshotPercent = Math.max(0,
                            Math.min(extraOS, slingshotDist * 2) / slingshotDist);
                    float tensionPercent = (float) ((tensionSlingshotPercent / 4) - Math.pow(
                            (tensionSlingshotPercent / 4), 2)) * 2f;
                    float extraMove = (slingshotDist) * tensionPercent * 2;

                    // int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
                    int targetY;
                    if (mDirection == SwipeRefreshLayoutDirection.TOP) {
                        targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
                    } else {
                        targetY = mOriginalOffsetTop - (int) ((slingshotDist * dragPercent) + extraMove);
                    }
                    // where 1.0f is a full circle
                    if (mCircleView.getVisibility() != View.VISIBLE) {
                        mCircleView.setVisibility(View.VISIBLE);
                    }
                    if (!mScale) {
                        ViewCompat.setScaleX(mCircleView, 1f);
                        ViewCompat.setScaleY(mCircleView, 1f);
                    }
                    if (overscrollTop < mTotalDragDistance) {
                        if (mScale) {
                            setAnimationProgress(overscrollTop / mTotalDragDistance);
                        }
                        if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                                && !isAnimationRunning(mAlphaStartAnimation)) {
                            // Animate the alpha
                            startProgressAlphaStartAnimation();
                        }
                        float strokeStart = (float) (adjustedPercent * .8f);
                        mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                        mProgress.setArrowScale(Math.min(1f, adjustedPercent));
                    } else {
                        if (mProgress.getAlpha() < MAX_ALPHA
                                && !isAnimationRunning(mAlphaMaxAnimation)) {
                            // Animate the alpha
                            startProgressAlphaMaxAnimation();
                        }
                    }
                    float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
                    mProgress.setProgressRotation(rotation);
                    setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop,
                            true /* requires update */);
                }
                break;
            }
            case MotionEventCompat.ACTION_POINTER_DOWN: {
                final int index = MotionEventCompat.getActionIndex(ev);
                mActivePointerId = MotionEventCompat.getPointerId(ev, index);
                break;
            }

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

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                if (mActivePointerId == INVALID_POINTER) {
                    if (action == MotionEvent.ACTION_UP) {
                    }
                    return false;
                }
                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                final float y = MotionEventCompat.getY(ev, pointerIndex);

                float overscrollTop;
                switch (mDirection) {
                    case BOTTOM:
                        overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
                        break;
                    case TOP:
                    default:
                        overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
                        break;
                }
                mIsBeingDragged = false;
                if (overscrollTop > mTotalDragDistance) {
                    setRefreshing(true, true /* notify */);
                } else {
                    // cancel refresh
                    mRefreshing = false;
                    mProgress.setStartEndTrim(0f, 0f);
                    AnimationListener listener = null;
                    if (!mScale) {
                        listener = new AnimationListener() {

                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                if (!mScale) {
                                    startScaleDownAnimation(null);
                                }
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                            }

                        };
                    }
                    animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
                    mProgress.showArrow(false);
                }
                mActivePointerId = INVALID_POINTER;
                return false;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "An exception occured during SwipyRefreshLayout onTouchEvent " + e.toString());
    }

    return true;
}

From source file:com.icloud.listenbook.base.view.DraggableGridViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong
        // to one of our
        // descendants.
        return false;
    }/* ww w  . j  av a2s  .com*/

    if (mPageCount <= 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();
        // Remember where the motion event started
        // ??
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);
        //  ? 
        if (!mIsBeingDragged && mScrollState == SCROLL_STATE_IDLE) {
            mLastPosition = getPositionByXY((int) mLastMotionX, (int) mLastMotionY);
        } else {
            mLastPosition = -1;
        }

        // 
        if (mLastPosition >= 0) {
            mLastDownTime = System.currentTimeMillis();
        } else {
            mLastDownTime = Long.MAX_VALUE;
        }
        DEBUG_LOG("Down at mLastPosition=" + mLastPosition);
        mLastDragged = -1;
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        // ?
        if (mLastDragged >= 0) {
            // change draw location of dragged visual
            // ? ?
            final View v = getChildAt(mLastDragged);
            final int l = getScrollX() + (int) x - v.getWidth() / 2;
            final int t = getScrollY() + (int) y - v.getHeight() / 2;
            v.layout(l, t, l + v.getWidth(), t + v.getHeight());

            // check for new target hover
            // 
            if (mScrollState == SCROLL_STATE_IDLE) {
                final int target = getTargetByXY((int) x, (int) y);
                // ?? ??
                if (target != -1 && mLastTarget != target) {
                    animateGap(target);
                    mLastTarget = target;
                    DEBUG_LOG("Moved to mLastTarget=" + mLastTarget);
                }
                // edge holding
                /**
                 * ? 
                 * */
                final int edge = getEdgeByXY((int) x, (int) y);
                if (mLastEdge == -1) {
                    if (edge != mLastEdge) {
                        mLastEdge = edge;
                        mLastEdgeTime = System.currentTimeMillis();
                    }
                } else {
                    // ?? 1.2 ?
                    if (edge != mLastEdge) {
                        mLastEdge = -1;
                    } else {
                        if ((System.currentTimeMillis() - mLastEdgeTime) >= EDGE_HOLD_DURATION) {
                            // ???
                            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                            triggerSwipe(edge);
                            mLastEdge = -1;
                        }
                    }
                }
            }
        }
        // ?
        else if (!mIsBeingDragged) {
            final float xDiff = Math.abs(x - mLastMotionX);
            final float yDiff = Math.abs(y - mLastMotionY);
            DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            // ?? ???
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                DEBUG_LOG("Starting drag!");
                mIsBeingDragged = true;
                requestParentDisallowInterceptTouchEvent(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
            // ?
            needsInvalidate |= performDrag(x);
        }
        // ??? ?
        else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            DEBUG_LOG("Moved to currentPosition=" + currentPosition);
            // ?
            if (currentPosition == mLastPosition) {

                // 1S?
                if ((System.currentTimeMillis() - mLastDownTime) >= LONG_CLICK_DURATION) {
                    if (onItemLongClick(currentPosition)) {
                        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                        mLastDragged = mLastPosition;
                        requestParentDisallowInterceptTouchEvent(true);
                        mLastTarget = -1;
                        animateDragged();
                        mLastPosition = -1;
                    }
                    mLastDownTime = Long.MAX_VALUE;
                }
            } else {
                mLastPosition = -1;
            }
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        DEBUG_LOG("Touch up!!!");
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        //  ?
        if (mLastDragged >= 0) {
            rearrange();
        } else if
        //  ??
        (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int width = getWidth();
            final int scrollX = getScrollX();
            final int currentPage = scrollX / width;
            final int offsetPixels = scrollX - currentPage * width;
            final float pageOffset = (float) offsetPixels / (float) width;
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            DEBUG_LOG("Touch up!!! currentPosition=" + currentPosition);
            if (currentPosition == mLastPosition) {
                onItemClick(currentPosition);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
        DEBUG_LOG("Touch cancel!!!");
        //  ? 
        if (mLastDragged >= 0) {
            rearrange();
        } else
        // ???
        if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        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;
}