OverScroller.java revision 0b8acd81d5029385ec0ae3cafd90acf3036b89ac
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 380b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell private 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. 70086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * @hide 71c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne */ 72c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne public OverScroller(Context context, Interpolator interpolator, boolean flywheel) { 73c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mInterpolator = interpolator; 74c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFlywheel = flywheel; 75c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerX = new SplineOverScroller(); 76c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerY = new SplineOverScroller(); 77c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 78c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne SplineOverScroller.initFromContext(context); 79637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 80637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 81fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 82fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * Creates an OverScroller with flywheel enabled. 83fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context The context of this application. 84fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 85fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * be used. 86fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 87fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 88c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 89c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 90c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 91086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link #OverScroller(Context, Interpolator, boolean)} instead. 92fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 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. 109086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link OverScroller(Context, Interpolator, boolean)} instead. 110637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 111637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public OverScroller(Context context, Interpolator interpolator, 112637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) { 113c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, flywheel); 114637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 115637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 1160b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell void setInterpolator(Interpolator interpolator) { 1170b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell mInterpolator = interpolator; 1180b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell } 1190b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell 120637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 121637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The amount of friction applied to flings. The default value 122637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * is {@link ViewConfiguration#getScrollFriction}. 123637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 124637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param friction A scalar dimension-less value representing the coefficient of 125637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * friction. 126637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 127637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void setFriction(float friction) { 128fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerX.setFriction(friction); 129fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerY.setFriction(friction); 130637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 131637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 132637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 133637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 134637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the scroller has finished scrolling. 135637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 136637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return True if the scroller has finished scrolling, false otherwise. 137637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 138637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final boolean isFinished() { 139637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinished && mScrollerY.mFinished; 140637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 141637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 142637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 143637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Force the finished field to a particular value. Contrary to 144637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #abortAnimation()}, forcing the animation to finished 145637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * does NOT cause the scroller to move to the final x and y 146637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * position. 147637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 148637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finished The new finished value. 149637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 150637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void forceFinished(boolean finished) { 151637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.mFinished = mScrollerY.mFinished = finished; 152637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 153637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 154637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 155637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current X offset in the scroll. 156637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 157637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new X offset as an absolute distance from the origin. 158637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 159637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrX() { 160637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mCurrentPosition; 161637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 162637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 163637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 164637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current Y offset in the scroll. 165637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 166637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new Y offset as an absolute distance from the origin. 167637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 168637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrY() { 169637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mCurrentPosition; 170637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 171637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 172637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 173c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Returns the absolute value of the current velocity. 174637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 175637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The original velocity less the deceleration, norm of the X and Y velocity vector. 176637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 177637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public float getCurrVelocity() { 178637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity; 179637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity; 180fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return FloatMath.sqrt(squaredNorm); 181637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 182637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 183637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 184637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start X offset in the scroll. 185637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 186637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start X offset as an absolute distance from the origin. 187637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 188637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartX() { 189637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mStart; 190637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 191637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 192637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 193637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start Y offset in the scroll. 194637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 195637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start Y offset as an absolute distance from the origin. 196637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 197637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartY() { 198637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mStart; 199637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 200637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 201637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 202637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 203637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 204637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final X offset as an absolute distance from the origin. 205637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 206637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalX() { 207637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinal; 208637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 209637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 210637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 211637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 212637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 213637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final Y offset as an absolute distance from the origin. 214637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 215637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalY() { 216637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mFinal; 217637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 218637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 219637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 220637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns how long the scroll event will take, in milliseconds. 221637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 222637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The duration of the scroll in milliseconds. 223637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 224637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 225637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 226637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * This function will lie to the best of its ability. 227637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 228637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 229637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getDuration() { 230637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return Math.max(mScrollerX.mDuration, mScrollerY.mDuration); 231637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 232637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 233637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 234637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Extend the scroll animation. This allows a running animation to scroll 235637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}. 236637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 237637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param extend Additional time to scroll in milliseconds. 238637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 239637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 240637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 241637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 242637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 243637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 244637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 245637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 246637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 247637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 248637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void extendDuration(int extend) { 249637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.extendDuration(extend); 250637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.extendDuration(extend); 251637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 252637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 253637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 254637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (X) for this scroller. 255637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 256637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newX The new X offset as an absolute distance from the origin. 257637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 258637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 259637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 260637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 261637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 262637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 263637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 264637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 265637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 266637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 267637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalX(int newX) { 268637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.setFinalPosition(newX); 269637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 270637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 271637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 272637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (Y) for this scroller. 273637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 274637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newY The new Y offset as an absolute distance from the origin. 275637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 276637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 277637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 278637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 279637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 280637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 281637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 282637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 283637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 284637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 285637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalY(int newY) { 286637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.setFinalPosition(newY); 287637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 288637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 289637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 290637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to know the new location. If it returns true, the 291637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animation is not yet finished. 292637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 293637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean computeScrollOffset() { 294637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (isFinished()) { 295637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 296637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 297637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 298637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mMode) { 299637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case SCROLL_MODE: 300637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell long time = AnimationUtils.currentAnimationTimeMillis(); 301637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Any scroller can be used for time, since they were started 302637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // together in scroll mode. We use X here. 303637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long elapsedTime = time - mScrollerX.mStartTime; 304637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 305637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int duration = mScrollerX.mDuration; 306637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (elapsedTime < duration) { 307637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float q = (float) (elapsedTime) / duration; 308637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 309fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (mInterpolator == null) { 310fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne q = Scroller.viscousFluid(q); 311fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } else { 312fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne q = mInterpolator.getInterpolation(q); 313fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 314637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 315637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.updateScroll(q); 316637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.updateScroll(q); 317637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 318637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell abortAnimation(); 319637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 320637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 321637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 322637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case FLING_MODE: 323637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.mFinished) { 324637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.update()) { 325637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.continueWhenFinished()) { 326637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 327637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 328637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 329637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 330637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 331637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.mFinished) { 332637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.update()) { 333637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.continueWhenFinished()) { 334637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 335637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 336637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 337637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 338637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 339637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 340637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 341637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 342637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 343637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 344637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 345637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 346637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 347637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The scroll will use the default value of 250 milliseconds for the 348637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * duration. 349637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 350637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 351637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 352637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 353637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 354637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 355637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 356637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 357637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 358637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 359637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy) { 360637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startScroll(startX, startY, dx, dy, DEFAULT_DURATION); 361637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 362637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 363637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 364637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 365637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 366637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 367637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 368637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 369637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 370637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 371637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 372637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 373637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 374637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param duration Duration of the scroll in milliseconds. 375637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 376637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy, int duration) { 377637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = SCROLL_MODE; 378637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.startScroll(startX, dx, duration); 379637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.startScroll(startY, dy, duration); 380637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 381637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 382637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 383637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to 'spring back' into a valid coordinate range. 384637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 385637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting X coordinate 386637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting Y coordinate 387637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum valid X value 388637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum valid X value 389637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum valid Y value 390637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Minimum valid Y value 391637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true if a springback was initiated, false if startX and startY were 392637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * already within the valid range. 393637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 394637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean springBack(int startX, int startY, int minX, int maxX, int minY, int maxY) { 395637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 396637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 397637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Make sure both methods are called. 398637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackX = mScrollerX.springback(startX, minX, maxX); 399637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackY = mScrollerY.springback(startY, minY, maxY); 400637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return spingbackX || spingbackY; 401637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 402637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 403637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 404637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY) { 405637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0); 406637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 407637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 408637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 409637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling based on a fling gesture. The distance traveled will 410637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * depend on the initial velocity of the fling. 411637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 412637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting point of the scroll (X) 413637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting point of the scroll (Y) 414637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityX Initial velocity of the fling (X) measured in pixels per 415637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second. 416637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityY Initial velocity of the fling (Y) measured in pixels per 417637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second 418637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum X value. The scroller will not scroll past this point 419637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use minX as 420637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 421637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum X value. The scroller will not scroll past this point 422637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use maxX as 423637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 424637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum Y value. The scroller will not scroll past this point 425637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use minY as 426637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 427637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Maximum Y value. The scroller will not scroll past this point 428637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use maxY as 429637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 430637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Overfling range. If > 0, horizontal overfling in either 431637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 432637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Overfling range. If > 0, vertical overfling in either 433637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 434637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 435637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 436637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY, int overX, int overY) { 437637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Continue a scroll or fling in progress 438637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFlywheel && !isFinished()) { 439637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityX = mScrollerX.mCurrVelocity; 440637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityY = mScrollerY.mCurrVelocity; 441637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (Math.signum(velocityX) == Math.signum(oldVelocityX) && 442637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell Math.signum(velocityY) == Math.signum(oldVelocityY)) { 443637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityX += oldVelocityX; 444637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityY += oldVelocityY; 445637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 446637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 447637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 448637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 449637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.fling(startX, velocityX, minX, maxX, overX); 450637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.fling(startY, velocityY, minY, maxY, overY); 451637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 452637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 453637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 454637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a horizontal boundary. 455637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 456637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 457637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 458637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will transition the current motion and 459637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animate from startX to finalX as appropriate. 460637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 461637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting/current X position 462637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalX Desired final X position 463637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Magnitude of overscroll allowed. This should be the maximum 464637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalX. Absolute value - must be positive. 465637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 466637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyHorizontalEdgeReached(int startX, int finalX, int overX) { 467637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.notifyEdgeReached(startX, finalX, overX); 468637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 469637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 470637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 471637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a vertical boundary. 472637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 473637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 474637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 475637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will animate a parabolic motion from 476637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * startY to finalY. 477637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 478637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting/current Y position 479637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalY Desired final Y position 480637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Magnitude of overscroll allowed. This should be the maximum 481637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalY. Absolute value - must be positive. 482637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 483637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyVerticalEdgeReached(int startY, int finalY, int overY) { 484637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.notifyEdgeReached(startY, finalY, overY); 485637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 486637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 487637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 488637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the current Scroller is currently returning to a valid position. 489637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Valid bounds were provided by the 490637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method. 491637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 492637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * One should check this value before calling 493637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #startScroll(int, int, int, int)} as the interpolation currently in progress 494637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to restore a valid position will then be stopped. The caller has to take into account 495637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the fact that the started scroll will start from an overscrolled position. 496637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 497637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true when the current position is overscrolled and in the process of 498637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * interpolating back to a valid value. 499637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 500637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isOverScrolled() { 501637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return ((!mScrollerX.mFinished && 502c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerX.mState != SplineOverScroller.SPLINE) || 503637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell (!mScrollerY.mFinished && 504c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerY.mState != SplineOverScroller.SPLINE)); 505637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 506637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 507637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 508637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Stops the animation. Contrary to {@link #forceFinished(boolean)}, 509637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * aborting the animating causes the scroller to move to the final x and y 510637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * positions. 511637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 512637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #forceFinished(boolean) 513637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 514637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void abortAnimation() { 515637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 516637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 517637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 518637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 519637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 520637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the time elapsed since the beginning of the scrolling. 521637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 522637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The elapsed time in milliseconds. 523637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 524637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 525637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 526637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public int timePassed() { 527637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 528637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime); 529637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return (int) (time - startTime); 530637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 531637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 532637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 533637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 534637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 535637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isScrollingInDirection(float xvel, float yvel) { 536637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dx = mScrollerX.mFinal - mScrollerX.mStart; 537637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dy = mScrollerY.mFinal - mScrollerY.mStart; 538637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !isFinished() && Math.signum(xvel) == Math.signum(dx) && 539b0c71eb9f50ce06327fa6bb6219f0970e04fd856Jeff Brown Math.signum(yvel) == Math.signum(dy); 540637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 541637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 542fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static class SplineOverScroller { 543637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial position 544fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mStart; 545637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 546637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current position 547fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mCurrentPosition; 548637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 549637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Final position 550fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mFinal; 551637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 552637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial velocity 553fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mVelocity; 554637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 555637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current velocity 556fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mCurrVelocity; 557637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 558637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Constant current deceleration 559fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mDeceleration; 560637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 561637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation starting time, in system milliseconds 562fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private long mStartTime; 563637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 564637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation duration, in milliseconds 565fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mDuration; 566637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 567637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration to complete spline component of animation 568fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDuration; 569637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 570637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Distance to travel along spline animation 571fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDistance; 572637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 573637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Whether the animation is currently in progress 574fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private boolean mFinished; 575637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 576fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // The allowed overshot distance before boundary is reached. 577fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mOver; 578fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 579fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Fling friction 580fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mFlingFriction = ViewConfiguration.getScrollFriction(); 581fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 582fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Current state of the animation. 583c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private int mState = SPLINE; 584637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 585fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Constant gravity value, used in the deceleration phase. 586fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float GRAVITY = 2000.0f; 587fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 588fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // A device specific coefficient adjusted to physical values. 589fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static float PHYSICAL_COEF; 590fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 591a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); 592a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1) 593a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float START_TENSION = 0.5f; 594a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float END_TENSION = 1.0f; 595fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P1 = START_TENSION * INFLEXION; 596fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); 597fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 598fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final int NB_SAMPLES = 100; 599fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; 600fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; 601fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 602c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int SPLINE = 0; 603c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int CUBIC = 1; 604c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int BALLISTIC = 2; 605637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 606fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static { 607fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_min = 0.0f; 608fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_min = 0.0f; 609fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne for (int i = 0; i < NB_SAMPLES; i++) { 610fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float alpha = (float) i / NB_SAMPLES; 611fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 612fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_max = 1.0f; 613fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x, tx, coef; 614fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 615fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne x = x_min + (x_max - x_min) / 2.0f; 616fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * x * (1.0f - x); 617fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; 618fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(tx - alpha) < 1E-5) break; 619fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (tx > alpha) x_max = x; 620fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else x_min = x; 621fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 622fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; 623fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 624fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_max = 1.0f; 625fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y, dy; 626fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 627fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne y = y_min + (y_max - y_min) / 2.0f; 628fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * y * (1.0f - y); 629fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; 630fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(dy - alpha) < 1E-5) break; 631fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (dy > alpha) y_max = y; 632fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else y_min = y; 633fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 634fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; 635fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 636fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; 637fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 638637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 639fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static void initFromContext(Context context) { 640fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float ppi = context.getResources().getDisplayMetrics().density * 160.0f; 641fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne PHYSICAL_COEF = SensorManager.GRAVITY_EARTH // g (m/s^2) 642fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * 39.37f // inch/meter 643fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * ppi 644fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * 0.84f; // look and feel tuning 645fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 646fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 647fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne void setFriction(float friction) { 648fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mFlingFriction = friction; 649fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 650fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 651fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SplineOverScroller() { 652637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 653637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 654637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 655637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void updateScroll(float q) { 656637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + Math.round(q * (mFinal - mStart)); 657637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 658637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 659637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 660637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Get a signed deceleration that will reduce the velocity. 661637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 662fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static private float getDeceleration(int velocity) { 663fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return velocity > 0 ? -GRAVITY : GRAVITY; 664637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 665637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 666637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 667637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Modifies mDuration to the duration it takes to get from start to newFinal using the 668637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * spline interpolation. The previous duration was needed to get to oldFinal. 669637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 670fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private void adjustDuration(int start, int oldFinal, int newFinal) { 671637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int oldDistance = oldFinal - start; 672637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int newDistance = newFinal - start; 673fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float x = Math.abs((float) newDistance / oldDistance); 674637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * x); 675637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 676637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_inf = (float) index / NB_SAMPLES; 677637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_sup = (float) (index + 1) / NB_SAMPLES; 678637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = SPLINE_TIME[index]; 679637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = SPLINE_TIME[index + 1]; 680637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf); 681637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration *= timeCoef; 682637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 683637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 684637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 685637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void startScroll(int start, int distance, int duration) { 686637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 687637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 688637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = start; 689637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + distance; 690637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 691637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 692637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = duration; 693637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 694637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Unused 695637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = 0.0f; 696637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 697637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 698637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 699637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void finish() { 700637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mFinal; 701637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Not reset since WebView relies on this value for fast fling. 702637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // TODO: restore when WebView uses the fast fling implemented in this class. 703637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mCurrVelocity = 0.0f; 704637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 705637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 706637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 707637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void setFinalPosition(int position) { 708637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = position; 709637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 710637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 711637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 712637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void extendDuration(int extend) { 713637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 714637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int elapsedTime = (int) (time - mStartTime); 715637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = elapsedTime + extend; 716637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 717637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 718637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 719637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean springback(int start, int min, int max) { 720637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 721637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 722637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal = start; 723637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 724637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 725637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 726637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = 0; 727637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 728637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start < min) { 729637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, min, 0); 730637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else if (start > max) { 731637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, max, 0); 732637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 733637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 734637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !mFinished; 735637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 736637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 737637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startSpringback(int start, int end, int velocity) { 738c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mStartTime has been set 739637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 740c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = CUBIC; 741c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mStart = start; 742c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = end; 743c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final int delta = start - end; 744c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = getDeceleration(delta); 745c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO take velocity into account 746c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mVelocity = -delta; // only sign is used 747c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = Math.abs(delta); 748c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration)); 749637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 750637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 751637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void fling(int start, int velocity, int min, int max, int over) { 752637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mOver = over; 753637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 754637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity = velocity; 755637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = mSplineDuration = 0; 756637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 757fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mCurrentPosition = mStart = start; 758637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 759637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > max || start < min) { 760637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startAfterEdge(start, min, max, velocity); 761637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 762637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 763637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 764c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = SPLINE; 765637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double totalDistance = 0.0; 766637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 767637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (velocity != 0) { 768fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mDuration = mSplineDuration = getSplineFlingDuration(velocity); 769fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne totalDistance = getSplineFlingDistance(velocity); 770637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 771637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 772637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mSplineDistance = (int) (totalDistance * Math.signum(velocity)); 773637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + mSplineDistance; 774637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 775637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Clamp to a valid final position 776637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal < min) { 777637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, min); 778637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = min; 779637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 780637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 781637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal > max) { 782637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, max); 783637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = max; 784637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 785637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 786637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 787fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineDeceleration(int velocity) { 788fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * PHYSICAL_COEF)); 789fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 790fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 791fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineFlingDistance(int velocity) { 792fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 793fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 794fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return mFlingFriction * PHYSICAL_COEF * Math.exp(DECELERATION_RATE / decelMinusOne * l); 795fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 796fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 797fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /* Returns the duration, expressed in milliseconds */ 798fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int getSplineFlingDuration(int velocity) { 799fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 800fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 801fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return (int) (1000.0 * Math.exp(l / decelMinusOne)); 802fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 803fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 804637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void fitOnBounceCurve(int start, int end, int velocity) { 805637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Simulate a bounce that started from edge 806637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float durationToApex = - velocity / mDeceleration; 807637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToApex = velocity * velocity / 2.0f / Math.abs(mDeceleration); 808637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToEdge = Math.abs(end - start); 809637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float totalDuration = (float) Math.sqrt( 810637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration)); 811637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime -= (int) (1000.0f * (totalDuration - durationToApex)); 812637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = end; 813637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) (- mDeceleration * totalDuration); 814637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 815637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 816637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startBounceAfterEdge(int start, int end, int velocity) { 817637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity); 818637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fitOnBounceCurve(start, end, velocity); 819637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 820637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 821637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 822637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startAfterEdge(int start, int min, int max, int velocity) { 823637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > min && start < max) { 824fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne Log.e("OverScroller", "startAfterEdge called from a valid position"); 825637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 826637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 827637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 828637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean positive = start > max; 829637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int edge = positive ? max : min; 830637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int overDistance = start - edge; 831637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean keepIncreasing = overDistance * velocity >= 0; 832637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (keepIncreasing) { 833637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Will result in a bounce or a to_boundary depending on velocity. 834637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startBounceAfterEdge(start, edge, velocity); 835637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 836fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double totalDistance = getSplineFlingDistance(velocity); 837637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (totalDistance > Math.abs(overDistance)) { 838637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(start, velocity, positive ? min : start, positive ? start : max, mOver); 839637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 840637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, edge, velocity); 841637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 842637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 843637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 844637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 845637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void notifyEdgeReached(int start, int end, int over) { 846c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mState is used to detect successive notifications 847c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (mState == SPLINE) { 848fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mOver = over; 849fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mStartTime = AnimationUtils.currentAnimationTimeMillis(); 850fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // We were in fling/scroll mode before: current velocity is such that distance to 851c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // edge is increasing. This ensures that startAfterEdge will not start a new fling. 852fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne startAfterEdge(start, end, end, (int) mCurrVelocity); 853fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 854637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 855637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 856637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void onEdgeReached() { 857637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached. 858c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne float distance = mVelocity * mVelocity / (2.0f * Math.abs(mDeceleration)); 859c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 860637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 861c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (distance > mOver) { 862c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // Default deceleration is not sufficient to slow us down before boundary 863c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = - sign * mVelocity * mVelocity / (2.0f * mOver); 864c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = mOver; 865637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 866c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 867c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = (int) distance; 868c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = BALLISTIC; 869c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance); 870c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = - (int) (1000.0f * mVelocity / mDeceleration); 871637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 872637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 873637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean continueWhenFinished() { 874637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 875c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: 876637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration from start to null velocity 877637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mDuration < mSplineDuration) { 878637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // If the animation was clamped, we reached the edge 879637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal; 880c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO Better compute speed when edge was reached 881637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) mCurrVelocity; 882637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(mVelocity); 883637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 884637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 885637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 886637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Normal stop, no need to continue 887637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 888637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 889637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 890c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: 891637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 892c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne startSpringback(mFinal, mStart, 0); 893637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 894c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: 895c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne return false; 896637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 897637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 898637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell update(); 899637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 900637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 901637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 902637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 903637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Update the current position and velocity for current time. Returns 904637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * true if update has been done and false if animation duration has been 905637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * reached. 906637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 907637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean update() { 908637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 909637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long currentTime = time - mStartTime; 910637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 911637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (currentTime > mDuration) { 912637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 913637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 914637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 915637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double distance = 0.0; 916637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 917c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: { 918637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = (float) currentTime / mSplineDuration; 919637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * t); 920637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float distanceCoef = 1.f; 921637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float velocityCoef = 0.f; 922637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 923637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = (float) index / NB_SAMPLES; 924637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = (float) (index + 1) / NB_SAMPLES; 925637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_inf = SPLINE_POSITION[index]; 926637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_sup = SPLINE_POSITION[index + 1]; 927637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); 928637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distanceCoef = d_inf + (t - t_inf) * velocityCoef; 929637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 930637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 931637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = distanceCoef * mSplineDistance; 932c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = velocityCoef * mSplineDistance / mSplineDuration * 1000.0f; 933637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 934637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 935637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 936c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: { 937637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = currentTime / 1000.0f; 938637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity + mDeceleration * t; 939637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = mVelocity * t + mDeceleration * t * t / 2.0f; 940637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 941637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 942637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 943c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: { 944c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t = (float) (currentTime) / mDuration; 945c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t2 = t * t; 946c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 947c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = sign * mOver * (3.0f * t2 - 2.0f * t * t2); 948c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = sign * mOver * 6.0f * (- t + t2); 949637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 950637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 951637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 952637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 953637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + (int) Math.round(distance); 95434961cc2e5cd483b15dfd1ff1eb33b01b849e0dcGilles Debunne 955637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 956637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 957637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 958637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell} 959