Search in sources :

Example 96 with VelocityTracker

use of android.view.VelocityTracker in project android_frameworks_base by AOSPA.

the class RotarySelector method onTouchEvent.

/**
     * Handle touch screen events.
     *
     * @param event The motion event.
     * @return True if the event was handled, false otherwise.
     */
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mAnimating) {
        return true;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    final int height = getHeight();
    final int eventX = isHoriz() ? (int) event.getX() : height - ((int) event.getY());
    final int hitWindow = mDimpleWidth;
    final int action = event.getAction();
    switch(action) {
        case MotionEvent.ACTION_DOWN:
            if (DBG)
                log("touch-down");
            mTriggered = false;
            if (mGrabbedState != NOTHING_GRABBED) {
                reset();
                invalidate();
            }
            if (eventX < mLeftHandleX + hitWindow) {
                mRotaryOffsetX = eventX - mLeftHandleX;
                setGrabbedState(LEFT_HANDLE_GRABBED);
                invalidate();
                vibrate(VIBRATE_SHORT);
            } else if (eventX > mRightHandleX - hitWindow) {
                mRotaryOffsetX = eventX - mRightHandleX;
                setGrabbedState(RIGHT_HANDLE_GRABBED);
                invalidate();
                vibrate(VIBRATE_SHORT);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (DBG)
                log("touch-move");
            if (mGrabbedState == LEFT_HANDLE_GRABBED) {
                mRotaryOffsetX = eventX - mLeftHandleX;
                invalidate();
                final int rightThresh = isHoriz() ? getRight() : height;
                if (eventX >= rightThresh - mEdgeTriggerThresh && !mTriggered) {
                    mTriggered = true;
                    dispatchTriggerEvent(OnDialTriggerListener.LEFT_HANDLE);
                    final VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                    final int rawVelocity = isHoriz() ? (int) velocityTracker.getXVelocity() : -(int) velocityTracker.getYVelocity();
                    final int velocity = Math.max(mMinimumVelocity, rawVelocity);
                    mDimplesOfFling = Math.max(8, Math.abs(velocity / mDimpleSpacing));
                    startAnimationWithVelocity(eventX - mLeftHandleX, mDimplesOfFling * mDimpleSpacing, velocity);
                }
            } else if (mGrabbedState == RIGHT_HANDLE_GRABBED) {
                mRotaryOffsetX = eventX - mRightHandleX;
                invalidate();
                if (eventX <= mEdgeTriggerThresh && !mTriggered) {
                    mTriggered = true;
                    dispatchTriggerEvent(OnDialTriggerListener.RIGHT_HANDLE);
                    final VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                    final int rawVelocity = isHoriz() ? (int) velocityTracker.getXVelocity() : -(int) velocityTracker.getYVelocity();
                    final int velocity = Math.min(-mMinimumVelocity, rawVelocity);
                    mDimplesOfFling = Math.max(8, Math.abs(velocity / mDimpleSpacing));
                    startAnimationWithVelocity(eventX - mRightHandleX, -(mDimplesOfFling * mDimpleSpacing), velocity);
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (DBG)
                log("touch-up");
            // handle animating back to start if they didn't trigger
            if (mGrabbedState == LEFT_HANDLE_GRABBED && Math.abs(eventX - mLeftHandleX) > 5) {
                // set up "snap back" animation
                startAnimation(eventX - mLeftHandleX, 0, SNAP_BACK_ANIMATION_DURATION_MILLIS);
            } else if (mGrabbedState == RIGHT_HANDLE_GRABBED && Math.abs(eventX - mRightHandleX) > 5) {
                // set up "snap back" animation
                startAnimation(eventX - mRightHandleX, 0, SNAP_BACK_ANIMATION_DURATION_MILLIS);
            }
            mRotaryOffsetX = 0;
            setGrabbedState(NOTHING_GRABBED);
            invalidate();
            if (mVelocityTracker != null) {
                // wishin' we had generational GC
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (DBG)
                log("touch-cancel");
            reset();
            invalidate();
            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
    }
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) Paint(android.graphics.Paint)

Example 97 with VelocityTracker

use of android.view.VelocityTracker in project android_frameworks_base by AOSPA.

the class NumberPicker method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled() || !mHasSelectorWheel) {
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch(action) {
        case MotionEvent.ACTION_MOVE:
            {
                if (mIgnoreMoveEvents) {
                    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:
            {
                removeBeginSoftInputCommand();
                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()) {
                        if (mPerformClickOnTap) {
                            mPerformClickOnTap = false;
                            performClick();
                        } else {
                            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;
}
Also used : VelocityTracker(android.view.VelocityTracker) Paint(android.graphics.Paint)

Example 98 with VelocityTracker

use of android.view.VelocityTracker in project android_frameworks_base by AOSPA.

the class ScrollView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();
    MotionEvent vtev = MotionEvent.obtain(ev);
    final int actionMasked = ev.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);
    switch(actionMasked) {
        case MotionEvent.ACTION_DOWN:
            {
                if (getChildCount() == 0) {
                    return false;
                }
                if ((mIsBeingDragged = !mScroller.isFinished())) {
                    final ViewParent parent = getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
                /*
                 * If being flinged and user touches, stop the fling. isFinished
                 * will be false if being flinged.
                 */
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                    if (mFlingStrictSpan != null) {
                        mFlingStrictSpan.finish();
                        mFlingStrictSpan = null;
                    }
                }
                // Remember where the motion event started
                mLastMotionY = (int) ev.getY();
                mActivePointerId = ev.getPointerId(0);
                startNestedScroll(SCROLL_AXIS_VERTICAL);
                break;
            }
        case MotionEvent.ACTION_MOVE:
            final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
            if (activePointerIndex == -1) {
                Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
                break;
            }
            final int y = (int) ev.getY(activePointerIndex);
            int deltaY = mLastMotionY - y;
            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }
            if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                mIsBeingDragged = true;
                if (deltaY > 0) {
                    deltaY -= mTouchSlop;
                } else {
                    deltaY += mTouchSlop;
                }
            }
            if (mIsBeingDragged) {
                // Scroll to follow the motion event
                mLastMotionY = y - mScrollOffset[1];
                final int oldY = mScrollY;
                final int range = getScrollRange();
                final int overscrollMode = getOverScrollMode();
                boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
                // calls onScrollChanged if applicable.
                if (overScrollBy(0, deltaY, 0, mScrollY, 0, range, 0, mOverscrollDistance, true) && !hasNestedScrollingParent()) {
                    // Break our velocity if we hit a scroll barrier.
                    mVelocityTracker.clear();
                }
                final int scrolledDeltaY = mScrollY - oldY;
                final int unconsumedY = deltaY - scrolledDeltaY;
                if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                    mLastMotionY -= mScrollOffset[1];
                    vtev.offsetLocation(0, mScrollOffset[1]);
                    mNestedYOffset += mScrollOffset[1];
                } else if (canOverscroll) {
                    final int pulledToY = oldY + deltaY;
                    if (pulledToY < 0) {
                        mEdgeGlowTop.onPull((float) deltaY / getHeight(), ev.getX(activePointerIndex) / getWidth());
                        if (!mEdgeGlowBottom.isFinished()) {
                            mEdgeGlowBottom.onRelease();
                        }
                    } else if (pulledToY > range) {
                        mEdgeGlowBottom.onPull((float) deltaY / getHeight(), 1.f - ev.getX(activePointerIndex) / getWidth());
                        if (!mEdgeGlowTop.isFinished()) {
                            mEdgeGlowTop.onRelease();
                        }
                    }
                    if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                        postInvalidateOnAnimation();
                    }
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);
                if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                    flingWithNestedDispatch(-initialVelocity);
                } else if (mScroller.springBack(mScrollX, mScrollY, 0, 0, 0, getScrollRange())) {
                    postInvalidateOnAnimation();
                }
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (mIsBeingDragged && getChildCount() > 0) {
                if (mScroller.springBack(mScrollX, mScrollY, 0, 0, 0, getScrollRange())) {
                    postInvalidateOnAnimation();
                }
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            {
                final int index = ev.getActionIndex();
                mLastMotionY = (int) ev.getY(index);
                mActivePointerId = ev.getPointerId(index);
                break;
            }
        case MotionEvent.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            mLastMotionY = (int) ev.getY(ev.findPointerIndex(mActivePointerId));
            break;
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) ViewParent(android.view.ViewParent) MotionEvent(android.view.MotionEvent)

Example 99 with VelocityTracker

use of android.view.VelocityTracker in project NewXmPluginSDK by MiEcosystem.

the class ViewFlow method onInterceptTouchEvent.

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);
    mHandler.removeMessages(CANCLE_MSG);
    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;
            break;
        case MotionEvent.ACTION_MOVE:
            final int deltaX = (int) (mLastMotionX - x);
            boolean xMoved = Math.abs(deltaX) > mTouchSlop;
            if (xMoved) {
                // Scroll if the user moved far enough along the X axis
                mTouchState = TOUCH_STATE_SCROLLING;
                if (mViewInitializeListener != null)
                    initializeView(deltaX);
            }
            if (mTouchState == TOUCH_STATE_SCROLLING) {
                // Scroll to follow the motion event
                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() - getPaddingRight() - getHorizontalFadingEdgeLength() - 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;
            break;
        case MotionEvent.ACTION_CANCEL:
            mTouchState = TOUCH_STATE_REST;
    }
    return false;
}
Also used : VelocityTracker(android.view.VelocityTracker)

Example 100 with VelocityTracker

use of android.view.VelocityTracker in project NewXmPluginSDK by MiEcosystem.

the class NumberPicker method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled() || !mHasSelectorWheel) {
        return false;
    }
    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:
            {
                removeBeginSoftInputCommand();
                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()) {
                        if (mShowSoftInputOnTap) {
                            mShowSoftInputOnTap = false;
                        } else {
                            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;
}
Also used : VelocityTracker(android.view.VelocityTracker) Paint(android.graphics.Paint)

Aggregations

VelocityTracker (android.view.VelocityTracker)125 ViewParent (android.view.ViewParent)32 Paint (android.graphics.Paint)23 View (android.view.View)14 MediumTest (android.test.suitebuilder.annotation.MediumTest)12 Drawable (android.graphics.drawable.Drawable)10 TransitionDrawable (android.graphics.drawable.TransitionDrawable)10 MotionEvent (android.view.MotionEvent)9 Handler (android.os.Handler)3 AnimatorSet (android.animation.AnimatorSet)2 SuppressLint (android.annotation.SuppressLint)2 PointF (android.graphics.PointF)2 Rect (android.graphics.Rect)1 ViewPager (android.support.v4.view.ViewPager)1 TextPaint (android.text.TextPaint)1 AccelerateInterpolator (android.view.animation.AccelerateInterpolator)1 DecelerateInterpolator (android.view.animation.DecelerateInterpolator)1 ListView (android.widget.ListView)1 OnClickHandler (android.widget.RemoteViews.OnClickHandler)1 Field (java.lang.reflect.Field)1