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.example.swiperefresh.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;//from  w  ww . ja  v a  2 s . c  o  m
    }

    if (!isEnabled() || mReturningToStart) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

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

        up = canChildScrollUp();
        down = canChildScrollDown();
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        //                final float yDiff = y - mInitialMotionY;
        final float yDiff = y - mStartPoint;

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

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

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

                }
                if ((mMode == Mode.PULL_FROM_START) || (mMode == Mode.BOTH)) {
                    mLastDirection = Mode.PULL_FROM_START;
                    startRefresh();
                }
            } else if (-yDiff > mDistanceToTriggerSync) {
                if ((!up && !down && !loadNoFull) || mLastDirection == Mode.PULL_FROM_START) {
                    return true;
                }
                if ((mMode == Mode.PULL_FROM_END) || (mMode == Mode.BOTH)) {
                    mLastDirection = Mode.PULL_FROM_END;
                    startLoad();
                }
            } else {
                if (!up && !down && yDiff < 0 && !loadNoFull) {
                    return true;
                }
                // Just track the user's movement
                //???????
                setTriggerPercentage(
                        mAccelerateInterpolator.getInterpolation(Math.abs(yDiff) / mDistanceToTriggerSync));
                updateContentOffsetTop((int) yDiff);
                if (mTarget.getTop() == getPaddingTop()) {
                    // If the user puts the view back at the top, we
                    // don't need to. This shouldn't be considered
                    // cancelling the gesture as the user can restart from the top.
                    removeCallbacks(mCancel);
                    mLastDirection = Mode.DISABLED;
                } else {
                    mDirection = (yDiff > 0 ? 1 : -1);
                    updatePositionTimeout();
                }
            }
            mLastMotionY = y;
        }
        break;

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

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

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mLastDirection = Mode.DISABLED;
        return false;
    }

    return true;
}

From source file:com.k.xdiary.views.MyNestedScrollView.java

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

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

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

    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

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

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

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

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

        if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
            flingWithNestedDispatch(-initialVelocity);
        } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
        //            }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

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

From source file:com.dystu.toolbar.widget.MySwipeRefreshLayout.java

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

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

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    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) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        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);
            // 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) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mProgress.setStartEndTrim(0f, 0f);
            Animation.AnimationListener listener = null;
            if (!mScale) {
                listener = new Animation.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.mucfc.refreshview.refresh.MySwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();//from w  ww .jav  a2 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 || (!mBothDirection && 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 || (!mBothDirection && 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:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialDownY = getMotionEventY(ev, mActivePointerId);
        if (initialDownY == -1) {
            return false;
        }
        mInitialDownY = initialDownY;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        if (mBothDirection) {
            if (y > mInitialDownY) {
                setRawDirection(MySwipeRefreshLayoutDirection.TOP);
            } else if (y < mInitialDownY) {
                setRawDirection(MySwipeRefreshLayoutDirection.BOTTOM);
            }
            if ((mDirection == MySwipeRefreshLayoutDirection.BOTTOM && canChildScrollDown())
                    || (mDirection == MySwipeRefreshLayoutDirection.TOP && canChildScrollUp())) {
                mInitialDownY = y;
                return false;
            }
        }
        float yDiff = 0;
        switch (mDirection) {
        case BOTTOM:
            yDiff = mInitialDownY - y;
            break;
        case TOP:
            yDiff = y - mInitialDownY;
            break;
        case NONE:
            break;
        default:
            yDiff = y - mInitialDownY;
            break;
        }
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            switch (mDirection) {
            case BOTTOM:
                mInitialMotionY = mInitialDownY - mTouchSlop;
                break;
            case TOP:
            default:
                mInitialMotionY = mInitialDownY + mTouchSlop;
                break;
            }
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

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

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

    return mIsBeingDragged;
}

From source file:com.hp.hoopeasy.widget.swipeRefresh.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/* w ww.ja va 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 || (!mBothDirection && 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 || (!mBothDirection && 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:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialDownY = getMotionEventY(ev, mActivePointerId);
        if (initialDownY == -1) {
            return false;
        }
        mInitialDownY = initialDownY;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        if (mBothDirection) {
            if (y > mInitialDownY) {
                setRawDirection(SwipeRefreshLayoutDirection.TOP);
            } else if (y < mInitialDownY) {
                setRawDirection(SwipeRefreshLayoutDirection.BOTTOM);
            }
            if ((mDirection == SwipeRefreshLayoutDirection.BOTTOM && canChildScrollDown())
                    || (mDirection == SwipeRefreshLayoutDirection.TOP && canChildScrollUp())) {
                mInitialDownY = y;
                return false;
            }
        }
        float yDiff;
        switch (mDirection) {
        case BOTTOM:
            yDiff = mInitialDownY - y;
            break;
        case TOP:
        default:
            yDiff = y - mInitialDownY;
            break;
        }
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            switch (mDirection) {
            case BOTTOM:
                mInitialMotionY = mInitialDownY - mTouchSlop;
                break;
            case TOP:
            default:
                mInitialMotionY = mInitialDownY + mTouchSlop;
                break;
            }
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

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

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

    return mIsBeingDragged;
}

From source file:com.fish.nsd.MyNestedScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/*  w w  w .j  av a  2s .c  om*/

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

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

    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

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

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

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

            final int scrolledDeltaY = getScrollY() - oldY;
            final int unconsumedY = deltaY - scrolledDeltaY;
            boolean consumedMove = false;
            if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
                if (mScrollOffset[1] > 0) {
                    consumedMove = true;
                }
            }

            if (canOverscroll && !consumedMove) {
                ensureGlows();
                final int pulledToY = oldY + deltaY;
                if (pulledToY < 0) {
                    if (glowTopEnable) {
                        mEdgeGlowTop.onPull((float) deltaY / getHeight(),
                                MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                        if (!mEdgeGlowBottom.isFinished()) {
                            mEdgeGlowBottom.onRelease();
                        }
                    } else {

                        //??child??parent?
                        MyNestedScrollView parent = (MyNestedScrollView) getParent().getParent();
                        parent.mEdgeGlowTop.onPull((float) deltaY / parent.getHeight(),
                                MotionEventCompat.getX(ev, activePointerIndex) / parent.getWidth());

                        if (parent.mEdgeGlowTop != null && (!parent.mEdgeGlowTop.isFinished())) {
                            ViewCompat.postInvalidateOnAnimation(parent);
                        }

                    }

                } else if (pulledToY > range) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
                            1.f - MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);

            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                flingWithNestedDispatch(-initialVelocity);
            } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

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

From source file:com.example.parking.refresh.RefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();//from   ww w .  j a  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 || (!mBothDirection && 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 || (!mBothDirection && 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:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialMotionY = getMotionEventY(ev, mActivePointerId);
        if (initialMotionY == -1) {
            return false;
        }
        mInitialMotionY = initialMotionY;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        if (mBothDirection) {
            if (y > mInitialMotionY) {
                setRawDirection(SwipeRefreshLayoutDirection.TOP);
            } else if (y < mInitialMotionY) {
                setRawDirection(SwipeRefreshLayoutDirection.BOTTOM);
            }
            if ((mDirection == SwipeRefreshLayoutDirection.BOTTOM && canChildScrollDown())
                    || (mDirection == SwipeRefreshLayoutDirection.TOP && canChildScrollUp())) {
                return false;
            }
        }
        float yDiff;
        switch (mDirection) {
        case BOTTOM:
            yDiff = mInitialMotionY - y;
            break;
        case TOP:
        default:
            yDiff = y - mInitialMotionY;
            break;
        }
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

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

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

    return mIsBeingDragged;
}

From source file:com.cheng.animationstudy.customview.googleimitatecode.SwipyRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*from ww  w.j  av  a 2  s . co  m*/

    final int action = MotionEventCompat.getActionMasked(ev);

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

    switch (mDirection) {
    case BOTTOM:
        if (!isEnabled() || mReturningToStart || (!mBothDirection && 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 || (!mBothDirection && 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:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialMotionY = getMotionEventY(ev, mActivePointerId);
        if (initialMotionY == -1) {
            return false;
        }
        mInitialMotionY = initialMotionY;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        if (mBothDirection) {
            if (y > mInitialMotionY) {
                setRawDirection(SwipyRefreshLayoutDirection.TOP);
            } else if (y < mInitialMotionY) {
                setRawDirection(SwipyRefreshLayoutDirection.BOTTOM);
            }
            if ((mDirection == SwipyRefreshLayoutDirection.BOTTOM && canChildScrollDown())
                    || (mDirection == SwipyRefreshLayoutDirection.TOP && canChildScrollUp())) {
                return false;
            }
        }
        float yDiff;
        switch (mDirection) {
        case BOTTOM:
            yDiff = mInitialMotionY - y;
            break;
        case TOP:
        default:
            yDiff = y - mInitialMotionY;
            break;
        }
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

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

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

    return mIsBeingDragged;
}

From source file:br.com.leoleal.swipetorefresh.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from   w w w . j  av a 2  s  .com*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    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) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        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);
            // where 1.0f is a full circle
            if (mCircleView.getVisibility() != View.VISIBLE) {
                mCircleView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                mCircleView.setScaleX(1f);
                mCircleView.setScaleY(1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
                float strokeStart = 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) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mProgress.setStartEndTrim(0f, 0f);
            Animation.AnimationListener listener = null;
            if (!mScale) {
                listener = new Animation.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.hengye.swiperefresh.SwipeRefreshCustomLayout.java

@SuppressLint("ClickableViewAccessibility")
@Override/*  ww w .j  a va2s  .  c om*/
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

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

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    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) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        if (mIsBeingDragged) {
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            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);
            // where 1.0f is a full circle
            if (mLoadingView.getVisibility() != View.VISIBLE) {
                mLoadingView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mLoadingView, 1f);
                ViewCompat.setScaleY(mLoadingView, 1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mLoadingView.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
            } else {
                if (mLoadingView.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                    // Animate the alpha
                    startProgressAlphaMaxAnimation();
                }
            }
            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) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            Animation.AnimationListener listener = null;
            if (!mScale) {
                listener = new Animation.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);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}