Example usage for android.view VelocityTracker getYVelocity

List of usage examples for android.view VelocityTracker getYVelocity

Introduction

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

Prototype

public float getYVelocity() 

Source Link

Document

Retrieve the last computed Y velocity.

Usage

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnableNestedScroll) {
        return super.onTouchEvent(ev);
    }/*ww w  . j  a va 2s .  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:org.zywx.wbpalmstar.engine.EBounceView.java

private boolean handlerTracker() {
    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000);
    int velocityX = (int) (velocityTracker.getXVelocity() / mDensity);
    int velocityY = (int) (velocityTracker.getYVelocity() / mDensity);
    endDrag();// w w  w.ja  va 2s.  co m
    return onTracker(velocityX, velocityY);
}

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

@SuppressLint("ClickableViewAccessibility")
@Override//from  w w w .j av  a  2  s  . c  om
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:org.telegram.ui.Components.NumberPicker.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }//w  w w. j a va  2  s  .c om
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        if (mIngonreMoveEvents) {
            break;
        }
        float currentMoveY = event.getY();
        if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
            int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY);
            if (deltaDownY > mTouchSlop) {
                removeAllCallbacks();
                onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            }
        } else {
            int deltaMoveY = (int) ((currentMoveY - mLastDownOrMoveEventY));
            scrollBy(0, deltaMoveY);
            invalidate();
        }
        mLastDownOrMoveEventY = currentMoveY;
    }
        break;
    case MotionEvent.ACTION_UP: {
        removeChangeCurrentByOneFromLongPress();
        mPressedStateHelper.cancel();
        VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int initialVelocity = (int) velocityTracker.getYVelocity();
        if (Math.abs(initialVelocity) > mMinimumFlingVelocity) {
            fling(initialVelocity);
            onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
        } else {
            int eventY = (int) event.getY();
            int deltaMoveY = (int) Math.abs(eventY - mLastDownEventY);
            long deltaTime = event.getEventTime() - mLastDownEventTime;
            if (deltaMoveY <= mTouchSlop && deltaTime < ViewConfiguration.getTapTimeout()) {
                int selectorIndexOffset = (eventY / mSelectorElementHeight) - SELECTOR_MIDDLE_ITEM_INDEX;
                if (selectorIndexOffset > 0) {
                    changeValueByOne(true);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_INCREMENT);
                } else if (selectorIndexOffset < 0) {
                    changeValueByOne(false);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_DECREMENT);
                }
            } else {
                ensureScrollWheelAdjusted();
            }
            onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }
        break;
    }
    return true;
}

From source file:jmri.enginedriver.throttle.java

public void gestureMove(MotionEvent event) {
    // Log.d("Engine_Driver", "gestureMove action " + event.getAction());
    if (gestureInProgress) {
        // stop the gesture timeout timer
        mainapp.throttle_msg_handler.removeCallbacks(gestureStopped);

        mVelocityTracker.addMovement(event);
        if ((event.getEventTime() - gestureLastCheckTime) > gestureCheckRate) {
            // monitor velocity and fail gesture if it is too low
            gestureLastCheckTime = event.getEventTime();
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000);
            int velocityX = (int) velocityTracker.getXVelocity();
            int velocityY = (int) velocityTracker.getYVelocity();
            // Log.d("Engine_Driver", "gestureVelocity vel " + velocityX);
            if ((Math.abs(velocityX) < threaded_application.min_fling_velocity)
                    && (Math.abs(velocityY) < threaded_application.min_fling_velocity)) {
                gestureFailed(event);/*  w  w  w  . ja  v  a  2 s  .  co  m*/
            }
        }
        if (gestureInProgress) {
            // restart the gesture timeout timer
            mainapp.throttle_msg_handler.postDelayed(gestureStopped, gestureCheckRate);
        }
    }
}