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