diff options
| author | Romain Guy <romainguy@android.com> | 2009-07-06 11:48:52 -0700 |
|---|---|---|
| committer | Romain Guy <romainguy@android.com> | 2009-07-06 11:48:52 -0700 |
| commit | 4296fc4d326447875c26a925f12b3935632f13bb (patch) | |
| tree | b2963d32112fb2d3d961ebd48dc8a4b67b8c4d64 /core/java/android/view | |
| parent | 816cf52abd8f45770f0ac922bbb819184ed4b90f (diff) | |
| download | frameworks_base-4296fc4d326447875c26a925f12b3935632f13bb.zip frameworks_base-4296fc4d326447875c26a925f12b3935632f13bb.tar.gz frameworks_base-4296fc4d326447875c26a925f12b3935632f13bb.tar.bz2 | |
Fixes #1444844. Set a maximum fling velocity in scrollable views.
Diffstat (limited to 'core/java/android/view')
| -rw-r--r-- | core/java/android/view/GestureDetector.java | 5 | ||||
| -rw-r--r-- | core/java/android/view/VelocityTracker.java | 21 | ||||
| -rw-r--r-- | core/java/android/view/ViewConfiguration.java | 25 |
3 files changed, 46 insertions, 5 deletions
diff --git a/core/java/android/view/GestureDetector.java b/core/java/android/view/GestureDetector.java index 23f3e3c..1e558be 100644 --- a/core/java/android/view/GestureDetector.java +++ b/core/java/android/view/GestureDetector.java @@ -198,6 +198,7 @@ public class GestureDetector { private int mTouchSlopSquare; private int mDoubleTapSlopSquare; private int mMinimumFlingVelocity; + private int mMaximumFlingVelocity; private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout(); private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); @@ -361,11 +362,13 @@ public class GestureDetector { doubleTapSlop = ViewConfiguration.getDoubleTapSlop(); //noinspection deprecation mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); + mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); } else { final ViewConfiguration configuration = ViewConfiguration.get(context); touchSlop = configuration.getScaledTouchSlop(); doubleTapSlop = configuration.getScaledDoubleTapSlop(); mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity(); + mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity(); } mTouchSlopSquare = touchSlop * touchSlop; mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop; @@ -505,7 +508,7 @@ public class GestureDetector { // A fling must travel the minimum tap distance final VelocityTracker velocityTracker = mVelocityTracker; - velocityTracker.computeCurrentVelocity(1000); + velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); final float velocityY = velocityTracker.getYVelocity(); final float velocityX = velocityTracker.getXVelocity(); diff --git a/core/java/android/view/VelocityTracker.java b/core/java/android/view/VelocityTracker.java index c708f54..5d89c46 100644 --- a/core/java/android/view/VelocityTracker.java +++ b/core/java/android/view/VelocityTracker.java @@ -165,7 +165,17 @@ public final class VelocityTracker implements Poolable<VelocityTracker> { pastTime[i] = 0; } } - + + /** + * Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum + * velocity of Float.MAX_VALUE. + * + * @see #computeCurrentVelocity(int, float) + */ + public void computeCurrentVelocity(int units) { + computeCurrentVelocity(units, Float.MAX_VALUE); + } + /** * Compute the current velocity based on the points that have been * collected. Only call this when you actually want to retrieve velocity @@ -175,8 +185,11 @@ public final class VelocityTracker implements Poolable<VelocityTracker> { * * @param units The units you would like the velocity in. A value of 1 * provides pixels per millisecond, 1000 provides pixels per second, etc. + * @param maxVelocity The maximum velocity that can be computed by this method. + * This value must be declared in the same unit as the units parameter. This value + * must be positive. */ - public void computeCurrentVelocity(int units) { + public void computeCurrentVelocity(int units, float maxVelocity) { final float[] pastX = mPastX; final float[] pastY = mPastY; final long[] pastTime = mPastTime; @@ -210,8 +223,8 @@ public final class VelocityTracker implements Poolable<VelocityTracker> { if (accumY == 0) accumY = vel; else accumY = (accumY + vel) * .5f; } - mXVelocity = accumX; - mYVelocity = accumY; + mXVelocity = accumX < 0.0f ? Math.max(accumX, -maxVelocity) : Math.min(accumX, maxVelocity); + mYVelocity = accumY < 0.0f ? Math.max(accumY, -maxVelocity) : Math.min(accumY, maxVelocity); if (localLOGV) Log.v(TAG, "Y velocity=" + mYVelocity +" X velocity=" + mXVelocity + " N=" + N); diff --git a/core/java/android/view/ViewConfiguration.java b/core/java/android/view/ViewConfiguration.java index 8e1524b..0e36ec2 100644 --- a/core/java/android/view/ViewConfiguration.java +++ b/core/java/android/view/ViewConfiguration.java @@ -106,6 +106,11 @@ public class ViewConfiguration { * Minimum velocity to initiate a fling, as measured in pixels per second */ private static final int MINIMUM_FLING_VELOCITY = 50; + + /** + * Maximum velocity to initiate a fling, as measured in pixels per second + */ + private static final int MAXIMUM_FLING_VELOCITY = 4000; /** * The maximum size of View's drawing cache, expressed in bytes. This size @@ -122,6 +127,7 @@ public class ViewConfiguration { private final int mEdgeSlop; private final int mFadingEdgeLength; private final int mMinimumFlingVelocity; + private final int mMaximumFlingVelocity; private final int mScrollbarSize; private final int mTouchSlop; private final int mDoubleTapSlop; @@ -139,6 +145,7 @@ public class ViewConfiguration { mEdgeSlop = EDGE_SLOP; mFadingEdgeLength = FADING_EDGE_LENGTH; mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY; + mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY; mScrollbarSize = SCROLL_BAR_SIZE; mTouchSlop = TOUCH_SLOP; mDoubleTapSlop = DOUBLE_TAP_SLOP; @@ -164,6 +171,7 @@ public class ViewConfiguration { mEdgeSlop = (int) (density * EDGE_SLOP + 0.5f); mFadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f); mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f); + mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f); mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f); mTouchSlop = (int) (density * TOUCH_SLOP + 0.5f); mDoubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f); @@ -367,6 +375,23 @@ public class ViewConfiguration { } /** + * @return Maximum velocity to initiate a fling, as measured in pixels per second. + * + * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead. + */ + @Deprecated + public static int getMaximumFlingVelocity() { + return MAXIMUM_FLING_VELOCITY; + } + + /** + * @return Maximum velocity to initiate a fling, as measured in pixels per second. + */ + public int getScaledMaximumFlingVelocity() { + return mMaximumFlingVelocity; + } + + /** * The maximum drawing cache size expressed in bytes. * * @return the maximum size of View's drawing cache expressed in bytes |
