Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 21 | import android.hardware.SensorManager; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 23 | import android.view.ViewConfiguration; |
| 24 | import android.view.animation.AnimationUtils; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 25 | import android.view.animation.Interpolator; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * This class encapsulates scrolling with the ability to overshoot the bounds |
| 29 | * of a scrolling operation. This class is a drop-in replacement for |
| 30 | * {@link android.widget.Scroller} in most cases. |
| 31 | */ |
| 32 | public class OverScroller { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 33 | private int mMode; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 34 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 35 | private final SplineOverScroller mScrollerX; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 36 | @UnsupportedAppUsage |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 37 | private final SplineOverScroller mScrollerY; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 38 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 39 | @UnsupportedAppUsage |
Adam Powell | 0b8acd8 | 2012-04-25 20:29:23 -0700 | [diff] [blame] | 40 | private Interpolator mInterpolator; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 41 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 42 | private final boolean mFlywheel; |
| 43 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 44 | private static final int DEFAULT_DURATION = 250; |
| 45 | private static final int SCROLL_MODE = 0; |
| 46 | private static final int FLING_MODE = 1; |
| 47 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 48 | /** |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 49 | * Creates an OverScroller with a viscous fluid scroll interpolator and flywheel. |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 50 | * @param context |
| 51 | */ |
| 52 | public OverScroller(Context context) { |
| 53 | this(context, null); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 56 | /** |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 57 | * Creates an OverScroller with flywheel enabled. |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 58 | * @param context The context of this application. |
| 59 | * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will |
| 60 | * be used. |
| 61 | */ |
| 62 | public OverScroller(Context context, Interpolator interpolator) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 63 | this(context, interpolator, true); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates an OverScroller. |
| 68 | * @param context The context of this application. |
| 69 | * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will |
| 70 | * be used. |
| 71 | * @param flywheel If true, successive fling motions will keep on increasing scroll speed. |
Wink Saville | 086c061 | 2010-12-11 09:01:31 -0800 | [diff] [blame] | 72 | * @hide |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 73 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 74 | @UnsupportedAppUsage |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 75 | public OverScroller(Context context, Interpolator interpolator, boolean flywheel) { |
Alan Viverette | 79a5fef | 2013-09-04 16:37:57 -0700 | [diff] [blame] | 76 | if (interpolator == null) { |
| 77 | mInterpolator = new Scroller.ViscousFluidInterpolator(); |
| 78 | } else { |
| 79 | mInterpolator = interpolator; |
| 80 | } |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 81 | mFlywheel = flywheel; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 82 | mScrollerX = new SplineOverScroller(context); |
| 83 | mScrollerY = new SplineOverScroller(context); |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates an OverScroller with flywheel enabled. |
| 88 | * @param context The context of this application. |
| 89 | * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will |
| 90 | * be used. |
| 91 | * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the |
| 92 | * velocity which is preserved in the bounce when the horizontal edge is reached. A null value |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 93 | * means no bounce. This behavior is no longer supported and this coefficient has no effect. |
| 94 | * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This |
| 95 | * behavior is no longer supported and this coefficient has no effect. |
Kirill Grouchnikov | cdac515 | 2016-09-06 19:02:33 -0700 | [diff] [blame] | 96 | * @deprecated Use {@link #OverScroller(Context, Interpolator)} instead. |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 97 | */ |
Kirill Grouchnikov | cdac515 | 2016-09-06 19:02:33 -0700 | [diff] [blame] | 98 | @Deprecated |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 99 | public OverScroller(Context context, Interpolator interpolator, |
| 100 | float bounceCoefficientX, float bounceCoefficientY) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 101 | this(context, interpolator, true); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates an OverScroller. |
| 106 | * @param context The context of this application. |
| 107 | * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will |
| 108 | * be used. |
| 109 | * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the |
| 110 | * velocity which is preserved in the bounce when the horizontal edge is reached. A null value |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 111 | * means no bounce. This behavior is no longer supported and this coefficient has no effect. |
| 112 | * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This |
| 113 | * behavior is no longer supported and this coefficient has no effect. |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 114 | * @param flywheel If true, successive fling motions will keep on increasing scroll speed. |
Kirill Grouchnikov | cdac515 | 2016-09-06 19:02:33 -0700 | [diff] [blame] | 115 | * @deprecated Use {@link #OverScroller(Context, Interpolator)} instead. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 116 | */ |
Kirill Grouchnikov | cdac515 | 2016-09-06 19:02:33 -0700 | [diff] [blame] | 117 | @Deprecated |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 118 | public OverScroller(Context context, Interpolator interpolator, |
| 119 | float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 120 | this(context, interpolator, flywheel); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Adam Powell | 0b8acd8 | 2012-04-25 20:29:23 -0700 | [diff] [blame] | 124 | void setInterpolator(Interpolator interpolator) { |
Alan Viverette | 79a5fef | 2013-09-04 16:37:57 -0700 | [diff] [blame] | 125 | if (interpolator == null) { |
| 126 | mInterpolator = new Scroller.ViscousFluidInterpolator(); |
| 127 | } else { |
| 128 | mInterpolator = interpolator; |
| 129 | } |
Adam Powell | 0b8acd8 | 2012-04-25 20:29:23 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 132 | /** |
| 133 | * The amount of friction applied to flings. The default value |
| 134 | * is {@link ViewConfiguration#getScrollFriction}. |
| 135 | * |
| 136 | * @param friction A scalar dimension-less value representing the coefficient of |
| 137 | * friction. |
| 138 | */ |
| 139 | public final void setFriction(float friction) { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 140 | mScrollerX.setFriction(friction); |
| 141 | mScrollerY.setFriction(friction); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * |
| 146 | * Returns whether the scroller has finished scrolling. |
| 147 | * |
| 148 | * @return True if the scroller has finished scrolling, false otherwise. |
| 149 | */ |
| 150 | public final boolean isFinished() { |
| 151 | return mScrollerX.mFinished && mScrollerY.mFinished; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Force the finished field to a particular value. Contrary to |
| 156 | * {@link #abortAnimation()}, forcing the animation to finished |
| 157 | * does NOT cause the scroller to move to the final x and y |
| 158 | * position. |
| 159 | * |
| 160 | * @param finished The new finished value. |
| 161 | */ |
| 162 | public final void forceFinished(boolean finished) { |
| 163 | mScrollerX.mFinished = mScrollerY.mFinished = finished; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the current X offset in the scroll. |
| 168 | * |
| 169 | * @return The new X offset as an absolute distance from the origin. |
| 170 | */ |
| 171 | public final int getCurrX() { |
| 172 | return mScrollerX.mCurrentPosition; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the current Y offset in the scroll. |
| 177 | * |
| 178 | * @return The new Y offset as an absolute distance from the origin. |
| 179 | */ |
| 180 | public final int getCurrY() { |
| 181 | return mScrollerY.mCurrentPosition; |
| 182 | } |
| 183 | |
| 184 | /** |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 185 | * Returns the absolute value of the current velocity. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 186 | * |
| 187 | * @return The original velocity less the deceleration, norm of the X and Y velocity vector. |
| 188 | */ |
| 189 | public float getCurrVelocity() { |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 190 | return (float) Math.hypot(mScrollerX.mCurrVelocity, mScrollerY.mCurrVelocity); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns the start X offset in the scroll. |
| 195 | * |
| 196 | * @return The start X offset as an absolute distance from the origin. |
| 197 | */ |
| 198 | public final int getStartX() { |
| 199 | return mScrollerX.mStart; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns the start Y offset in the scroll. |
| 204 | * |
| 205 | * @return The start Y offset as an absolute distance from the origin. |
| 206 | */ |
| 207 | public final int getStartY() { |
| 208 | return mScrollerY.mStart; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns where the scroll will end. Valid only for "fling" scrolls. |
| 213 | * |
| 214 | * @return The final X offset as an absolute distance from the origin. |
| 215 | */ |
| 216 | public final int getFinalX() { |
| 217 | return mScrollerX.mFinal; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns where the scroll will end. Valid only for "fling" scrolls. |
| 222 | * |
| 223 | * @return The final Y offset as an absolute distance from the origin. |
| 224 | */ |
| 225 | public final int getFinalY() { |
| 226 | return mScrollerY.mFinal; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns how long the scroll event will take, in milliseconds. |
| 231 | * |
| 232 | * @return The duration of the scroll in milliseconds. |
| 233 | * |
| 234 | * @hide Pending removal once nothing depends on it |
| 235 | * @deprecated OverScrollers don't necessarily have a fixed duration. |
| 236 | * This function will lie to the best of its ability. |
| 237 | */ |
| 238 | @Deprecated |
| 239 | public final int getDuration() { |
| 240 | return Math.max(mScrollerX.mDuration, mScrollerY.mDuration); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Extend the scroll animation. This allows a running animation to scroll |
| 245 | * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}. |
| 246 | * |
| 247 | * @param extend Additional time to scroll in milliseconds. |
| 248 | * @see #setFinalX(int) |
| 249 | * @see #setFinalY(int) |
| 250 | * |
| 251 | * @hide Pending removal once nothing depends on it |
| 252 | * @deprecated OverScrollers don't necessarily have a fixed duration. |
| 253 | * Instead of setting a new final position and extending |
| 254 | * the duration of an existing scroll, use startScroll |
| 255 | * to begin a new animation. |
| 256 | */ |
| 257 | @Deprecated |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 258 | @UnsupportedAppUsage |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 259 | public void extendDuration(int extend) { |
| 260 | mScrollerX.extendDuration(extend); |
| 261 | mScrollerY.extendDuration(extend); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets the final position (X) for this scroller. |
| 266 | * |
| 267 | * @param newX The new X offset as an absolute distance from the origin. |
| 268 | * @see #extendDuration(int) |
| 269 | * @see #setFinalY(int) |
| 270 | * |
| 271 | * @hide Pending removal once nothing depends on it |
| 272 | * @deprecated OverScroller's final position may change during an animation. |
| 273 | * Instead of setting a new final position and extending |
| 274 | * the duration of an existing scroll, use startScroll |
| 275 | * to begin a new animation. |
| 276 | */ |
| 277 | @Deprecated |
| 278 | public void setFinalX(int newX) { |
| 279 | mScrollerX.setFinalPosition(newX); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Sets the final position (Y) for this scroller. |
| 284 | * |
| 285 | * @param newY The new Y offset as an absolute distance from the origin. |
| 286 | * @see #extendDuration(int) |
| 287 | * @see #setFinalX(int) |
| 288 | * |
| 289 | * @hide Pending removal once nothing depends on it |
| 290 | * @deprecated OverScroller's final position may change during an animation. |
| 291 | * Instead of setting a new final position and extending |
| 292 | * the duration of an existing scroll, use startScroll |
| 293 | * to begin a new animation. |
| 294 | */ |
| 295 | @Deprecated |
| 296 | public void setFinalY(int newY) { |
| 297 | mScrollerY.setFinalPosition(newY); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Call this when you want to know the new location. If it returns true, the |
| 302 | * animation is not yet finished. |
| 303 | */ |
| 304 | public boolean computeScrollOffset() { |
| 305 | if (isFinished()) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | switch (mMode) { |
| 310 | case SCROLL_MODE: |
| 311 | long time = AnimationUtils.currentAnimationTimeMillis(); |
| 312 | // Any scroller can be used for time, since they were started |
| 313 | // together in scroll mode. We use X here. |
| 314 | final long elapsedTime = time - mScrollerX.mStartTime; |
| 315 | |
| 316 | final int duration = mScrollerX.mDuration; |
| 317 | if (elapsedTime < duration) { |
Alan Viverette | 79a5fef | 2013-09-04 16:37:57 -0700 | [diff] [blame] | 318 | final float q = mInterpolator.getInterpolation(elapsedTime / (float) duration); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 319 | mScrollerX.updateScroll(q); |
| 320 | mScrollerY.updateScroll(q); |
| 321 | } else { |
| 322 | abortAnimation(); |
| 323 | } |
| 324 | break; |
| 325 | |
| 326 | case FLING_MODE: |
| 327 | if (!mScrollerX.mFinished) { |
| 328 | if (!mScrollerX.update()) { |
| 329 | if (!mScrollerX.continueWhenFinished()) { |
| 330 | mScrollerX.finish(); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (!mScrollerY.mFinished) { |
| 336 | if (!mScrollerY.update()) { |
| 337 | if (!mScrollerY.continueWhenFinished()) { |
| 338 | mScrollerY.finish(); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Start scrolling by providing a starting point and the distance to travel. |
| 351 | * The scroll will use the default value of 250 milliseconds for the |
| 352 | * duration. |
| 353 | * |
| 354 | * @param startX Starting horizontal scroll offset in pixels. Positive |
| 355 | * numbers will scroll the content to the left. |
| 356 | * @param startY Starting vertical scroll offset in pixels. Positive numbers |
| 357 | * will scroll the content up. |
| 358 | * @param dx Horizontal distance to travel. Positive numbers will scroll the |
| 359 | * content to the left. |
| 360 | * @param dy Vertical distance to travel. Positive numbers will scroll the |
| 361 | * content up. |
| 362 | */ |
| 363 | public void startScroll(int startX, int startY, int dx, int dy) { |
| 364 | startScroll(startX, startY, dx, dy, DEFAULT_DURATION); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Start scrolling by providing a starting point and the distance to travel. |
| 369 | * |
| 370 | * @param startX Starting horizontal scroll offset in pixels. Positive |
| 371 | * numbers will scroll the content to the left. |
| 372 | * @param startY Starting vertical scroll offset in pixels. Positive numbers |
| 373 | * will scroll the content up. |
| 374 | * @param dx Horizontal distance to travel. Positive numbers will scroll the |
| 375 | * content to the left. |
| 376 | * @param dy Vertical distance to travel. Positive numbers will scroll the |
| 377 | * content up. |
| 378 | * @param duration Duration of the scroll in milliseconds. |
| 379 | */ |
| 380 | public void startScroll(int startX, int startY, int dx, int dy, int duration) { |
| 381 | mMode = SCROLL_MODE; |
| 382 | mScrollerX.startScroll(startX, dx, duration); |
| 383 | mScrollerY.startScroll(startY, dy, duration); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Call this when you want to 'spring back' into a valid coordinate range. |
| 388 | * |
| 389 | * @param startX Starting X coordinate |
| 390 | * @param startY Starting Y coordinate |
| 391 | * @param minX Minimum valid X value |
| 392 | * @param maxX Maximum valid X value |
| 393 | * @param minY Minimum valid Y value |
| 394 | * @param maxY Minimum valid Y value |
| 395 | * @return true if a springback was initiated, false if startX and startY were |
| 396 | * already within the valid range. |
| 397 | */ |
| 398 | public boolean springBack(int startX, int startY, int minX, int maxX, int minY, int maxY) { |
| 399 | mMode = FLING_MODE; |
| 400 | |
| 401 | // Make sure both methods are called. |
| 402 | final boolean spingbackX = mScrollerX.springback(startX, minX, maxX); |
| 403 | final boolean spingbackY = mScrollerY.springback(startY, minY, maxY); |
| 404 | return spingbackX || spingbackY; |
| 405 | } |
| 406 | |
| 407 | public void fling(int startX, int startY, int velocityX, int velocityY, |
| 408 | int minX, int maxX, int minY, int maxY) { |
| 409 | fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Start scrolling based on a fling gesture. The distance traveled will |
| 414 | * depend on the initial velocity of the fling. |
| 415 | * |
| 416 | * @param startX Starting point of the scroll (X) |
| 417 | * @param startY Starting point of the scroll (Y) |
| 418 | * @param velocityX Initial velocity of the fling (X) measured in pixels per |
| 419 | * second. |
| 420 | * @param velocityY Initial velocity of the fling (Y) measured in pixels per |
| 421 | * second |
| 422 | * @param minX Minimum X value. The scroller will not scroll past this point |
| 423 | * unless overX > 0. If overfling is allowed, it will use minX as |
| 424 | * a springback boundary. |
| 425 | * @param maxX Maximum X value. The scroller will not scroll past this point |
| 426 | * unless overX > 0. If overfling is allowed, it will use maxX as |
| 427 | * a springback boundary. |
| 428 | * @param minY Minimum Y value. The scroller will not scroll past this point |
| 429 | * unless overY > 0. If overfling is allowed, it will use minY as |
| 430 | * a springback boundary. |
| 431 | * @param maxY Maximum Y value. The scroller will not scroll past this point |
| 432 | * unless overY > 0. If overfling is allowed, it will use maxY as |
| 433 | * a springback boundary. |
| 434 | * @param overX Overfling range. If > 0, horizontal overfling in either |
| 435 | * direction will be possible. |
| 436 | * @param overY Overfling range. If > 0, vertical overfling in either |
| 437 | * direction will be possible. |
| 438 | */ |
| 439 | public void fling(int startX, int startY, int velocityX, int velocityY, |
| 440 | int minX, int maxX, int minY, int maxY, int overX, int overY) { |
| 441 | // Continue a scroll or fling in progress |
| 442 | if (mFlywheel && !isFinished()) { |
| 443 | float oldVelocityX = mScrollerX.mCurrVelocity; |
| 444 | float oldVelocityY = mScrollerY.mCurrVelocity; |
| 445 | if (Math.signum(velocityX) == Math.signum(oldVelocityX) && |
| 446 | Math.signum(velocityY) == Math.signum(oldVelocityY)) { |
| 447 | velocityX += oldVelocityX; |
| 448 | velocityY += oldVelocityY; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | mMode = FLING_MODE; |
| 453 | mScrollerX.fling(startX, velocityX, minX, maxX, overX); |
| 454 | mScrollerY.fling(startY, velocityY, minY, maxY, overY); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Notify the scroller that we've reached a horizontal boundary. |
| 459 | * Normally the information to handle this will already be known |
| 460 | * when the animation is started, such as in a call to one of the |
| 461 | * fling functions. However there are cases where this cannot be known |
| 462 | * in advance. This function will transition the current motion and |
| 463 | * animate from startX to finalX as appropriate. |
| 464 | * |
| 465 | * @param startX Starting/current X position |
| 466 | * @param finalX Desired final X position |
| 467 | * @param overX Magnitude of overscroll allowed. This should be the maximum |
| 468 | * desired distance from finalX. Absolute value - must be positive. |
| 469 | */ |
| 470 | public void notifyHorizontalEdgeReached(int startX, int finalX, int overX) { |
| 471 | mScrollerX.notifyEdgeReached(startX, finalX, overX); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Notify the scroller that we've reached a vertical boundary. |
| 476 | * Normally the information to handle this will already be known |
| 477 | * when the animation is started, such as in a call to one of the |
| 478 | * fling functions. However there are cases where this cannot be known |
| 479 | * in advance. This function will animate a parabolic motion from |
| 480 | * startY to finalY. |
| 481 | * |
| 482 | * @param startY Starting/current Y position |
| 483 | * @param finalY Desired final Y position |
| 484 | * @param overY Magnitude of overscroll allowed. This should be the maximum |
| 485 | * desired distance from finalY. Absolute value - must be positive. |
| 486 | */ |
| 487 | public void notifyVerticalEdgeReached(int startY, int finalY, int overY) { |
| 488 | mScrollerY.notifyEdgeReached(startY, finalY, overY); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Returns whether the current Scroller is currently returning to a valid position. |
| 493 | * Valid bounds were provided by the |
| 494 | * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method. |
| 495 | * |
| 496 | * One should check this value before calling |
| 497 | * {@link #startScroll(int, int, int, int)} as the interpolation currently in progress |
| 498 | * to restore a valid position will then be stopped. The caller has to take into account |
| 499 | * the fact that the started scroll will start from an overscrolled position. |
| 500 | * |
| 501 | * @return true when the current position is overscrolled and in the process of |
| 502 | * interpolating back to a valid value. |
| 503 | */ |
| 504 | public boolean isOverScrolled() { |
| 505 | return ((!mScrollerX.mFinished && |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 506 | mScrollerX.mState != SplineOverScroller.SPLINE) || |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 507 | (!mScrollerY.mFinished && |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 508 | mScrollerY.mState != SplineOverScroller.SPLINE)); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Stops the animation. Contrary to {@link #forceFinished(boolean)}, |
| 513 | * aborting the animating causes the scroller to move to the final x and y |
| 514 | * positions. |
| 515 | * |
| 516 | * @see #forceFinished(boolean) |
| 517 | */ |
| 518 | public void abortAnimation() { |
| 519 | mScrollerX.finish(); |
| 520 | mScrollerY.finish(); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Returns the time elapsed since the beginning of the scrolling. |
| 525 | * |
| 526 | * @return The elapsed time in milliseconds. |
| 527 | * |
| 528 | * @hide |
| 529 | */ |
| 530 | public int timePassed() { |
| 531 | final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 532 | final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime); |
| 533 | return (int) (time - startTime); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * @hide |
| 538 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 539 | @UnsupportedAppUsage |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 540 | public boolean isScrollingInDirection(float xvel, float yvel) { |
| 541 | final int dx = mScrollerX.mFinal - mScrollerX.mStart; |
| 542 | final int dy = mScrollerY.mFinal - mScrollerY.mStart; |
| 543 | return !isFinished() && Math.signum(xvel) == Math.signum(dx) && |
Jeff Brown | b0c71eb | 2011-09-16 21:40:49 -0700 | [diff] [blame] | 544 | Math.signum(yvel) == Math.signum(dy); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 547 | static class SplineOverScroller { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 548 | // Initial position |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 549 | private int mStart; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 550 | |
| 551 | // Current position |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 552 | private int mCurrentPosition; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 553 | |
| 554 | // Final position |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 555 | private int mFinal; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 556 | |
| 557 | // Initial velocity |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 558 | private int mVelocity; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 559 | |
| 560 | // Current velocity |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 561 | @UnsupportedAppUsage |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 562 | private float mCurrVelocity; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 563 | |
| 564 | // Constant current deceleration |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 565 | private float mDeceleration; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 566 | |
| 567 | // Animation starting time, in system milliseconds |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 568 | private long mStartTime; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 569 | |
| 570 | // Animation duration, in milliseconds |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 571 | private int mDuration; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 572 | |
| 573 | // Duration to complete spline component of animation |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 574 | private int mSplineDuration; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 575 | |
| 576 | // Distance to travel along spline animation |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 577 | private int mSplineDistance; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 578 | |
| 579 | // Whether the animation is currently in progress |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 580 | private boolean mFinished; |
| 581 | |
| 582 | // The allowed overshot distance before boundary is reached. |
| 583 | private int mOver; |
| 584 | |
| 585 | // Fling friction |
| 586 | private float mFlingFriction = ViewConfiguration.getScrollFriction(); |
| 587 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 588 | // Current state of the animation. |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 589 | private int mState = SPLINE; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 590 | |
| 591 | // Constant gravity value, used in the deceleration phase. |
| 592 | private static final float GRAVITY = 2000.0f; |
| 593 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 594 | // A context-specific coefficient adjusted to physical values. |
| 595 | private float mPhysicalCoeff; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 596 | |
Justin Ho | a181bb4 | 2011-09-19 17:52:06 -0700 | [diff] [blame] | 597 | private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); |
| 598 | private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1) |
| 599 | private static final float START_TENSION = 0.5f; |
| 600 | private static final float END_TENSION = 1.0f; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 601 | private static final float P1 = START_TENSION * INFLEXION; |
| 602 | private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); |
| 603 | |
| 604 | private static final int NB_SAMPLES = 100; |
| 605 | private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; |
| 606 | private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 607 | |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 608 | private static final int SPLINE = 0; |
| 609 | private static final int CUBIC = 1; |
| 610 | private static final int BALLISTIC = 2; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 611 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 612 | static { |
| 613 | float x_min = 0.0f; |
| 614 | float y_min = 0.0f; |
| 615 | for (int i = 0; i < NB_SAMPLES; i++) { |
| 616 | final float alpha = (float) i / NB_SAMPLES; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 617 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 618 | float x_max = 1.0f; |
| 619 | float x, tx, coef; |
| 620 | while (true) { |
| 621 | x = x_min + (x_max - x_min) / 2.0f; |
| 622 | coef = 3.0f * x * (1.0f - x); |
| 623 | tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; |
| 624 | if (Math.abs(tx - alpha) < 1E-5) break; |
| 625 | if (tx > alpha) x_max = x; |
| 626 | else x_min = x; |
| 627 | } |
| 628 | SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; |
| 629 | |
| 630 | float y_max = 1.0f; |
| 631 | float y, dy; |
| 632 | while (true) { |
| 633 | y = y_min + (y_max - y_min) / 2.0f; |
| 634 | coef = 3.0f * y * (1.0f - y); |
| 635 | dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; |
| 636 | if (Math.abs(dy - alpha) < 1E-5) break; |
| 637 | if (dy > alpha) y_max = y; |
| 638 | else y_min = y; |
| 639 | } |
| 640 | SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; |
| 641 | } |
| 642 | SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; |
| 643 | } |
| 644 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 645 | void setFriction(float friction) { |
| 646 | mFlingFriction = friction; |
| 647 | } |
| 648 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 649 | SplineOverScroller(Context context) { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 650 | mFinished = true; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 651 | final float ppi = context.getResources().getDisplayMetrics().density * 160.0f; |
| 652 | mPhysicalCoeff = SensorManager.GRAVITY_EARTH // g (m/s^2) |
| 653 | * 39.37f // inch/meter |
| 654 | * ppi |
| 655 | * 0.84f; // look and feel tuning |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void updateScroll(float q) { |
| 659 | mCurrentPosition = mStart + Math.round(q * (mFinal - mStart)); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Get a signed deceleration that will reduce the velocity. |
| 664 | */ |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 665 | static private float getDeceleration(int velocity) { |
| 666 | return velocity > 0 ? -GRAVITY : GRAVITY; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Modifies mDuration to the duration it takes to get from start to newFinal using the |
| 671 | * spline interpolation. The previous duration was needed to get to oldFinal. |
| 672 | */ |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 673 | private void adjustDuration(int start, int oldFinal, int newFinal) { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 674 | final int oldDistance = oldFinal - start; |
| 675 | final int newDistance = newFinal - start; |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 676 | final float x = Math.abs((float) newDistance / oldDistance); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 677 | final int index = (int) (NB_SAMPLES * x); |
| 678 | if (index < NB_SAMPLES) { |
| 679 | final float x_inf = (float) index / NB_SAMPLES; |
| 680 | final float x_sup = (float) (index + 1) / NB_SAMPLES; |
| 681 | final float t_inf = SPLINE_TIME[index]; |
| 682 | final float t_sup = SPLINE_TIME[index + 1]; |
| 683 | final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 684 | mDuration *= timeCoef; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | void startScroll(int start, int distance, int duration) { |
| 689 | mFinished = false; |
| 690 | |
Vladislav Kaznacheev | 84ad7e9 | 2015-08-04 10:57:04 -0700 | [diff] [blame] | 691 | mCurrentPosition = mStart = start; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 692 | mFinal = start + distance; |
| 693 | |
| 694 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 695 | mDuration = duration; |
| 696 | |
| 697 | // Unused |
| 698 | mDeceleration = 0.0f; |
| 699 | mVelocity = 0; |
| 700 | } |
| 701 | |
| 702 | void finish() { |
| 703 | mCurrentPosition = mFinal; |
| 704 | // Not reset since WebView relies on this value for fast fling. |
| 705 | // TODO: restore when WebView uses the fast fling implemented in this class. |
| 706 | // mCurrVelocity = 0.0f; |
| 707 | mFinished = true; |
| 708 | } |
| 709 | |
| 710 | void setFinalPosition(int position) { |
| 711 | mFinal = position; |
| 712 | mFinished = false; |
| 713 | } |
| 714 | |
| 715 | void extendDuration(int extend) { |
| 716 | final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 717 | final int elapsedTime = (int) (time - mStartTime); |
| 718 | mDuration = elapsedTime + extend; |
| 719 | mFinished = false; |
| 720 | } |
| 721 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 722 | boolean springback(int start, int min, int max) { |
| 723 | mFinished = true; |
| 724 | |
Vladislav Kaznacheev | 84ad7e9 | 2015-08-04 10:57:04 -0700 | [diff] [blame] | 725 | mCurrentPosition = mStart = mFinal = start; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 726 | mVelocity = 0; |
| 727 | |
| 728 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 729 | mDuration = 0; |
| 730 | |
| 731 | if (start < min) { |
| 732 | startSpringback(start, min, 0); |
| 733 | } else if (start > max) { |
| 734 | startSpringback(start, max, 0); |
| 735 | } |
| 736 | |
| 737 | return !mFinished; |
| 738 | } |
| 739 | |
| 740 | private void startSpringback(int start, int end, int velocity) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 741 | // mStartTime has been set |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 742 | mFinished = false; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 743 | mState = CUBIC; |
Vladislav Kaznacheev | 160d12e | 2015-06-18 17:30:57 +0300 | [diff] [blame] | 744 | mCurrentPosition = mStart = start; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 745 | mFinal = end; |
| 746 | final int delta = start - end; |
| 747 | mDeceleration = getDeceleration(delta); |
| 748 | // TODO take velocity into account |
| 749 | mVelocity = -delta; // only sign is used |
| 750 | mOver = Math.abs(delta); |
| 751 | mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration)); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void fling(int start, int velocity, int min, int max, int over) { |
| 755 | mOver = over; |
| 756 | mFinished = false; |
| 757 | mCurrVelocity = mVelocity = velocity; |
| 758 | mDuration = mSplineDuration = 0; |
| 759 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 760 | mCurrentPosition = mStart = start; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 761 | |
| 762 | if (start > max || start < min) { |
| 763 | startAfterEdge(start, min, max, velocity); |
| 764 | return; |
| 765 | } |
| 766 | |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 767 | mState = SPLINE; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 768 | double totalDistance = 0.0; |
| 769 | |
| 770 | if (velocity != 0) { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 771 | mDuration = mSplineDuration = getSplineFlingDuration(velocity); |
| 772 | totalDistance = getSplineFlingDistance(velocity); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | mSplineDistance = (int) (totalDistance * Math.signum(velocity)); |
| 776 | mFinal = start + mSplineDistance; |
| 777 | |
| 778 | // Clamp to a valid final position |
| 779 | if (mFinal < min) { |
| 780 | adjustDuration(mStart, mFinal, min); |
| 781 | mFinal = min; |
| 782 | } |
| 783 | |
| 784 | if (mFinal > max) { |
| 785 | adjustDuration(mStart, mFinal, max); |
| 786 | mFinal = max; |
| 787 | } |
| 788 | } |
| 789 | |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 790 | private double getSplineDeceleration(int velocity) { |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 791 | return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff)); |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | private double getSplineFlingDistance(int velocity) { |
| 795 | final double l = getSplineDeceleration(velocity); |
| 796 | final double decelMinusOne = DECELERATION_RATE - 1.0; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 797 | return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l); |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* Returns the duration, expressed in milliseconds */ |
| 801 | private int getSplineFlingDuration(int velocity) { |
| 802 | final double l = getSplineDeceleration(velocity); |
| 803 | final double decelMinusOne = DECELERATION_RATE - 1.0; |
| 804 | return (int) (1000.0 * Math.exp(l / decelMinusOne)); |
| 805 | } |
| 806 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 807 | private void fitOnBounceCurve(int start, int end, int velocity) { |
| 808 | // Simulate a bounce that started from edge |
| 809 | final float durationToApex = - velocity / mDeceleration; |
Vladislav Kaznacheev | 160d12e | 2015-06-18 17:30:57 +0300 | [diff] [blame] | 810 | // The float cast below is necessary to avoid integer overflow. |
| 811 | final float velocitySquared = (float) velocity * velocity; |
| 812 | final float distanceToApex = velocitySquared / 2.0f / Math.abs(mDeceleration); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 813 | final float distanceToEdge = Math.abs(end - start); |
| 814 | final float totalDuration = (float) Math.sqrt( |
| 815 | 2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration)); |
| 816 | mStartTime -= (int) (1000.0f * (totalDuration - durationToApex)); |
Vladislav Kaznacheev | 84ad7e9 | 2015-08-04 10:57:04 -0700 | [diff] [blame] | 817 | mCurrentPosition = mStart = end; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 818 | mVelocity = (int) (- mDeceleration * totalDuration); |
| 819 | } |
| 820 | |
| 821 | private void startBounceAfterEdge(int start, int end, int velocity) { |
| 822 | mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity); |
| 823 | fitOnBounceCurve(start, end, velocity); |
| 824 | onEdgeReached(); |
| 825 | } |
| 826 | |
| 827 | private void startAfterEdge(int start, int min, int max, int velocity) { |
| 828 | if (start > min && start < max) { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 829 | Log.e("OverScroller", "startAfterEdge called from a valid position"); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 830 | mFinished = true; |
| 831 | return; |
| 832 | } |
| 833 | final boolean positive = start > max; |
| 834 | final int edge = positive ? max : min; |
| 835 | final int overDistance = start - edge; |
| 836 | boolean keepIncreasing = overDistance * velocity >= 0; |
| 837 | if (keepIncreasing) { |
| 838 | // Will result in a bounce or a to_boundary depending on velocity. |
| 839 | startBounceAfterEdge(start, edge, velocity); |
| 840 | } else { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 841 | final double totalDistance = getSplineFlingDistance(velocity); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 842 | if (totalDistance > Math.abs(overDistance)) { |
| 843 | fling(start, velocity, positive ? min : start, positive ? start : max, mOver); |
| 844 | } else { |
| 845 | startSpringback(start, edge, velocity); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void notifyEdgeReached(int start, int end, int over) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 851 | // mState is used to detect successive notifications |
| 852 | if (mState == SPLINE) { |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 853 | mOver = over; |
| 854 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 855 | // We were in fling/scroll mode before: current velocity is such that distance to |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 856 | // edge is increasing. This ensures that startAfterEdge will not start a new fling. |
Gilles Debunne | fff4ab0 | 2010-11-23 15:34:37 -0800 | [diff] [blame] | 857 | startAfterEdge(start, end, end, (int) mCurrVelocity); |
| 858 | } |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | private void onEdgeReached() { |
| 862 | // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached. |
Vladislav Kaznacheev | 160d12e | 2015-06-18 17:30:57 +0300 | [diff] [blame] | 863 | // The float cast below is necessary to avoid integer overflow. |
| 864 | final float velocitySquared = (float) mVelocity * mVelocity; |
| 865 | float distance = velocitySquared / (2.0f * Math.abs(mDeceleration)); |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 866 | final float sign = Math.signum(mVelocity); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 867 | |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 868 | if (distance > mOver) { |
| 869 | // Default deceleration is not sufficient to slow us down before boundary |
Vladislav Kaznacheev | 160d12e | 2015-06-18 17:30:57 +0300 | [diff] [blame] | 870 | mDeceleration = - sign * velocitySquared / (2.0f * mOver); |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 871 | distance = mOver; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 872 | } |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 873 | |
| 874 | mOver = (int) distance; |
| 875 | mState = BALLISTIC; |
| 876 | mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance); |
| 877 | mDuration = - (int) (1000.0f * mVelocity / mDeceleration); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | boolean continueWhenFinished() { |
| 881 | switch (mState) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 882 | case SPLINE: |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 883 | // Duration from start to null velocity |
| 884 | if (mDuration < mSplineDuration) { |
| 885 | // If the animation was clamped, we reached the edge |
Vladislav Kaznacheev | 84ad7e9 | 2015-08-04 10:57:04 -0700 | [diff] [blame] | 886 | mCurrentPosition = mStart = mFinal; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 887 | // TODO Better compute speed when edge was reached |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 888 | mVelocity = (int) mCurrVelocity; |
| 889 | mDeceleration = getDeceleration(mVelocity); |
| 890 | mStartTime += mDuration; |
| 891 | onEdgeReached(); |
| 892 | } else { |
| 893 | // Normal stop, no need to continue |
| 894 | return false; |
| 895 | } |
| 896 | break; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 897 | case BALLISTIC: |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 898 | mStartTime += mDuration; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 899 | startSpringback(mFinal, mStart, 0); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 900 | break; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 901 | case CUBIC: |
| 902 | return false; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | update(); |
| 906 | return true; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Update the current position and velocity for current time. Returns |
| 911 | * true if update has been done and false if animation duration has been |
| 912 | * reached. |
| 913 | */ |
| 914 | boolean update() { |
| 915 | final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 916 | final long currentTime = time - mStartTime; |
| 917 | |
Adam Powell | 311506a | 2014-10-31 13:55:58 -0700 | [diff] [blame] | 918 | if (currentTime == 0) { |
| 919 | // Skip work but report that we're still going if we have a nonzero duration. |
| 920 | return mDuration > 0; |
| 921 | } |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 922 | if (currentTime > mDuration) { |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | double distance = 0.0; |
| 927 | switch (mState) { |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 928 | case SPLINE: { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 929 | final float t = (float) currentTime / mSplineDuration; |
| 930 | final int index = (int) (NB_SAMPLES * t); |
| 931 | float distanceCoef = 1.f; |
| 932 | float velocityCoef = 0.f; |
| 933 | if (index < NB_SAMPLES) { |
| 934 | final float t_inf = (float) index / NB_SAMPLES; |
| 935 | final float t_sup = (float) (index + 1) / NB_SAMPLES; |
| 936 | final float d_inf = SPLINE_POSITION[index]; |
| 937 | final float d_sup = SPLINE_POSITION[index + 1]; |
| 938 | velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); |
| 939 | distanceCoef = d_inf + (t - t_inf) * velocityCoef; |
| 940 | } |
| 941 | |
| 942 | distance = distanceCoef * mSplineDistance; |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 943 | mCurrVelocity = velocityCoef * mSplineDistance / mSplineDuration * 1000.0f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 947 | case BALLISTIC: { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 948 | final float t = currentTime / 1000.0f; |
| 949 | mCurrVelocity = mVelocity + mDeceleration * t; |
| 950 | distance = mVelocity * t + mDeceleration * t * t / 2.0f; |
| 951 | break; |
| 952 | } |
| 953 | |
Gilles Debunne | c35a829 | 2010-12-10 14:36:17 -0800 | [diff] [blame] | 954 | case CUBIC: { |
| 955 | final float t = (float) (currentTime) / mDuration; |
| 956 | final float t2 = t * t; |
| 957 | final float sign = Math.signum(mVelocity); |
| 958 | distance = sign * mOver * (3.0f * t2 - 2.0f * t * t2); |
| 959 | mCurrVelocity = sign * mOver * 6.0f * (- t + t2); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | mCurrentPosition = mStart + (int) Math.round(distance); |
Gilles Debunne | 34961cc | 2010-12-14 15:25:36 -0800 | [diff] [blame] | 965 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | } |
| 969 | } |