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

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

Introduction

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

Prototype

public static int getActionIndex(MotionEvent event) 

Source Link

Document

Call MotionEvent#getAction , returning only the pointer index portion

Usage

From source file:com.example.googleplay.view.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*w  w w  .  jav a  2s . co 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:
        break;
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mLastDirection = Mode.DISABLED;
        return false;
    }

    return true;
}

From source file:com.zzti.fyg.widgets.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    try {//from ww w  .j a va2s .c om
        final int action = MotionEventCompat.getActionMasked(ev);

        if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {

            mReturningToStart = false;
        }

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

        if (mLoading) {
            return false;
        }

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

            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;

            if (up) { // ??
                if (down) { // ??
                    return false;
                } else {
                    if (-yDiff > mTouchSlop) { // 
                        direction = 1;
                        mIsBeingDragged = true;
                    }
                }
            } else {
                if (yDiff > mTouchSlop) {
                    direction = 0;
                    mIsBeingDragged = true;
                }
            }

            if (mIsBeingDragged) {
                if (direction == 0) {
                    // User velocity passed min velocity; trigger a refresh
                    if (yDiff > mDistanceToTriggerSync) {
                        state = RELEASE_TO_REFRESH;
                        defaultText = releaseDownLabel;

                    } else {
                        state = PULL_TO_REFRESH;
                        defaultText = pullDownLabel;
                    }
                    //                  if (latestRefreshTime.equals("")) {
                    //                     latestRefreshTime = formatter.format(new Date());
                    //                  }
                    latestRefreshTime = formatter.format(new Date());
                    setProgressBarProperty(mTarget.getTop());
                    updateContentOffsetTop((int) yDiff >> 1);
                } else {
                    if (-yDiff > mDistanceToTriggerSync) {
                        state = RELEASE_TO_REFRESH;
                        defaultText = releaseUpLabel;
                    } else {
                        state = PULL_TO_REFRESH;
                        defaultText = pullUpLabel;
                    }
                    //                  if (latestLoadTime.equals("")) {
                    //                     latestLoadTime = formatter.format(new Date());
                    //                  }

                    latestLoadTime = formatter.format(new Date());
                    setProgressBarProperty(mTarget.getTop());
                    updateContentOffsetBottom((int) yDiff >> 1);
                }
            }

            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:
            final int mPointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (mPointerIndex < 0) {
                Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
                return false;
            }

            final float mY = MotionEventCompat.getY(ev, mPointerIndex);
            final float mYDiff = mY - mInitialMotionY;

            if (!mIsBeingDragged && mYDiff > mTouchSlop) {
                mIsBeingDragged = true;
            }

            if (direction == 0) {
                if (mIsBeingDragged) {
                    if (mYDiff > mDistanceToTriggerSync) {
                        // User movement passed distance; trigger a refresh
                        startRefresh();
                    } else {
                        mReturnToStartPosition.run();
                    }
                }
            } else {
                if (mIsBeingDragged) {
                    if (-mYDiff > mDistanceToTriggerSync) {
                        // User movement passed distance; trigger a refresh
                        startLoad();
                    } else {
                        mReturnToStartPosition.run();
                    }
                }
            }

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

        return true;
    } catch (IllegalArgumentException e) {
        Log.e("error", e.getMessage());
        return false;
    }

}

From source file:com.yimi.onemeter.widget.support.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;// w w  w.  j a va2s  .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 false;
}

From source file:com.gmy.widget.swiperefresh.SwipeRefreshLayout.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new  
        // active pointer and adjust accordingly.  
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionY = MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
    }//from  ww w .  j  a  v a2  s  . c o  m
}

From source file:cn.org.eshow.framwork.view.slidingmenu.CustomViewAbove.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled)
        return false;

    if (!mIsBeingDragged && !thisTouchAllowed(ev))
        return false;

    //      if (!mIsBeingDragged && !mQuickReturn)
    //         return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//  www .  j  a  va 2 s .c o m
    mVelocityTracker.addMovement(ev);

    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
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        mLastMotionX = mInitialMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            determineDrag(ev);
            if (mIsUnableToDrag)
                return false;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId == INVALID_POINTER)
                break;
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final float leftBound = getLeftBound();
            final float rightBound = getRightBound();
            if (scrollX < leftBound) {
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int scrollX = getScrollX();
            //            final int widthWithMargin = getWidth();
            //            final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            // TODO test this. should get better flinging behavior
            final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId != INVALID_POINTER) {
                final float x = MotionEventCompat.getX(ev, activePointerIndex);
                final int totalDelta = (int) (x - mInitialMotionX);
                int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
                setCurrentItemInternal(nextPage, true, true, initialVelocity);
            } else {
                setCurrentItemInternal(mCurItem, true, true, initialVelocity);
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mQuickReturn
                && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
            // close the menu
            setCurrentItem(1);
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int indexx = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, indexx);
        mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER)
            break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
}

From source file:com.purezhihudaily.ui.widget.SwipeRefreshLayout.java

@SuppressLint({ "NewApi", "ClickableViewAccessibility" })
@Override/*from  w  w w . jav a2 s .c  o m*/
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) {
            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.hengye.swiperefresh.SwipeRefreshCustomLayout.java

@SuppressLint("ClickableViewAccessibility")
@Override/*from   w  w w  .  j a  v a  2s.co m*/
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;
}

From source file:io.amira.zen.layout.slider.ZenViewAbove.java

private void onSecondaryPointerUp(MotionEvent ev) {
    if (DEBUG)/*from  www.j av  a2  s  .co m*/
        ZenApplication.log.v("onSecondaryPointerUp called");
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}

From source file:com.hxqc.mall.core.views.CustomScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();//from w w  w. ja v  a 2 s  .c o m

    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) {
            DebugLog.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);
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            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.swiperefresh.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;// w  w  w  .  j a v  a 2  s  . co 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;
}