The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 19 | import android.content.Context; |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 20 | import android.hardware.SensorManager; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 21 | import android.os.Build; |
| 22 | import android.util.FloatMath; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.view.ViewConfiguration; |
| 24 | import android.view.animation.AnimationUtils; |
| 25 | import android.view.animation.Interpolator; |
| 26 | |
| 27 | |
| 28 | /** |
Katie McCormick | 87cfad7 | 2013-02-06 18:15:13 -0800 | [diff] [blame] | 29 | * <p>This class encapsulates scrolling. You can use scrollers ({@link Scroller} |
| 30 | * or {@link OverScroller}) to collect the data you need to produce a scrolling |
| 31 | * animation—for example, in response to a fling gesture. Scrollers track |
| 32 | * scroll offsets for you over time, but they don't automatically apply those |
| 33 | * positions to your view. It's your responsibility to get and apply new |
| 34 | * coordinates at a rate that will make the scrolling animation look smooth.</p> |
| 35 | * |
| 36 | * <p>Here is a simple example:</p> |
| 37 | * |
| 38 | * <pre> private Scroller mScroller = new Scroller(context); |
| 39 | * ... |
| 40 | * public void zoomIn() { |
| 41 | * // Revert any animation currently in progress |
| 42 | * mScroller.forceFinished(true); |
| 43 | * // Start scrolling by providing a starting point and |
| 44 | * // the distance to travel |
| 45 | * mScroller.startScroll(0, 0, 100, 0); |
| 46 | * // Invalidate to request a redraw |
| 47 | * invalidate(); |
| 48 | * }</pre> |
| 49 | * |
| 50 | * <p>To track the changing positions of the x/y coordinates, use |
| 51 | * {@link #computeScrollOffset}. The method returns a boolean to indicate |
| 52 | * whether the scroller is finished. If it isn't, it means that a fling or |
| 53 | * programmatic pan operation is still in progress. You can use this method to |
| 54 | * find the current offsets of the x and y coordinates, for example:</p> |
| 55 | * |
| 56 | * <pre>if (mScroller.computeScrollOffset()) { |
| 57 | * // Get current x and y positions |
| 58 | * int currX = mScroller.getCurrX(); |
| 59 | * int currY = mScroller.getCurrY(); |
| 60 | * ... |
| 61 | * }</pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | */ |
| 63 | public class Scroller { |
Alan Viverette | d75e253 | 2013-09-04 14:15:23 -0700 | [diff] [blame] | 64 | private final Interpolator mInterpolator; |
| 65 | |
Adam Powell | 6579b0b | 2010-03-25 12:21:34 -0700 | [diff] [blame] | 66 | private int mMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 68 | private int mStartX; |
| 69 | private int mStartY; |
| 70 | private int mFinalX; |
| 71 | private int mFinalY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 73 | private int mMinX; |
| 74 | private int mMaxX; |
| 75 | private int mMinY; |
| 76 | private int mMaxY; |
| 77 | |
| 78 | private int mCurrX; |
| 79 | private int mCurrY; |
| 80 | private long mStartTime; |
| 81 | private int mDuration; |
| 82 | private float mDurationReciprocal; |
| 83 | private float mDeltaX; |
| 84 | private float mDeltaY; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 85 | private boolean mFinished; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 86 | private boolean mFlywheel; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 87 | |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 88 | private float mVelocity; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 89 | private float mCurrVelocity; |
| 90 | private int mDistance; |
| 91 | |
| 92 | private float mFlingFriction = ViewConfiguration.getScrollFriction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
Adam Powell | 6579b0b | 2010-03-25 12:21:34 -0700 | [diff] [blame] | 94 | private static final int DEFAULT_DURATION = 250; |
| 95 | private static final int SCROLL_MODE = 0; |
| 96 | private static final int FLING_MODE = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 98 | private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); |
| 99 | private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1) |
| 100 | private static final float START_TENSION = 0.5f; |
| 101 | private static final float END_TENSION = 1.0f; |
| 102 | private static final float P1 = START_TENSION * INFLEXION; |
| 103 | private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); |
| 104 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 105 | private static final int NB_SAMPLES = 100; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 106 | private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; |
| 107 | private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 108 | |
Romain Guy | 4bede9e | 2010-10-11 19:36:59 -0700 | [diff] [blame] | 109 | private float mDeceleration; |
| 110 | private final float mPpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 112 | // A context-specific coefficient adjusted to physical values. |
| 113 | private float mPhysicalCoeff; |
| 114 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 115 | static { |
| 116 | float x_min = 0.0f; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 117 | float y_min = 0.0f; |
| 118 | for (int i = 0; i < NB_SAMPLES; i++) { |
| 119 | final float alpha = (float) i / NB_SAMPLES; |
| 120 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 121 | float x_max = 1.0f; |
| 122 | float x, tx, coef; |
| 123 | while (true) { |
| 124 | x = x_min + (x_max - x_min) / 2.0f; |
| 125 | coef = 3.0f * x * (1.0f - x); |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 126 | tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; |
| 127 | if (Math.abs(tx - alpha) < 1E-5) break; |
| 128 | if (tx > alpha) x_max = x; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 129 | else x_min = x; |
| 130 | } |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 131 | SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; |
| 132 | |
| 133 | float y_max = 1.0f; |
| 134 | float y, dy; |
| 135 | while (true) { |
| 136 | y = y_min + (y_max - y_min) / 2.0f; |
| 137 | coef = 3.0f * y * (1.0f - y); |
| 138 | dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; |
| 139 | if (Math.abs(dy - alpha) < 1E-5) break; |
| 140 | if (dy > alpha) y_max = y; |
| 141 | else y_min = y; |
| 142 | } |
| 143 | SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 144 | } |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 145 | SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 146 | } |
| 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 149 | * Create a Scroller with the default duration and interpolator. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | */ |
| 151 | public Scroller(Context context) { |
| 152 | this(context, null); |
| 153 | } |
| 154 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Create a Scroller with the specified interpolator. If the interpolator is |
| 157 | * null, the default (viscous) interpolator will be used. "Flywheel" behavior will |
| 158 | * be in effect for apps targeting Honeycomb or newer. |
| 159 | */ |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 160 | public Scroller(Context context, Interpolator interpolator) { |
| 161 | this(context, interpolator, |
| 162 | context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB); |
| 163 | } |
| 164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Create a Scroller with the specified interpolator. If the interpolator is |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 167 | * null, the default (viscous) interpolator will be used. Specify whether or |
| 168 | * not to support progressive "flywheel" behavior in flinging. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | */ |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 170 | public Scroller(Context context, Interpolator interpolator, boolean flywheel) { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 171 | mFinished = true; |
Alan Viverette | d75e253 | 2013-09-04 14:15:23 -0700 | [diff] [blame] | 172 | if (interpolator == null) { |
| 173 | mInterpolator = new ViscousFluidInterpolator(); |
| 174 | } else { |
| 175 | mInterpolator = interpolator; |
| 176 | } |
Romain Guy | 4bede9e | 2010-10-11 19:36:59 -0700 | [diff] [blame] | 177 | mPpi = context.getResources().getDisplayMetrics().density * 160.0f; |
| 178 | mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction()); |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 179 | mFlywheel = flywheel; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 180 | |
| 181 | mPhysicalCoeff = computeDeceleration(0.84f); // look and feel tuning |
Romain Guy | 4bede9e | 2010-10-11 19:36:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * The amount of friction applied to flings. The default value |
| 186 | * is {@link ViewConfiguration#getScrollFriction}. |
| 187 | * |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 188 | * @param friction A scalar dimension-less value representing the coefficient of |
Romain Guy | 4bede9e | 2010-10-11 19:36:59 -0700 | [diff] [blame] | 189 | * friction. |
| 190 | */ |
| 191 | public final void setFriction(float friction) { |
Romain Guy | a655c63 | 2010-10-12 10:19:25 -0700 | [diff] [blame] | 192 | mDeceleration = computeDeceleration(friction); |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 193 | mFlingFriction = friction; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 195 | |
Romain Guy | 4bede9e | 2010-10-11 19:36:59 -0700 | [diff] [blame] | 196 | private float computeDeceleration(float friction) { |
| 197 | return SensorManager.GRAVITY_EARTH // g (m/s^2) |
| 198 | * 39.37f // inch/meter |
| 199 | * mPpi // pixels per inch |
| 200 | * friction; |
| 201 | } |
| 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | /** |
| 204 | * |
| 205 | * Returns whether the scroller has finished scrolling. |
| 206 | * |
| 207 | * @return True if the scroller has finished scrolling, false otherwise. |
| 208 | */ |
| 209 | public final boolean isFinished() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 210 | return mFinished; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Force the finished field to a particular value. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 215 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | * @param finished The new finished value. |
| 217 | */ |
| 218 | public final void forceFinished(boolean finished) { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 219 | mFinished = finished; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Returns how long the scroll event will take, in milliseconds. |
| 224 | * |
| 225 | * @return The duration of the scroll in milliseconds. |
| 226 | */ |
| 227 | public final int getDuration() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 228 | return mDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 232 | * Returns the current X offset in the scroll. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * |
| 234 | * @return The new X offset as an absolute distance from the origin. |
| 235 | */ |
| 236 | public final int getCurrX() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 237 | return mCurrX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 241 | * Returns the current Y offset in the scroll. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | * |
| 243 | * @return The new Y offset as an absolute distance from the origin. |
| 244 | */ |
| 245 | public final int getCurrY() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 246 | return mCurrY; |
Cary Clark | 278ce05 | 2009-08-31 16:08:42 -0400 | [diff] [blame] | 247 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 248 | |
Cary Clark | 278ce05 | 2009-08-31 16:08:42 -0400 | [diff] [blame] | 249 | /** |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 250 | * Returns the current velocity. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 251 | * |
| 252 | * @return The original velocity less the deceleration. Result may be |
| 253 | * negative. |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 254 | */ |
| 255 | public float getCurrVelocity() { |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 256 | return mMode == FLING_MODE ? |
| 257 | mCurrVelocity : mVelocity - mDeceleration * timePassed() / 2000.0f; |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 261 | * Returns the start X offset in the scroll. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | * |
| 263 | * @return The start X offset as an absolute distance from the origin. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | */ |
| 265 | public final int getStartX() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 266 | return mStartX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 270 | * Returns the start Y offset in the scroll. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | * |
| 272 | * @return The start Y offset as an absolute distance from the origin. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | */ |
| 274 | public final int getStartY() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 275 | return mStartY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Returns where the scroll will end. Valid only for "fling" scrolls. |
| 280 | * |
| 281 | * @return The final X offset as an absolute distance from the origin. |
| 282 | */ |
| 283 | public final int getFinalX() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 284 | return mFinalX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Returns where the scroll will end. Valid only for "fling" scrolls. |
| 289 | * |
| 290 | * @return The final Y offset as an absolute distance from the origin. |
| 291 | */ |
| 292 | public final int getFinalY() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 293 | return mFinalY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 297 | * Call this when you want to know the new location. If it returns true, |
Katie McCormick | 87cfad7 | 2013-02-06 18:15:13 -0800 | [diff] [blame] | 298 | * the animation is not yet finished. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 299 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | public boolean computeScrollOffset() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 301 | if (mFinished) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 305 | int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime); |
| 306 | |
| 307 | if (timePassed < mDuration) { |
| 308 | switch (mMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | case SCROLL_MODE: |
Alan Viverette | d75e253 | 2013-09-04 14:15:23 -0700 | [diff] [blame] | 310 | final float x = mInterpolator.getInterpolation(timePassed * mDurationReciprocal); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 311 | mCurrX = mStartX + Math.round(x * mDeltaX); |
| 312 | mCurrY = mStartY + Math.round(x * mDeltaY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | break; |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 314 | case FLING_MODE: |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 315 | final float t = (float) timePassed / mDuration; |
| 316 | final int index = (int) (NB_SAMPLES * t); |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 317 | float distanceCoef = 1.f; |
| 318 | float velocityCoef = 0.f; |
| 319 | if (index < NB_SAMPLES) { |
| 320 | final float t_inf = (float) index / NB_SAMPLES; |
| 321 | final float t_sup = (float) (index + 1) / NB_SAMPLES; |
| 322 | final float d_inf = SPLINE_POSITION[index]; |
| 323 | final float d_sup = SPLINE_POSITION[index + 1]; |
| 324 | velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); |
| 325 | distanceCoef = d_inf + (t - t_inf) * velocityCoef; |
| 326 | } |
| 327 | |
| 328 | mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 329 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 330 | mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX)); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 331 | // Pin to mMinX <= mCurrX <= mMaxX |
| 332 | mCurrX = Math.min(mCurrX, mMaxX); |
| 333 | mCurrX = Math.max(mCurrX, mMinX); |
| 334 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 335 | mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY)); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 336 | // Pin to mMinY <= mCurrY <= mMaxY |
| 337 | mCurrY = Math.min(mCurrY, mMaxY); |
| 338 | mCurrY = Math.max(mCurrY, mMinY); |
Adam Powell | 1b088be | 2010-07-23 15:49:03 -0700 | [diff] [blame] | 339 | |
| 340 | if (mCurrX == mFinalX && mCurrY == mFinalY) { |
| 341 | mFinished = true; |
| 342 | } |
| 343 | |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 344 | break; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 347 | else { |
| 348 | mCurrX = mFinalX; |
| 349 | mCurrY = mFinalY; |
| 350 | mFinished = true; |
| 351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | return true; |
| 353 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Start scrolling by providing a starting point and the distance to travel. |
| 357 | * The scroll will use the default value of 250 milliseconds for the |
| 358 | * duration. |
| 359 | * |
| 360 | * @param startX Starting horizontal scroll offset in pixels. Positive |
| 361 | * numbers will scroll the content to the left. |
| 362 | * @param startY Starting vertical scroll offset in pixels. Positive numbers |
| 363 | * will scroll the content up. |
| 364 | * @param dx Horizontal distance to travel. Positive numbers will scroll the |
| 365 | * content to the left. |
| 366 | * @param dy Vertical distance to travel. Positive numbers will scroll the |
| 367 | * content up. |
| 368 | */ |
| 369 | public void startScroll(int startX, int startY, int dx, int dy) { |
| 370 | startScroll(startX, startY, dx, dy, DEFAULT_DURATION); |
| 371 | } |
| 372 | |
| 373 | /** |
Katie McCormick | 87cfad7 | 2013-02-06 18:15:13 -0800 | [diff] [blame] | 374 | * Start scrolling by providing a starting point, the distance to travel, |
| 375 | * and the duration of the scroll. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | * |
| 377 | * @param startX Starting horizontal scroll offset in pixels. Positive |
| 378 | * numbers will scroll the content to the left. |
| 379 | * @param startY Starting vertical scroll offset in pixels. Positive numbers |
| 380 | * will scroll the content up. |
| 381 | * @param dx Horizontal distance to travel. Positive numbers will scroll the |
| 382 | * content to the left. |
| 383 | * @param dy Vertical distance to travel. Positive numbers will scroll the |
| 384 | * content up. |
| 385 | * @param duration Duration of the scroll in milliseconds. |
| 386 | */ |
| 387 | public void startScroll(int startX, int startY, int dx, int dy, int duration) { |
| 388 | mMode = SCROLL_MODE; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 389 | mFinished = false; |
| 390 | mDuration = duration; |
| 391 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 392 | mStartX = startX; |
| 393 | mStartY = startY; |
| 394 | mFinalX = startX + dx; |
| 395 | mFinalY = startY + dy; |
| 396 | mDeltaX = dx; |
| 397 | mDeltaY = dy; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 398 | mDurationReciprocal = 1.0f / (float) mDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 402 | * Start scrolling based on a fling gesture. The distance travelled will |
| 403 | * depend on the initial velocity of the fling. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | * |
| 405 | * @param startX Starting point of the scroll (X) |
| 406 | * @param startY Starting point of the scroll (Y) |
| 407 | * @param velocityX Initial velocity of the fling (X) measured in pixels per |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 408 | * second. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | * @param velocityY Initial velocity of the fling (Y) measured in pixels per |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 410 | * second |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | * @param minX Minimum X value. The scroller will not scroll past this |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 412 | * point. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | * @param maxX Maximum X value. The scroller will not scroll past this |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 414 | * point. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | * @param minY Minimum Y value. The scroller will not scroll past this |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 416 | * point. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | * @param maxY Maximum Y value. The scroller will not scroll past this |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 418 | * point. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | */ |
| 420 | public void fling(int startX, int startY, int velocityX, int velocityY, |
| 421 | int minX, int maxX, int minY, int maxY) { |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 422 | // Continue a scroll or fling in progress |
| 423 | if (mFlywheel && !mFinished) { |
| 424 | float oldVel = getCurrVelocity(); |
| 425 | |
| 426 | float dx = (float) (mFinalX - mStartX); |
| 427 | float dy = (float) (mFinalY - mStartY); |
| 428 | float hyp = FloatMath.sqrt(dx * dx + dy * dy); |
| 429 | |
| 430 | float ndx = dx / hyp; |
| 431 | float ndy = dy / hyp; |
| 432 | |
| 433 | float oldVelocityX = ndx * oldVel; |
| 434 | float oldVelocityY = ndy * oldVel; |
| 435 | if (Math.signum(velocityX) == Math.signum(oldVelocityX) && |
| 436 | Math.signum(velocityY) == Math.signum(oldVelocityY)) { |
| 437 | velocityX += oldVelocityX; |
| 438 | velocityY += oldVelocityY; |
| 439 | } |
| 440 | } |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | mMode = FLING_MODE; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 443 | mFinished = false; |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 444 | |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 445 | float velocity = FloatMath.sqrt(velocityX * velocityX + velocityY * velocityY); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 446 | |
| 447 | mVelocity = velocity; |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 448 | mDuration = getSplineFlingDuration(velocity); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 449 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 450 | mStartX = startX; |
| 451 | mStartY = startY; |
| 452 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 453 | float coeffX = velocity == 0 ? 1.0f : velocityX / velocity; |
| 454 | float coeffY = velocity == 0 ? 1.0f : velocityY / velocity; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 455 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 456 | double totalDistance = getSplineFlingDistance(velocity); |
| 457 | mDistance = (int) (totalDistance * Math.signum(velocity)); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 458 | |
| 459 | mMinX = minX; |
| 460 | mMaxX = maxX; |
| 461 | mMinY = minY; |
| 462 | mMaxY = maxY; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 463 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 464 | mFinalX = startX + (int) Math.round(totalDistance * coeffX); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 465 | // Pin to mMinX <= mFinalX <= mMaxX |
| 466 | mFinalX = Math.min(mFinalX, mMaxX); |
| 467 | mFinalX = Math.max(mFinalX, mMinX); |
| 468 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 469 | mFinalY = startY + (int) Math.round(totalDistance * coeffY); |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 470 | // Pin to mMinY <= mFinalY <= mMaxY |
| 471 | mFinalY = Math.min(mFinalY, mMaxY); |
| 472 | mFinalY = Math.max(mFinalY, mMinY); |
| 473 | } |
| 474 | |
Adam Powell | 990dfc6f | 2012-08-09 16:45:59 -0700 | [diff] [blame] | 475 | private double getSplineDeceleration(float velocity) { |
| 476 | return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff)); |
| 477 | } |
| 478 | |
| 479 | private int getSplineFlingDuration(float velocity) { |
| 480 | final double l = getSplineDeceleration(velocity); |
| 481 | final double decelMinusOne = DECELERATION_RATE - 1.0; |
| 482 | return (int) (1000.0 * Math.exp(l / decelMinusOne)); |
| 483 | } |
| 484 | |
| 485 | private double getSplineFlingDistance(float velocity) { |
| 486 | final double l = getSplineDeceleration(velocity); |
| 487 | final double decelMinusOne = DECELERATION_RATE - 1.0; |
| 488 | return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l); |
| 489 | } |
| 490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | /** |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 492 | * Stops the animation. Contrary to {@link #forceFinished(boolean)}, |
| 493 | * aborting the animating cause the scroller to move to the final x and y |
| 494 | * position |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 495 | * |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 496 | * @see #forceFinished(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | */ |
| 498 | public void abortAnimation() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 499 | mCurrX = mFinalX; |
| 500 | mCurrY = mFinalY; |
| 501 | mFinished = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | /** |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 505 | * Extend the scroll animation. This allows a running animation to scroll |
djken | d663dab | 2009-05-08 07:07:55 -0400 | [diff] [blame] | 506 | * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 507 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | * @param extend Additional time to scroll in milliseconds. |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 509 | * @see #setFinalX(int) |
| 510 | * @see #setFinalY(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | */ |
| 512 | public void extendDuration(int extend) { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 513 | int passed = timePassed(); |
| 514 | mDuration = passed + extend; |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 515 | mDurationReciprocal = 1.0f / mDuration; |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 516 | mFinished = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * Returns the time elapsed since the beginning of the scrolling. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 521 | * |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 522 | * @return The elapsed time in milliseconds. |
| 523 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | public int timePassed() { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 525 | return (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | } |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * Sets the final position (X) for this scroller. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 530 | * |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 531 | * @param newX The new X offset as an absolute distance from the origin. |
| 532 | * @see #extendDuration(int) |
| 533 | * @see #setFinalY(int) |
| 534 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | public void setFinalX(int newX) { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 536 | mFinalX = newX; |
| 537 | mDeltaX = mFinalX - mStartX; |
| 538 | mFinished = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 541 | /** |
| 542 | * Sets the final position (Y) for this scroller. |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 543 | * |
Cyril Mottier | c312942 | 2009-05-05 18:13:48 +0200 | [diff] [blame] | 544 | * @param newY The new Y offset as an absolute distance from the origin. |
| 545 | * @see #extendDuration(int) |
| 546 | * @see #setFinalX(int) |
| 547 | */ |
| 548 | public void setFinalY(int newY) { |
Adam Powell | 9d32d24 | 2010-03-29 16:02:07 -0700 | [diff] [blame] | 549 | mFinalY = newY; |
| 550 | mDeltaY = mFinalY - mStartY; |
| 551 | mFinished = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
Gilles Debunne | d348bb4 | 2010-11-15 12:19:35 -0800 | [diff] [blame] | 553 | |
| 554 | /** |
| 555 | * @hide |
| 556 | */ |
| 557 | public boolean isScrollingInDirection(float xvel, float yvel) { |
| 558 | return !mFinished && Math.signum(xvel) == Math.signum(mFinalX - mStartX) && |
| 559 | Math.signum(yvel) == Math.signum(mFinalY - mStartY); |
| 560 | } |
Alan Viverette | d75e253 | 2013-09-04 14:15:23 -0700 | [diff] [blame] | 561 | |
Alan Viverette | 79a5fef | 2013-09-04 16:37:57 -0700 | [diff] [blame] | 562 | static class ViscousFluidInterpolator implements Interpolator { |
Alan Viverette | d75e253 | 2013-09-04 14:15:23 -0700 | [diff] [blame] | 563 | /** Controls the viscous fluid effect (how much of it). */ |
| 564 | private static final float VISCOUS_FLUID_SCALE = 8.0f; |
| 565 | |
| 566 | private static final float VISCOUS_FLUID_NORMALIZE; |
| 567 | private static final float VISCOUS_FLUID_OFFSET; |
| 568 | |
| 569 | static { |
| 570 | |
| 571 | // must be set to 1.0 (used in viscousFluid()) |
| 572 | VISCOUS_FLUID_NORMALIZE = 1.0f / viscousFluid(1.0f); |
| 573 | // account for very small floating-point error |
| 574 | VISCOUS_FLUID_OFFSET = 1.0f - VISCOUS_FLUID_NORMALIZE * viscousFluid(1.0f); |
| 575 | } |
| 576 | |
| 577 | private static float viscousFluid(float x) { |
| 578 | x *= VISCOUS_FLUID_SCALE; |
| 579 | if (x < 1.0f) { |
| 580 | x -= (1.0f - (float)Math.exp(-x)); |
| 581 | } else { |
| 582 | float start = 0.36787944117f; // 1/e == exp(-1) |
| 583 | x = 1.0f - (float)Math.exp(1.0f - x); |
| 584 | x = start + x * (1.0f - start); |
| 585 | } |
| 586 | return x; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public float getInterpolation(float input) { |
| 591 | final float interpolated = VISCOUS_FLUID_NORMALIZE * viscousFluid(input); |
| 592 | if (input > 0) { |
| 593 | return input + VISCOUS_FLUID_OFFSET; |
| 594 | } |
| 595 | return input; |
| 596 | } |
| 597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | } |