OverScroller.java revision c35a829b10946fe88b53fd3354b55218ecaff00e
1637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell/* 2637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Copyright (C) 2010 The Android Open Source Project 3637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 4637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * you may not use this file except in compliance with the License. 6637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * You may obtain a copy of the License at 7637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 8637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 9637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 10637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Unless required by applicable law or agreed to in writing, software 11637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * See the License for the specific language governing permissions and 14637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * limitations under the License. 15637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 16637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 17637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellpackage android.widget; 18637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 19637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.content.Context; 20fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.hardware.SensorManager; 21fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.util.FloatMath; 22fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.util.Log; 23637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.view.ViewConfiguration; 24637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.view.animation.AnimationUtils; 25fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.view.animation.Interpolator; 26637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 27637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell/** 28637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * This class encapsulates scrolling with the ability to overshoot the bounds 29637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * of a scrolling operation. This class is a drop-in replacement for 30637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link android.widget.Scroller} in most cases. 31637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 32637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellpublic class OverScroller { 33fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mMode; 34637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 35fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private final SplineOverScroller mScrollerX; 36fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private final SplineOverScroller mScrollerY; 37637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 38fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private final Interpolator mInterpolator; 39637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 40fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private final boolean mFlywheel; 41637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 42637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private static final int DEFAULT_DURATION = 250; 43637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private static final int SCROLL_MODE = 0; 44637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private static final int FLING_MODE = 1; 45637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 46fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 47c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Creates an OverScroller with a viscous fluid scroll interpolator and flywheel. 48fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context 49fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 50fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne public OverScroller(Context context) { 51fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne this(context, null); 52fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 53637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 54fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 55c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Creates an OverScroller with flywheel enabled. 56fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context The context of this application. 57fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 58fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * be used. 59fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 60fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne public OverScroller(Context context, Interpolator interpolator) { 61c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, true); 62c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne } 63c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 64c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne /** 65c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Creates an OverScroller. 66c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param context The context of this application. 67c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 68c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * be used. 69c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param flywheel If true, successive fling motions will keep on increasing scroll speed. 70c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne */ 71c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne public OverScroller(Context context, Interpolator interpolator, boolean flywheel) { 72c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mInterpolator = interpolator; 73c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFlywheel = flywheel; 74c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerX = new SplineOverScroller(); 75c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerY = new SplineOverScroller(); 76c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 77c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne SplineOverScroller.initFromContext(context); 78637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 79637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 80fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 81fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * Creates an OverScroller with flywheel enabled. 82fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context The context of this application. 83fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 84fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * be used. 85fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 86fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 87c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 88c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 89c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 90c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @deprecated Use {@link #OverScroller(Context, Interpolator, boolean)} instead. 91fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 92c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne @Deprecated 93fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne public OverScroller(Context context, Interpolator interpolator, 94fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float bounceCoefficientX, float bounceCoefficientY) { 95c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, true); 96637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 97637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 98637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 99637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Creates an OverScroller. 100637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param context The context of this application. 101637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 102637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * be used. 103637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 104637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 105c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 106c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 107c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 108fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param flywheel If true, successive fling motions will keep on increasing scroll speed. 109c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @deprecated Use {@link #OverScroller(Context, Interpolator, boolean)} instead. 110637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 111c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne @Deprecated 112637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public OverScroller(Context context, Interpolator interpolator, 113637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) { 114c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, flywheel); 115637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 116637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 117637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 118637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The amount of friction applied to flings. The default value 119637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * is {@link ViewConfiguration#getScrollFriction}. 120637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 121637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param friction A scalar dimension-less value representing the coefficient of 122637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * friction. 123637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 124637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void setFriction(float friction) { 125fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerX.setFriction(friction); 126fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerY.setFriction(friction); 127637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 128637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 129637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 130637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 131637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the scroller has finished scrolling. 132637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 133637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return True if the scroller has finished scrolling, false otherwise. 134637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 135637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final boolean isFinished() { 136637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinished && mScrollerY.mFinished; 137637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 138637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 139637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 140637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Force the finished field to a particular value. Contrary to 141637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #abortAnimation()}, forcing the animation to finished 142637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * does NOT cause the scroller to move to the final x and y 143637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * position. 144637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 145637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finished The new finished value. 146637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 147637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void forceFinished(boolean finished) { 148637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.mFinished = mScrollerY.mFinished = finished; 149637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 150637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 151637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 152637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current X offset in the scroll. 153637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 154637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new X offset as an absolute distance from the origin. 155637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 156637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrX() { 157637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mCurrentPosition; 158637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 159637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 160637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 161637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current Y offset in the scroll. 162637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 163637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new Y offset as an absolute distance from the origin. 164637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 165637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrY() { 166637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mCurrentPosition; 167637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 168637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 169637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 170637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 171c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Returns the absolute value of the current velocity. 172637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 173637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The original velocity less the deceleration, norm of the X and Y velocity vector. 174637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 175637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public float getCurrVelocity() { 176637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity; 177637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity; 178fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return FloatMath.sqrt(squaredNorm); 179637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 180637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 181637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 182637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start X offset in the scroll. 183637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 184637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start X offset as an absolute distance from the origin. 185637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 186637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartX() { 187637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mStart; 188637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 189637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 190637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 191637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start Y offset in the scroll. 192637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 193637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start Y offset as an absolute distance from the origin. 194637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 195637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartY() { 196637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mStart; 197637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 198637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 199637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 200637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 201637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 202637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final X offset as an absolute distance from the origin. 203637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 204637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalX() { 205637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinal; 206637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 207637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 208637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 209637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 210637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 211637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final Y offset as an absolute distance from the origin. 212637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 213637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalY() { 214637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mFinal; 215637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 216637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 217637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 218637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns how long the scroll event will take, in milliseconds. 219637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 220637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The duration of the scroll in milliseconds. 221637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 222637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 223637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 224637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * This function will lie to the best of its ability. 225637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 226637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 227637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getDuration() { 228637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return Math.max(mScrollerX.mDuration, mScrollerY.mDuration); 229637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 230637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 231637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 232637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Extend the scroll animation. This allows a running animation to scroll 233637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}. 234637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 235637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param extend Additional time to scroll in milliseconds. 236637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 237637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 238637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 239637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 240637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 241637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 242637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 243637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 244637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 245637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 246637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void extendDuration(int extend) { 247637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.extendDuration(extend); 248637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.extendDuration(extend); 249637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 250637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 251637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 252637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (X) for this scroller. 253637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 254637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newX The new X offset as an absolute distance from the origin. 255637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 256637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 257637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 258637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 259637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 260637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 261637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 262637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 263637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 264637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 265637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalX(int newX) { 266637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.setFinalPosition(newX); 267637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 268637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 269637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 270637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (Y) for this scroller. 271637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 272637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newY The new Y offset as an absolute distance from the origin. 273637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 274637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 275637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 276637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 277637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 278637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 279637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 280637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 281637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 282637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 283637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalY(int newY) { 284637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.setFinalPosition(newY); 285637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 286637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 287637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 288637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to know the new location. If it returns true, the 289637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animation is not yet finished. 290637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 291637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean computeScrollOffset() { 292637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (isFinished()) { 293637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 294637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 295637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 296637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mMode) { 297637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case SCROLL_MODE: 298637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell long time = AnimationUtils.currentAnimationTimeMillis(); 299637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Any scroller can be used for time, since they were started 300637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // together in scroll mode. We use X here. 301637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long elapsedTime = time - mScrollerX.mStartTime; 302637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 303637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int duration = mScrollerX.mDuration; 304637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (elapsedTime < duration) { 305637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float q = (float) (elapsedTime) / duration; 306637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 307fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (mInterpolator == null) { 308fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne q = Scroller.viscousFluid(q); 309fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } else { 310fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne q = mInterpolator.getInterpolation(q); 311fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 312637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 313637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.updateScroll(q); 314637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.updateScroll(q); 315637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 316637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell abortAnimation(); 317637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 318637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 319637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 320637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case FLING_MODE: 321637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.mFinished) { 322637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.update()) { 323637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.continueWhenFinished()) { 324637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 325637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 326637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 327637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 328637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 329637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.mFinished) { 330637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.update()) { 331637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.continueWhenFinished()) { 332637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 333637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 334637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 335637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 336637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 337637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 338637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 339637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 340637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 341637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 342637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 343637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 344637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 345637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The scroll will use the default value of 250 milliseconds for the 346637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * duration. 347637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 348637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 349637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 350637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 351637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 352637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 353637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 354637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 355637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 356637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 357637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy) { 358637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startScroll(startX, startY, dx, dy, DEFAULT_DURATION); 359637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 360637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 361637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 362637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 363637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 364637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 365637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 366637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 367637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 368637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 369637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 370637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 371637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 372637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param duration Duration of the scroll in milliseconds. 373637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 374637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy, int duration) { 375637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = SCROLL_MODE; 376637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.startScroll(startX, dx, duration); 377637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.startScroll(startY, dy, duration); 378637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 379637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 380637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 381637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to 'spring back' into a valid coordinate range. 382637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 383637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting X coordinate 384637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting Y coordinate 385637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum valid X value 386637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum valid X value 387637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum valid Y value 388637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Minimum valid Y value 389637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true if a springback was initiated, false if startX and startY were 390637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * already within the valid range. 391637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 392637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean springBack(int startX, int startY, int minX, int maxX, int minY, int maxY) { 393637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 394637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 395637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Make sure both methods are called. 396637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackX = mScrollerX.springback(startX, minX, maxX); 397637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackY = mScrollerY.springback(startY, minY, maxY); 398637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return spingbackX || spingbackY; 399637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 400637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 401637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 402637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY) { 403637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0); 404637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 405637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 406637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 407637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling based on a fling gesture. The distance traveled will 408637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * depend on the initial velocity of the fling. 409637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 410637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting point of the scroll (X) 411637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting point of the scroll (Y) 412637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityX Initial velocity of the fling (X) measured in pixels per 413637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second. 414637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityY Initial velocity of the fling (Y) measured in pixels per 415637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second 416637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum X value. The scroller will not scroll past this point 417637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use minX as 418637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 419637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum X value. The scroller will not scroll past this point 420637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use maxX as 421637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 422637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum Y value. The scroller will not scroll past this point 423637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use minY as 424637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 425637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Maximum Y value. The scroller will not scroll past this point 426637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use maxY as 427637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 428637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Overfling range. If > 0, horizontal overfling in either 429637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 430637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Overfling range. If > 0, vertical overfling in either 431637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 432637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 433637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 434637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY, int overX, int overY) { 435637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Continue a scroll or fling in progress 436637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFlywheel && !isFinished()) { 437637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityX = mScrollerX.mCurrVelocity; 438637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityY = mScrollerY.mCurrVelocity; 439637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (Math.signum(velocityX) == Math.signum(oldVelocityX) && 440637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell Math.signum(velocityY) == Math.signum(oldVelocityY)) { 441637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityX += oldVelocityX; 442637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityY += oldVelocityY; 443637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 444637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 445637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 446637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 447637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.fling(startX, velocityX, minX, maxX, overX); 448637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.fling(startY, velocityY, minY, maxY, overY); 449637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 450637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 451637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 452637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a horizontal boundary. 453637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 454637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 455637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 456637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will transition the current motion and 457637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animate from startX to finalX as appropriate. 458637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 459637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting/current X position 460637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalX Desired final X position 461637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Magnitude of overscroll allowed. This should be the maximum 462637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalX. Absolute value - must be positive. 463637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 464637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyHorizontalEdgeReached(int startX, int finalX, int overX) { 465637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.notifyEdgeReached(startX, finalX, overX); 466637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 467637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 468637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 469637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a vertical boundary. 470637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 471637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 472637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 473637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will animate a parabolic motion from 474637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * startY to finalY. 475637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 476637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting/current Y position 477637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalY Desired final Y position 478637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Magnitude of overscroll allowed. This should be the maximum 479637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalY. Absolute value - must be positive. 480637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 481637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyVerticalEdgeReached(int startY, int finalY, int overY) { 482637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.notifyEdgeReached(startY, finalY, overY); 483637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 484637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 485637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 486637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the current Scroller is currently returning to a valid position. 487637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Valid bounds were provided by the 488637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method. 489637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 490637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * One should check this value before calling 491637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #startScroll(int, int, int, int)} as the interpolation currently in progress 492637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to restore a valid position will then be stopped. The caller has to take into account 493637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the fact that the started scroll will start from an overscrolled position. 494637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 495637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true when the current position is overscrolled and in the process of 496637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * interpolating back to a valid value. 497637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 498637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isOverScrolled() { 499637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return ((!mScrollerX.mFinished && 500c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerX.mState != SplineOverScroller.SPLINE) || 501637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell (!mScrollerY.mFinished && 502c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerY.mState != SplineOverScroller.SPLINE)); 503637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 504637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 505637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 506637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Stops the animation. Contrary to {@link #forceFinished(boolean)}, 507637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * aborting the animating causes the scroller to move to the final x and y 508637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * positions. 509637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 510637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #forceFinished(boolean) 511637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 512637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void abortAnimation() { 513637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 514637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 515637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 516637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 517637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 518637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the time elapsed since the beginning of the scrolling. 519637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 520637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The elapsed time in milliseconds. 521637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 522637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 523637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 524637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public int timePassed() { 525637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 526637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime); 527637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return (int) (time - startTime); 528637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 529637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 530637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 531637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 532637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 533637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isScrollingInDirection(float xvel, float yvel) { 534637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dx = mScrollerX.mFinal - mScrollerX.mStart; 535637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dy = mScrollerY.mFinal - mScrollerY.mStart; 536637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !isFinished() && Math.signum(xvel) == Math.signum(dx) && 537637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell Math.signum(yvel) == Math.signum(dy); 538637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 539637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 540fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static class SplineOverScroller { 541637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial position 542fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mStart; 543637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 544637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current position 545fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mCurrentPosition; 546637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 547637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Final position 548fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mFinal; 549637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 550637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial velocity 551fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mVelocity; 552637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 553637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current velocity 554fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mCurrVelocity; 555637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 556637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Constant current deceleration 557fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mDeceleration; 558637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 559637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation starting time, in system milliseconds 560fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private long mStartTime; 561637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 562637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation duration, in milliseconds 563fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mDuration; 564637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 565637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration to complete spline component of animation 566fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDuration; 567637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 568637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Distance to travel along spline animation 569fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDistance; 570637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 571637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Whether the animation is currently in progress 572fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private boolean mFinished; 573637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 574fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // The allowed overshot distance before boundary is reached. 575fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mOver; 576fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 577fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Fling friction 578fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mFlingFriction = ViewConfiguration.getScrollFriction(); 579fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 580fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Current state of the animation. 581c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private int mState = SPLINE; 582637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 583fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Constant gravity value, used in the deceleration phase. 584fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float GRAVITY = 2000.0f; 585fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 586fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // A device specific coefficient adjusted to physical values. 587fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static float PHYSICAL_COEF; 588fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 589fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static float DECELERATION_RATE = (float) (Math.log(0.75) / Math.log(0.9)); 590c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final float INFLEXION = 0.4f; // Tension lines cross at (INFLEXION, 1) 591c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final float START_TENSION = 1.0f; 592c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final float END_TENSION = 0.6666f; 593fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P1 = START_TENSION * INFLEXION; 594fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); 595fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 596fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final int NB_SAMPLES = 100; 597fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; 598fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; 599fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 600c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int SPLINE = 0; 601c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int CUBIC = 1; 602c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int BALLISTIC = 2; 603637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 604fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static { 605fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_min = 0.0f; 606fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_min = 0.0f; 607fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne for (int i = 0; i < NB_SAMPLES; i++) { 608fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float alpha = (float) i / NB_SAMPLES; 609fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 610fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_max = 1.0f; 611fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x, tx, coef; 612fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 613fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne x = x_min + (x_max - x_min) / 2.0f; 614fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * x * (1.0f - x); 615fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; 616fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(tx - alpha) < 1E-5) break; 617fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (tx > alpha) x_max = x; 618fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else x_min = x; 619fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 620fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; 621fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 622fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_max = 1.0f; 623fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y, dy; 624fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 625fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne y = y_min + (y_max - y_min) / 2.0f; 626fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * y * (1.0f - y); 627fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; 628fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(dy - alpha) < 1E-5) break; 629fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (dy > alpha) y_max = y; 630fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else y_min = y; 631fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 632fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; 633fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 634fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; 635fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 636637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 637fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static void initFromContext(Context context) { 638fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float ppi = context.getResources().getDisplayMetrics().density * 160.0f; 639fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne PHYSICAL_COEF = SensorManager.GRAVITY_EARTH // g (m/s^2) 640fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * 39.37f // inch/meter 641fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * ppi 642fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * 0.84f; // look and feel tuning 643fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 644fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 645fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne void setFriction(float friction) { 646fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mFlingFriction = friction; 647fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 648fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 649fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SplineOverScroller() { 650637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 651637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 652637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 653637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void updateScroll(float q) { 654637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + Math.round(q * (mFinal - mStart)); 655637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 656637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 657637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 658637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Get a signed deceleration that will reduce the velocity. 659637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 660fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static private float getDeceleration(int velocity) { 661fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return velocity > 0 ? -GRAVITY : GRAVITY; 662637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 663637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 664637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 665637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Modifies mDuration to the duration it takes to get from start to newFinal using the 666637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * spline interpolation. The previous duration was needed to get to oldFinal. 667637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 668fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private void adjustDuration(int start, int oldFinal, int newFinal) { 669637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int oldDistance = oldFinal - start; 670637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int newDistance = newFinal - start; 671fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float x = Math.abs((float) newDistance / oldDistance); 672637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * x); 673637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 674637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_inf = (float) index / NB_SAMPLES; 675637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_sup = (float) (index + 1) / NB_SAMPLES; 676637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = SPLINE_TIME[index]; 677637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = SPLINE_TIME[index + 1]; 678637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf); 679637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration *= timeCoef; 680637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 681637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 682637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 683637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void startScroll(int start, int distance, int duration) { 684637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 685637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 686637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = start; 687637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + distance; 688637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 689637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 690637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = duration; 691637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 692637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Unused 693637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = 0.0f; 694637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 695637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 696637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 697637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void finish() { 698637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mFinal; 699637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Not reset since WebView relies on this value for fast fling. 700637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // TODO: restore when WebView uses the fast fling implemented in this class. 701637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mCurrVelocity = 0.0f; 702637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 703637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 704637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 705637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void setFinalPosition(int position) { 706637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = position; 707637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 708637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 709637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 710637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void extendDuration(int extend) { 711637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 712637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int elapsedTime = (int) (time - mStartTime); 713637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = elapsedTime + extend; 714637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 715637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 716637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 717637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean springback(int start, int min, int max) { 718637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 719637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 720637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal = start; 721637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 722637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 723637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 724637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = 0; 725637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 726637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start < min) { 727637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, min, 0); 728637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else if (start > max) { 729637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, max, 0); 730637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 731637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 732637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !mFinished; 733637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 734637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 735637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startSpringback(int start, int end, int velocity) { 736c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mStartTime has been set 737637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 738c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = CUBIC; 739c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mStart = start; 740c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = end; 741c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final int delta = start - end; 742c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = getDeceleration(delta); 743c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO take velocity into account 744c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mVelocity = -delta; // only sign is used 745c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = Math.abs(delta); 746c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration)); 747637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 748637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 749637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void fling(int start, int velocity, int min, int max, int over) { 750637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mOver = over; 751637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 752637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity = velocity; 753637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = mSplineDuration = 0; 754637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 755fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mCurrentPosition = mStart = start; 756637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 757637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > max || start < min) { 758637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startAfterEdge(start, min, max, velocity); 759637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 760637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 761637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 762c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = SPLINE; 763637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double totalDistance = 0.0; 764637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 765637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (velocity != 0) { 766fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mDuration = mSplineDuration = getSplineFlingDuration(velocity); 767fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne totalDistance = getSplineFlingDistance(velocity); 768637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 769637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 770637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mSplineDistance = (int) (totalDistance * Math.signum(velocity)); 771637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + mSplineDistance; 772637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 773637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Clamp to a valid final position 774637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal < min) { 775637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, min); 776637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = min; 777637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 778637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 779637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal > max) { 780637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, max); 781637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = max; 782637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 783637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 784637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 785fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineDeceleration(int velocity) { 786fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * PHYSICAL_COEF)); 787fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 788fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 789fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineFlingDistance(int velocity) { 790fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 791fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 792fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return mFlingFriction * PHYSICAL_COEF * Math.exp(DECELERATION_RATE / decelMinusOne * l); 793fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 794fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 795fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /* Returns the duration, expressed in milliseconds */ 796fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int getSplineFlingDuration(int velocity) { 797fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 798fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 799fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return (int) (1000.0 * Math.exp(l / decelMinusOne)); 800fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 801fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 802637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void fitOnBounceCurve(int start, int end, int velocity) { 803637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Simulate a bounce that started from edge 804637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float durationToApex = - velocity / mDeceleration; 805637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToApex = velocity * velocity / 2.0f / Math.abs(mDeceleration); 806637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToEdge = Math.abs(end - start); 807637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float totalDuration = (float) Math.sqrt( 808637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration)); 809637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime -= (int) (1000.0f * (totalDuration - durationToApex)); 810637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = end; 811637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) (- mDeceleration * totalDuration); 812637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 813637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 814637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startBounceAfterEdge(int start, int end, int velocity) { 815637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity); 816637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fitOnBounceCurve(start, end, velocity); 817637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 818637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 819637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 820637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startAfterEdge(int start, int min, int max, int velocity) { 821637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > min && start < max) { 822fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne Log.e("OverScroller", "startAfterEdge called from a valid position"); 823637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 824637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 825637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 826637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean positive = start > max; 827637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int edge = positive ? max : min; 828637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int overDistance = start - edge; 829637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean keepIncreasing = overDistance * velocity >= 0; 830637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (keepIncreasing) { 831637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Will result in a bounce or a to_boundary depending on velocity. 832637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startBounceAfterEdge(start, edge, velocity); 833637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 834fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double totalDistance = getSplineFlingDistance(velocity); 835637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (totalDistance > Math.abs(overDistance)) { 836637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(start, velocity, positive ? min : start, positive ? start : max, mOver); 837637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 838637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, edge, velocity); 839637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 840637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 841637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 842637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 843637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void notifyEdgeReached(int start, int end, int over) { 844c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mState is used to detect successive notifications 845c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (mState == SPLINE) { 846fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mOver = over; 847fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mStartTime = AnimationUtils.currentAnimationTimeMillis(); 848fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // We were in fling/scroll mode before: current velocity is such that distance to 849c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // edge is increasing. This ensures that startAfterEdge will not start a new fling. 850fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne startAfterEdge(start, end, end, (int) mCurrVelocity); 851fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 852637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 853637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 854637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void onEdgeReached() { 855637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached. 856c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne float distance = mVelocity * mVelocity / (2.0f * Math.abs(mDeceleration)); 857c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 858637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 859c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (distance > mOver) { 860c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // Default deceleration is not sufficient to slow us down before boundary 861c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = - sign * mVelocity * mVelocity / (2.0f * mOver); 862c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = mOver; 863637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 864c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 865c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = (int) distance; 866c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = BALLISTIC; 867c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance); 868c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = - (int) (1000.0f * mVelocity / mDeceleration); 869637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 870637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 871637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean continueWhenFinished() { 872637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 873c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: 874637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration from start to null velocity 875637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mDuration < mSplineDuration) { 876637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // If the animation was clamped, we reached the edge 877637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal; 878c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO Better compute speed when edge was reached 879637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) mCurrVelocity; 880637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(mVelocity); 881637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 882637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 883637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 884637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Normal stop, no need to continue 885637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 886637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 887637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 888c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: 889637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 890c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne startSpringback(mFinal, mStart, 0); 891637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 892c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: 893c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne return false; 894637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 895637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 896637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell update(); 897637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 898637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 899637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 900637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 901637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Update the current position and velocity for current time. Returns 902637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * true if update has been done and false if animation duration has been 903637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * reached. 904637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 905637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean update() { 906637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 907637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long currentTime = time - mStartTime; 908637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 909637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (currentTime > mDuration) { 910637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 911637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 912637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 913637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double distance = 0.0; 914637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 915c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: { 916637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = (float) currentTime / mSplineDuration; 917637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * t); 918637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float distanceCoef = 1.f; 919637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float velocityCoef = 0.f; 920637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 921637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = (float) index / NB_SAMPLES; 922637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = (float) (index + 1) / NB_SAMPLES; 923637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_inf = SPLINE_POSITION[index]; 924637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_sup = SPLINE_POSITION[index + 1]; 925637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); 926637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distanceCoef = d_inf + (t - t_inf) * velocityCoef; 927637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 928637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 929637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = distanceCoef * mSplineDistance; 930c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = velocityCoef * mSplineDistance / mSplineDuration * 1000.0f; 931637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 932637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 933637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 934c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: { 935637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = currentTime / 1000.0f; 936637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity + mDeceleration * t; 937637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = mVelocity * t + mDeceleration * t * t / 2.0f; 938637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 939637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 940637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 941c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: { 942c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t = (float) (currentTime) / mDuration; 943c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t2 = t * t; 944c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 945c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = sign * mOver * (3.0f * t2 - 2.0f * t * t2); 946c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = sign * mOver * 6.0f * (- t + t2); 947637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 948637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 949637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 950637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 951637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + (int) Math.round(distance); 952637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 953637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 954637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 955637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell} 956