Example usage for android.support.v4.view VelocityTrackerCompat getXVelocity

List of usage examples for android.support.v4.view VelocityTrackerCompat getXVelocity

Introduction

In this page you can find the example usage for android.support.v4.view VelocityTrackerCompat getXVelocity.

Prototype

public static float getXVelocity(VelocityTracker tracker, int pointerId) 

Source Link

Document

Call VelocityTracker#getXVelocity(int) .

Usage

From source file:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.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  .  java 2s  .  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: {
        mScroller.abortAnimation();
        //             mPopulatePending = false;
        //                populate();

        //automatic H/V-Detection
        downX = ev.getX();
        downY = ev.getY();

        /*
         * 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 = mInitialMotionX = ev.getX();
        } else {
            mLastMotionY = mInitialMotionY = ev.getY();
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:

        //auto H/V-detection
        float moveX = ev.getX();
        float moveY = ev.getY();

        float deltaX = Math.abs(downX - moveX);
        float deltaY = Math.abs(downY - moveY);

        if (!isOrientationModeLocked) {
            if (deltaY > deltaX && deltaY > MIN_HORIZONTAL_VERTICAL_LOCK_DISTANCE) {
                Log.v(TAG, "lock vertical");
                this.setOrientation(VERTICAL);
                isOrientationModeLocked = true;
            } else if (deltaX > deltaY && deltaX > MIN_HORIZONTAL_VERTICAL_LOCK_DISTANCE) {
                Log.v(TAG, "lock horizontal");
                this.setOrientation(HORIZONTAL);
                isOrientationModeLocked = true;
            } else {
                Log.v(TAG, "Not Locked: DeltaX=(" + deltaX + ") DeltaY=(" + deltaY + ")");
                break;
            }
        }

        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) {
            mPopulatePending = true;
            // 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;
            }
            // Log.i(TAG, "mLastMotionY ="+y+" mLastMotionX="+x);
            if (DEBUG)
                Log.i(TAG, "current Item =" + mCurItem);
            final float lowerBound = computePrevPosition() == 0 ? size : 0; //Math.max(0, (mCurItem - 1) * size);
            final float upperBound = computeNextPosition() == 0 ? size : size * 2;
            // Math.min(mCurItem + 1, mAdapter.getCount() - 1) * size;
            if (scroll < lowerBound) {
                scroll = lowerBound;
            } else if (scroll > upperBound) {
                scroll = upperBound;
            }
            if (mOrientation == HORIZONTAL) {
                scrollTo((int) scroll, getScrollY());
            } else {
                scrollTo(getScrollX(), (int) scroll);
            }
            //mLastMotionY += scroll - (int) scroll;
            //mLastMotionX += scroll - (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:
        isOrientationModeLocked = false;
        //TODO snap back to correct position
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity;
            float lastMotion;
            int sizeOverThree;
            float initialMotion;
            if (mOrientation == HORIZONTAL) {
                initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionX;
                sizeOverThree = getWidth() / 3;
                initialMotion = mInitialMotionX;
            } else {
                initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionY;
                sizeOverThree = getHeight() / 3;
                initialMotion = mInitialMotionY;
            }

            mPopulatePending = true;
            if ((Math.abs(initialVelocity) > mMinimumVelocity)
                    || Math.abs(initialMotion - lastMotion) >= sizeOverThree) {
                Log.i(TAG, "lastMotion=" + lastMotion + "mInitialMotionX=" + initialMotion);
                if (lastMotion > initialMotion) {
                    if (computePrevPosition() != null)
                        setCurrentItemInternal(computePrevPosition(), true, true);
                } else {
                    if (computeNextPosition() != null)
                        setCurrentItemInternal(computeNextPosition(), true, true);
                }
            } else {
                setCurrentItemInternal(0, true, true); //snap back to original position
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        isOrientationModeLocked = false;
        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);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        break;
    }
    return true;
}

From source file:android.support.v7.widget.RecyclerView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    if (dispatchOnItemTouch(e)) {
        cancelTouch();// www  .  j  av  a2s. c o  m
        return true;
    }

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

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

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

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

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

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

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

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

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

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

    return true;
}

From source file:com.boutline.sports.helpers.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//from w w  w  .  j a va 2 s  . co m

    if (!mIsAttached || mAdapter == null) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            final boolean inList;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:com.runmit.sweedee.view.DirectListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }// w  w  w.  j  a v a 2 s .  c o  m

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
                Log.d(LOGTAG, "velocity=" + velocity);
                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*  w  w w  . j  a v  a 2s.  c o  m*/

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:com.teamvh.orbital.athena.ViewDragHelper.java

private void releaseViewForPointerUp() {
    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
    final float xvel = clampMag(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    final float yvel = clampMag(VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    //dispatchViewReleased(xvel, yvel);
    dispatchViewReleased(0, 0);/*ww w  .jav  a2s  .  c o  m*/
}

From source file:com.pt.treasuretrash.widget.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*from  ww  w.java 2s  . com*/

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        try {
            if (mDataChanged) {
                break;
            }

            mVelocityTracker.clear();
            mScroller.abortAnimation();

            final float x = ev.getX();
            final float y = ev.getY();

            mLastTouchPos = (mIsVertical ? y : x);

            int motionPosition = pointToPosition((int) x, (int) y);

            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mTouchRemainderPos = 0;

            if (mDataChanged) {
                break;
            }

            if (mTouchMode == TOUCH_MODE_FLINGING) {
                mTouchMode = TOUCH_MODE_DRAGGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
                motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
                return true;
            } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
                mTouchMode = TOUCH_MODE_DOWN;
                triggerCheckForTap();
            }

            mMotionPosition = motionPosition;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        try {
            final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (index < 0) {
                Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                        + " - did TwoWayView receive an inconsistent " + "event stream?");
                return false;
            }

            final float pos;
            if (mIsVertical) {
                pos = MotionEventCompat.getY(ev, index);
            } else {
                pos = MotionEventCompat.getX(ev, index);
            }

            if (mDataChanged) {
                // Re-sync everything if data has been changed
                // since the scroll operation can query the adapter.
                layoutChildren();
            }

            final float diff = pos - mLastTouchPos + mTouchRemainderPos;
            final int delta = (int) diff;
            mTouchRemainderPos = diff - delta;

            switch (mTouchMode) {
            case TOUCH_MODE_DOWN:
            case TOUCH_MODE_TAP:
            case TOUCH_MODE_DONE_WAITING:
                // Check if we have moved far enough that it looks more like a
                // scroll than a tap
                maybeStartScrolling(delta);
                break;

            case TOUCH_MODE_DRAGGING:
            case TOUCH_MODE_OVERSCROLL:
                mLastTouchPos = pos;
                maybeScroll(delta);
                break;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:com.ruyigu.freeart.ui.widget.HorizontalListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//from  w  w w .  j a  va  2s  . c  o m

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more
            // like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:cn.edu.bjtu.svnteen.nourriture.uilib.ViewDragHelper.java

private void releaseViewForPointerUp(MotionEvent ev) {
    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
    float xvel = 0;
    float yvel = 0;

    final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
    final float x = MotionEventCompat.getX(ev, index);
    final float y = MotionEventCompat.getY(ev, index);
    final int idx = (int) (x - mInitialMotionX[mActivePointerId]);
    final int idy = (int) (y - mInitialMotionY[mActivePointerId]);

    if (Math.abs(idx) > Math.abs(idy) * 2) {
        xvel = clampMag(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId), mMinVelocity,
                mMaxVelocity);//from  w  ww . ja v a  2s  . c o m
    }
    if (Math.abs(idy) > Math.abs(idx) * 2) {
        yvel = clampMag(VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId), mMinVelocity,
                mMaxVelocity);
    }

    dispatchViewReleased(xvel, yvel);
}

From source file:android.improving.utils.views.swipeback.ViewDragHelper.java

private void releaseViewForPointerUp() {
    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
    final float xvel = clampMag(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    final float yvel = clampMag(VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
            mMinVelocity, mMaxVelocity);
    dispatchViewReleased(xvel, yvel);/*from  w  w  w  .j  a  va 2  s  .com*/
}