Search in sources :

Example 6 with VelocityTracker

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

the class ScrollView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction();
    switch(action & MotionEvent.ACTION_MASK) {
        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);
                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 (!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;
                final int oldX = mScrollX;
                final int oldY = mScrollY;
                final int range = getScrollRange();
                final int overscrollMode = getOverScrollMode();
                final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
                if (overScrollBy(0, deltaY, 0, mScrollY, 0, range, 0, mOverscrollDistance, true)) {
                    // Break our velocity if we hit a scroll barrier.
                    mVelocityTracker.clear();
                }
                onScrollChanged(mScrollX, mScrollY, oldX, oldY);
                if (canOverscroll) {
                    final int pulledToY = oldY + deltaY;
                    if (pulledToY < 0) {
                        mEdgeGlowTop.onPull((float) deltaY / getHeight());
                        if (!mEdgeGlowBottom.isFinished()) {
                            mEdgeGlowBottom.onRelease();
                        }
                    } else if (pulledToY > range) {
                        mEdgeGlowBottom.onPull((float) deltaY / getHeight());
                        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 (getChildCount() > 0) {
                    if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                        fling(-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;
    }
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) ViewParent(android.view.ViewParent)

Example 7 with VelocityTracker

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

the class VelocityTest method testInitialCondiditions.

@MediumTest
public void testInitialCondiditions() {
    VelocityTracker vt = VelocityTracker.obtain();
    assertNotNull(vt);
    vt.recycle();
}
Also used : VelocityTracker(android.view.VelocityTracker) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 8 with VelocityTracker

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

the class VelocityTest method testDragWith2Points.

/**
     * Test dragging with two points only
     * (velocity must be an exact value)
     */
@MediumTest
public void testDragWith2Points() {
    long t = System.currentTimeMillis();
    VelocityTracker vt = VelocityTracker.obtain();
    // 100px, 2 steps, 100ms => 1000px/s
    drag(vt, 100, 200, 100, 200, 2, t, 100);
    vt.computeCurrentVelocity(1000);
    assertEquals(1000.0f, vt.getXVelocity());
    assertEquals(1000.0f, vt.getYVelocity());
    vt.recycle();
}
Also used : VelocityTracker(android.view.VelocityTracker) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 9 with VelocityTracker

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

the class VelocityTest method testDragDeceleration.

@MediumTest
public void testDragDeceleration() {
    long t = System.currentTimeMillis();
    VelocityTracker vt = VelocityTracker.obtain();
    drag(vt, 100, 200, 100, 200, 15, t, 400, new DecelerateInterpolator());
    vt.computeCurrentVelocity(1000);
    assertLower(250.0f, vt.getXVelocity());
    assertLower(250.0f, vt.getYVelocity());
    vt.recycle();
}
Also used : DecelerateInterpolator(android.view.animation.DecelerateInterpolator) VelocityTracker(android.view.VelocityTracker) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 10 with VelocityTracker

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

the class VelocityTest method testStabilityInNbPoints.

/**
     * Velocity is independent of the number of points used during
     * the same interval
     */
@MediumTest
public void testStabilityInNbPoints() {
    long t = System.currentTimeMillis();
    VelocityTracker vt = VelocityTracker.obtain();
    // 10 steps over 400ms
    drag(vt, 100, 200, 100, 200, 10, t, 400);
    vt.computeCurrentVelocity(1);
    float firstX = vt.getXVelocity();
    float firstY = vt.getYVelocity();
    vt.clear();
    // 20 steps over 400ms
    drag(vt, 100, 200, 100, 200, 20, t, 400);
    vt.computeCurrentVelocity(1);
    float secondX = vt.getXVelocity();
    float secondY = vt.getYVelocity();
    assertEqualFuzzy(firstX, secondX, 0.1f);
    assertEqualFuzzy(firstY, secondY, 0.1f);
    vt.recycle();
}
Also used : VelocityTracker(android.view.VelocityTracker) MediumTest(android.test.suitebuilder.annotation.MediumTest)

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