Example usage for android.view VelocityTracker computeCurrentVelocity

List of usage examples for android.view VelocityTracker computeCurrentVelocity

Introduction

In this page you can find the example usage for android.view VelocityTracker computeCurrentVelocity.

Prototype

public void computeCurrentVelocity(int units, float maxVelocity) 

Source Link

Document

Compute the current velocity based on the points that have been collected.

Usage

From source file:Main.java

public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) {

    // Check the dot product of current velocities.
    // If the pointer that left was opposing another velocity vector, clear.
    tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
    final int upIndex = ev.getActionIndex();
    final int id1 = ev.getPointerId(upIndex);
    final float x1 = tracker.getXVelocity(id1);
    final float y1 = tracker.getYVelocity(id1);
    for (int i = 0, count = ev.getPointerCount(); i < count; i++) {
        if (i == upIndex)
            continue;

        final int id2 = ev.getPointerId(i);
        final float x = x1 * tracker.getXVelocity(id2);
        final float y = y1 * tracker.getYVelocity(id2);

        final float dot = x + y;
        if (dot < 0) {
            tracker.clear();/* www .j  a v a2  s.  co  m*/
            break;
        }
    }
}

From source file:com.douban.rexxar.view.NestedWebView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnableNestedScroll) {
        return super.onTouchEvent(ev);
    }//from w w  w .  j a  v  a  2 s  . c  o m
    boolean returnValue = false;

    MotionEvent event = MotionEvent.obtain(ev);
    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;
    }
    int eventX = (int) event.getX();
    int eventY = (int) event.getY();
    event.offsetLocation(0, -mNestedOffsetY);
    switch (action) {
    case MotionEvent.ACTION_MOVE:
        if (mNestedScrollEstablish) {
            mVelocityTracker.addMovement(ev);
            int deltaX = mLastX - eventX;
            int deltaY = mLastY - eventY;
            // ???
            if (mOptimizeHorizontalScroll) {
                // ??
                if (!mScrollHorizontalEstablish && !mScrollVerticalEstablish) {
                    if (Math.abs(deltaX) > Math.abs(deltaY) * 1.5 && Math.abs(deltaX) > mTouchSlop) {
                        mScrollHorizontalEstablish = true;
                    } else if (Math.abs(deltaY) > Math.abs(deltaX) && Math.abs(deltaY) > mTouchSlop) {
                        mScrollVerticalEstablish = true;
                        mFrozenX = eventX;
                    }
                }
            }
            mLastX = eventX;
            if (mScrollHorizontalEstablish) {
                event.offsetLocation(0, deltaY);
                // ?
                returnValue = super.onTouchEvent(event);
            } else {
                // ?
                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mOffsetInWindow)) {
                    deltaY -= mScrollConsumed[1];
                    mLastY = eventY - mOffsetInWindow[1];
                    mNestedOffsetY += mOffsetInWindow[1];
                    event.offsetLocation(0, -mOffsetInWindow[1]);
                } else {
                    mLastY = eventY;
                }

                // parent?consumedelta?webView?
                int oldScrollY = getScrollY();
                if ((deltaY < 0 && getScrollY() > 0) || deltaY > 0) {
                    // ???
                    if (mScrollVerticalEstablish) {
                        event.offsetLocation(mFrozenX - eventX, 0);
                        returnValue = super.onTouchEvent(event);
                    } else {
                        returnValue = super.onTouchEvent(event);
                    }
                    mLastYWebViewConsume = event.getY();
                } else {
                    // FIXME ??
                    if (mScrollVerticalEstablish) {
                        event.offsetLocation(mFrozenX - eventX, mLastYWebViewConsume - event.getY());
                    } else {
                        event.offsetLocation(0, mLastYWebViewConsume - event.getY());
                    }
                    super.onTouchEvent(event);
                }

                // deltaY
                if (deltaY == getScrollY() - oldScrollY) {
                    // ???
                } else if (deltaY < getScrollY() - oldScrollY) {
                    // 
                    if (getScrollY() <= 5) {
                        int dyConsumed = oldScrollY - getScrollY();
                        int dyUnconsumed = deltaY - (getScrollY() - oldScrollY);
                        if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, mOffsetInWindow)) {
                            mNestedOffsetY += mOffsetInWindow[1];
                            mLastY -= mOffsetInWindow[1];
                            event.offsetLocation(0, mOffsetInWindow[1]);
                        }
                    }
                    returnValue = true;
                } else {
                    // ???
                }
            }
        } else {
            returnValue = super.onTouchEvent(event);
        }
        break;
    case MotionEvent.ACTION_DOWN:
        mLastYWebViewConsume = event.getY();
        returnValue = super.onTouchEvent(event);
        mLastX = eventX;
        mLastY = eventY;
        // start NestedScroll
        mNestedScrollEstablish = startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mNestedScrollEstablish) {
            returnValue = super.onTouchEvent(event);
            // end NestedScroll
            stopNestedScroll();
        } else {
            returnValue = super.onTouchEvent(event);
        }
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        mFrozenX = 0;
        recycleVelocityTracker();
        break;
    case MotionEvent.ACTION_UP:
        if (mNestedScrollEstablish) {
            if (mScrollHorizontalEstablish) {
                // ?
                event.offsetLocation(0, mLastY - eventY);
            }
            returnValue = super.onTouchEvent(event);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) velocityTracker.getYVelocity();
            if ((Math.abs(initialVelocity) > mMinimumVelocity) && getScrollY() == 0) {
                flingWithNestedDispatch(-initialVelocity);
            } else {// end NestedScroll
                stopNestedScroll();
            }
        } else {
            returnValue = super.onTouchEvent(event);
        }
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        mFrozenX = 0;
        recycleVelocityTracker();
        break;
    }
    return returnValue;
}

From source file:com.conduit.plastic.widget.NestedWebView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            this.mIsBeingDragged = false;
            this.mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            this.startNestedScroll(2);
            break;
        }//from w  ww . j av  a  2  s . co  m
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            this.endTouch();
            break;
        }
        }
        super.onTouchEvent(ev);
        return true;
    }
    final int actionMasked = MotionEventCompat.getActionMasked(ev);
    initVelocityTrackerIfNotExists();
    MotionEvent vtev = MotionEvent.obtain(ev);
    final int index = MotionEventCompat.getActionIndex(ev);
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);
    this.consumedY = 0;
    this.direction = 0;
    boolean onTouchEvent = false;
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        // Remember where the motion event started
        onTouchEvent = super.onTouchEvent(ev);
        mLastMotionY = (int) (ev.getY() + 0.5f);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        this.preY = vtev.getY();
        this.mIsBeingDragged = false;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        onTouchEvent = super.onTouchEvent(ev);
        mLastMotionY = (int) (MotionEventCompat.getY(ev, index) + 0.5f);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }
        if (!mIsBeingDragged && Math.abs(vtev.getY() - this.preY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
        }
        //                if(!mIsBeingDragged){
        //                    setLongClickEnable(true);
        //                }
        final int y = (int) (MotionEventCompat.getY(ev, activePointerIndex) + 0.5f);
        Log.i(TAG, "mLastMotionY=====" + mLastMotionY);
        Log.i(TAG, "YYYYYYY=====" + y);
        int deltaY = mLastMotionY - y;

        if (deltaY != 0) {
            this.direction = this.directionDetector.getDirection(deltaY, true, this.scrollStateChangedListener);
        }
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (mIsBeingDragged) {
            //                    setJavaScriptEnable(true);
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];
            Log.i(TAG, "deltaY===" + deltaY);
            Log.i(TAG, "this.consumedY===" + this.consumedY);
            final int unconsumedY = deltaY - this.consumedY;

            Log.i(TAG, " child consumed = " + this.mScrollConsumed[1] + " un_consumed = " + unconsumedY
                    + " position = " + this.position + " direction = " + this.direction);
            onTouchEvent = super.onTouchEvent(ev);
            if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) {
                return true;
            }
            switch (this.direction) {
            case 1: {
                if ((this.position != ScrollStateChangedListener.ScrollState.BOTTOM)
                        && (this.contentHeight != this.webviewHeight)) {
                    scrollBy(0, unconsumedY);
                    break;
                }
                Log.i(TAG, "1111111consumedY===" + consumedY + "  unconsumedY==" + unconsumedY);
                if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) {
                    vtev.offsetLocation(0.0F, this.mScrollOffset[1]);
                    this.mNestedYOffset += this.mScrollOffset[1];
                    this.mLastMotionY -= this.mScrollOffset[1];
                }
            }
                break;
            case 2:
                if ((this.position == ScrollStateChangedListener.ScrollState.TOP)
                        || (this.contentHeight == this.webviewHeight)) {
                    Log.i(TAG, "2222222consumedY===" + consumedY + "  unconsumedY==" + unconsumedY);
                    if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) {
                        vtev.offsetLocation(0.0F, this.mScrollOffset[1]);
                        this.mNestedYOffset += this.mScrollOffset[1];
                        this.mLastMotionY -= this.mScrollOffset[1];
                    }
                } else {
                    scrollBy(0, unconsumedY);
                }
                break;
            default:
                break;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        onTouchEvent = super.onTouchEvent(ev);
        break;
    case MotionEvent.ACTION_UP:
        onTouchEvent = super.onTouchEvent(ev);
        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;
        endTouch();
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onTouchEvent = super.onTouchEvent(ev);
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) (MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId)) + 0.5F);
        break;
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return onTouchEvent;
}

From source file:com.coco.slidinguppanel.SlidingUpPanel.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getState() == STATE_OPENED) {
        // disable touch handle when in opened state.
        return false;
    }/*  w w  w .  ja v  a  2 s .  c  o m*/

    final int action = MotionEventCompat.getActionMasked(ev);

    if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our descendants.
        return false;
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev, false);
        DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER || mIsUnableToDrag) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float xDiff = Math.abs(x - mInitialMotionX);
        final float yDiff = Math.abs(y - mInitialMotionY);
        DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        onTouchMove(x, y, xDiff, yDiff, false);
        break;
    }
    case MotionEvent.ACTION_UP: {
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                    mActivePointerId);
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final int totalDelta = (int) (y - mInitialMotionY);
            boolean toOpen = determineToOpen(initialVelocity, totalDelta);
            startFling(toOpen, initialVelocity);
            endDrag();
        }
        DEBUG_LOG("Touch up!!!");
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        if (mIsBeingDragged) {
            startFling(isOpen(), 0);
            endDrag();
        }
        DEBUG_LOG("Touch cancel!!!");
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onTouchPointerUp(ev);
        break;
    }

    return true;
}

From source file:com.gitstudy.rili.liarbry.CalendarLayout.java

@SuppressLint("ClickableViewAccessibility")
@Override/*w ww .ja v a 2  s.com*/
public boolean onTouchEvent(MotionEvent event) {

    if (mDelegate.isShowYearSelectedLayout) {
        return false;
    }

    if (mContentView == null) {
        return false;
    }

    int action = event.getAction();
    float y = event.getY();
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:

        int index = MotionEventCompat.getActionIndex(event);
        mActivePointerId = MotionEventCompat.getPointerId(event, index);
        mLastY = downY = y;
        return true;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int indexx = MotionEventCompat.getActionIndex(event);
        mActivePointerId = MotionEventCompat.getPointerId(event, indexx);
        if (mActivePointerId == 0) {
            //?? dy = y- mLastY == 0??
            mLastY = MotionEventCompat.getY(event, mActivePointerId);
        }
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (mGestureMode == GESTURE_MODE_DISABLED || mCalendarShowMode == CALENDAR_SHOW_MODE_ONLY_MONTH_VIEW
                || mCalendarShowMode == CALENDAR_SHOW_MODE_ONLY_WEEK_VIEW) {//????
            return false;
        }

        getPointerIndex(event, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) {
            //?mLastY?y????? dy== 0??
            mLastY = y;
            mActivePointerId = ACTIVE_POINTER;
        }

        float dy = y - mLastY;
        //?contentView?
        if (dy < 0 && mContentView.getTranslationY() == -mContentViewTranslateY) {
            //mContentView.onTouchEvent(event);
            showWeek();
            mLastY = y;
            return false;
        }
        hideWeek();

        //?contentView?
        if (dy > 0 && mContentView.getTranslationY() + dy >= 0) {
            mContentView.setTranslationY(0);
            translationViewPager();
            mLastY = y;
            return super.onTouchEvent(event);
        }

        //?contentView??contentView?
        if (dy < 0 && mContentView.getTranslationY() + dy <= -mContentViewTranslateY) {
            mContentView.setTranslationY(-mContentViewTranslateY);
            translationViewPager();
            mLastY = y;
            return super.onTouchEvent(event);
        }
        //?
        mContentView.setTranslationY(mContentView.getTranslationY() + dy);
        translationViewPager();
        mLastY = y;
        break;
    case MotionEvent.ACTION_CANCEL:

    case MotionEvent.ACTION_POINTER_UP:
        int pointerIndex = getPointerIndex(event, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER)
            break;
        mLastY = MotionEventCompat.getY(event, pointerIndex);
        break;
    case MotionEvent.ACTION_UP:

        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        float mYVelocity = velocityTracker.getYVelocity();
        if (mContentView.getTranslationY() == 0 || mContentView.getTranslationY() == mContentViewTranslateY) {
            break;
        }
        if (Math.abs(mYVelocity) >= 800) {
            if (mYVelocity < 0) {
                shrink();
            } else {
                expand();
            }
            return super.onTouchEvent(event);
        }
        if (event.getY() - downY > 0) {
            expand();
        } else {
            shrink();
        }
        break;
    }
    return super.onTouchEvent(event);
}

From source file:com.yj.ecard.ui.views.viewflow.ViewFlow.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }/*from w w w  .java 2s . c om*/
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (parentViewpager != null)
            parentViewpager.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
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

        if (handler != null) {
            handler.removeMessages(0);
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;
        // action_up?????
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (parentViewpager != null)
            parentViewpager.requestDisallowInterceptTouchEvent(true);
        mTouchState = TOUCH_STATE_REST;
    }
    return false;
}

From source file:com.yj.ecard.ui.views.viewflow.ViewFlow.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//from  w ww. j  av a  2 s  . c o  m
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        /*
         * 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
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        // ??
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            }
            // else if (velocityX < -SNAP_VELOCITY
            // && mCurrentScreen == getChildCount() - 1) {
            // snapToScreen(0);
            // }
            // else if (velocityX > SNAP_VELOCITY
            // && mCurrentScreen == 0) {
            // snapToScreen(getChildCount() - 1);
            // }
            else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        // action_up?????
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        snapToDestination();
        mTouchState = TOUCH_STATE_REST;
    }
    return true;
}

From source file:com.jp.fristandroidapp.widget.adview.AdViewFlow.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }// w w  w  .j  a v a2 s  .c  o  m
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setIsInterceptParent(true);
        setIsInterceptParent2(true);
        setIsInterceptParent3(true);
        setIsInterceptParent4(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
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;
        setIsInterceptParent(false);
        setIsInterceptParent2(false);
        setIsInterceptParent3(false);
        setIsInterceptParent4(false);
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        setIsInterceptParent(false);
        setIsInterceptParent2(false);
        setIsInterceptParent3(false);
        setIsInterceptParent4(false);
    }
    return false;
}

From source file:com.jp.fristandroidapp.widget.adview.AdViewFlow.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }/*from w w w . ja v  a2  s.  com*/
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    final float x = ev.getX();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        /*
         * 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
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            }
            //            else if (velocityX < -SNAP_VELOCITY
            //                     && mCurrentScreen == getChildCount() - 1) {
            //                  snapToScreen(0);
            //            }
            //            else if (velocityX > SNAP_VELOCITY
            //                     && mCurrentScreen == 0) {
            //                  snapToScreen(getChildCount() - 1);
            //            }
            else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        snapToDestination();
        mTouchState = TOUCH_STATE_REST;
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
    }
    return true;
}

From source file:com.gu.swiperefresh.SwipeRefreshPlush.java

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

    final int action = MotionEventCompat.getActionMasked(ev);
    int pointerIndex;

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || (!canLoadMore() && !canRefresh()) || mReturningToStart || mRefreshController.isRefresh()
            || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mRefreshController.setTargetOffsetTopAndBottom(
                mRefreshController.getCurrentTargetOffsetTop() - mRefreshView.getTop(), true);
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragUp = false;
        mIsBeingDragDown = false;

        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        mInitialDownY = ev.getY(pointerIndex);
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }
        mVelocityTracker.addMovement(ev);
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        final float y = ev.getY(pointerIndex);
        startDragging(y);
        break;

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

    case MotionEvent.ACTION_UP:
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        float initialVelocity = velocityTracker.getYVelocity(mActivePointerId);
        Log.d(TAG, "fling:" + initialVelocity);
        if (Math.abs(initialVelocity) > mMinimumVelocity) {
            flingWithNestedDispatch(0, -initialVelocity);
        }
        releaseVelocityTracker();
        break;
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragUp = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragUp || mIsBeingDragDown;
}