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; 75990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mScrollerX = new SplineOverScroller(context); 76990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mScrollerY = new SplineOverScroller(context); 77637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 78637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 79fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 80fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * Creates an OverScroller with flywheel enabled. 81fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context The context of this application. 82fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 83fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * be used. 84fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 85fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 86c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 87c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 88c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 89086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link #OverScroller(Context, Interpolator, boolean)} instead. 90fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 91fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne public OverScroller(Context context, Interpolator interpolator, 92fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float bounceCoefficientX, float bounceCoefficientY) { 93c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, true); 94637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 95637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 96637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 97637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Creates an OverScroller. 98637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param context The context of this application. 99637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 100637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * be used. 101637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 102637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 103c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 104c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 105c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 106fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param flywheel If true, successive fling motions will keep on increasing scroll speed. 107086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link OverScroller(Context, Interpolator, boolean)} instead. 108637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 109637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public OverScroller(Context context, Interpolator interpolator, 110637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) { 111c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, flywheel); 112637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 113637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 1140b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell void setInterpolator(Interpolator interpolator) { 1150b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell mInterpolator = interpolator; 1160b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell } 1170b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell 118637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 119637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The amount of friction applied to flings. The default value 120637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * is {@link ViewConfiguration#getScrollFriction}. 121637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 122637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param friction A scalar dimension-less value representing the coefficient of 123637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * friction. 124637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 125637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void setFriction(float friction) { 126fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerX.setFriction(friction); 127fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerY.setFriction(friction); 128637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 129637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 130637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 131637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 132637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the scroller has finished scrolling. 133637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 134637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return True if the scroller has finished scrolling, false otherwise. 135637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 136637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final boolean isFinished() { 137637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinished && mScrollerY.mFinished; 138637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 139637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 140637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 141637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Force the finished field to a particular value. Contrary to 142637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #abortAnimation()}, forcing the animation to finished 143637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * does NOT cause the scroller to move to the final x and y 144637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * position. 145637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 146637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finished The new finished value. 147637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 148637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void forceFinished(boolean finished) { 149637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.mFinished = mScrollerY.mFinished = finished; 150637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 151637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 152637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 153637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current X offset in the scroll. 154637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 155637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new X offset as an absolute distance from the origin. 156637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 157637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrX() { 158637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mCurrentPosition; 159637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 160637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 161637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 162637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current Y offset in the scroll. 163637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 164637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new Y offset as an absolute distance from the origin. 165637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 166637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrY() { 167637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mCurrentPosition; 168637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 169637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 170637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 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) && 537b0c71eb9f50ce06327fa6bb6219f0970e04fd856Jeff Brown 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 586990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell // A context-specific coefficient adjusted to physical values. 587990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell private float mPhysicalCoeff; 588fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 589a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); 590a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1) 591a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float START_TENSION = 0.5f; 592a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float END_TENSION = 1.0f; 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 void setFriction(float friction) { 638fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mFlingFriction = friction; 639fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 640fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 641990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell SplineOverScroller(Context context) { 642637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 643990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell final float ppi = context.getResources().getDisplayMetrics().density * 160.0f; 644990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mPhysicalCoeff = SensorManager.GRAVITY_EARTH // g (m/s^2) 645990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * 39.37f // inch/meter 646990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * ppi 647990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * 0.84f; // look and feel tuning 648637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 649637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 650637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void updateScroll(float q) { 651637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + Math.round(q * (mFinal - mStart)); 652637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 653637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 654637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 655637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Get a signed deceleration that will reduce the velocity. 656637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 657fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static private float getDeceleration(int velocity) { 658fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return velocity > 0 ? -GRAVITY : GRAVITY; 659637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 660637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 661637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 662637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Modifies mDuration to the duration it takes to get from start to newFinal using the 663637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * spline interpolation. The previous duration was needed to get to oldFinal. 664637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 665fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private void adjustDuration(int start, int oldFinal, int newFinal) { 666637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int oldDistance = oldFinal - start; 667637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int newDistance = newFinal - start; 668fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float x = Math.abs((float) newDistance / oldDistance); 669637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * x); 670637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 671637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_inf = (float) index / NB_SAMPLES; 672637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_sup = (float) (index + 1) / NB_SAMPLES; 673637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = SPLINE_TIME[index]; 674637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = SPLINE_TIME[index + 1]; 675637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf); 676637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration *= timeCoef; 677637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 678637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 679637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 680637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void startScroll(int start, int distance, int duration) { 681637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 682637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 683637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = start; 684637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + distance; 685637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 686637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 687637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = duration; 688637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 689637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Unused 690637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = 0.0f; 691637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 692637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 693637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 694637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void finish() { 695637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mFinal; 696637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Not reset since WebView relies on this value for fast fling. 697637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // TODO: restore when WebView uses the fast fling implemented in this class. 698637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mCurrVelocity = 0.0f; 699637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 700637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 701637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 702637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void setFinalPosition(int position) { 703637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = position; 704637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 705637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 706637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 707637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void extendDuration(int extend) { 708637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 709637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int elapsedTime = (int) (time - mStartTime); 710637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = elapsedTime + extend; 711637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 712637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 713637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 714637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean springback(int start, int min, int max) { 715637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 716637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 717637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal = start; 718637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 719637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 720637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 721637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = 0; 722637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 723637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start < min) { 724637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, min, 0); 725637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else if (start > max) { 726637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, max, 0); 727637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 728637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 729637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !mFinished; 730637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 731637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 732637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startSpringback(int start, int end, int velocity) { 733c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mStartTime has been set 734637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 735c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = CUBIC; 736c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mStart = start; 737c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = end; 738c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final int delta = start - end; 739c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = getDeceleration(delta); 740c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO take velocity into account 741c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mVelocity = -delta; // only sign is used 742c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = Math.abs(delta); 743c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration)); 744637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 745637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 746637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void fling(int start, int velocity, int min, int max, int over) { 747637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mOver = over; 748637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 749637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity = velocity; 750637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = mSplineDuration = 0; 751637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 752fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mCurrentPosition = mStart = start; 753637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 754637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > max || start < min) { 755637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startAfterEdge(start, min, max, velocity); 756637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 757637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 758637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 759c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = SPLINE; 760637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double totalDistance = 0.0; 761637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 762637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (velocity != 0) { 763fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mDuration = mSplineDuration = getSplineFlingDuration(velocity); 764fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne totalDistance = getSplineFlingDistance(velocity); 765637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 766637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 767637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mSplineDistance = (int) (totalDistance * Math.signum(velocity)); 768637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + mSplineDistance; 769637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 770637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Clamp to a valid final position 771637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal < min) { 772637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, min); 773637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = min; 774637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 775637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 776637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal > max) { 777637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, max); 778637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = max; 779637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 780637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 781637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 782fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineDeceleration(int velocity) { 783990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff)); 784fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 785fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 786fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineFlingDistance(int velocity) { 787fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 788fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 789990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l); 790fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 791fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 792fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /* Returns the duration, expressed in milliseconds */ 793fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int getSplineFlingDuration(int velocity) { 794fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 795fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 796fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return (int) (1000.0 * Math.exp(l / decelMinusOne)); 797fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 798fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 799637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void fitOnBounceCurve(int start, int end, int velocity) { 800637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Simulate a bounce that started from edge 801637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float durationToApex = - velocity / mDeceleration; 802637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToApex = velocity * velocity / 2.0f / Math.abs(mDeceleration); 803637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToEdge = Math.abs(end - start); 804637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float totalDuration = (float) Math.sqrt( 805637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration)); 806637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime -= (int) (1000.0f * (totalDuration - durationToApex)); 807637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = end; 808637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) (- mDeceleration * totalDuration); 809637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 810637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 811637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startBounceAfterEdge(int start, int end, int velocity) { 812637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity); 813637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fitOnBounceCurve(start, end, velocity); 814637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 815637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 816637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 817637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startAfterEdge(int start, int min, int max, int velocity) { 818637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > min && start < max) { 819fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne Log.e("OverScroller", "startAfterEdge called from a valid position"); 820637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 821637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 822637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 823637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean positive = start > max; 824637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int edge = positive ? max : min; 825637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int overDistance = start - edge; 826637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean keepIncreasing = overDistance * velocity >= 0; 827637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (keepIncreasing) { 828637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Will result in a bounce or a to_boundary depending on velocity. 829637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startBounceAfterEdge(start, edge, velocity); 830637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 831fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double totalDistance = getSplineFlingDistance(velocity); 832637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (totalDistance > Math.abs(overDistance)) { 833637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(start, velocity, positive ? min : start, positive ? start : max, mOver); 834637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 835637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, edge, velocity); 836637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 837637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 838637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 839637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 840637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void notifyEdgeReached(int start, int end, int over) { 841c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mState is used to detect successive notifications 842c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (mState == SPLINE) { 843fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mOver = over; 844fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mStartTime = AnimationUtils.currentAnimationTimeMillis(); 845fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // We were in fling/scroll mode before: current velocity is such that distance to 846c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // edge is increasing. This ensures that startAfterEdge will not start a new fling. 847fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne startAfterEdge(start, end, end, (int) mCurrVelocity); 848fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 849637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 850637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 851637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void onEdgeReached() { 852637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached. 853c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne float distance = mVelocity * mVelocity / (2.0f * Math.abs(mDeceleration)); 854c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 855637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 856c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (distance > mOver) { 857c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // Default deceleration is not sufficient to slow us down before boundary 858c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = - sign * mVelocity * mVelocity / (2.0f * mOver); 859c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = mOver; 860637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 861c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 862c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = (int) distance; 863c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = BALLISTIC; 864c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance); 865c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = - (int) (1000.0f * mVelocity / mDeceleration); 866637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 867637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 868637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean continueWhenFinished() { 869637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 870c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: 871637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration from start to null velocity 872637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mDuration < mSplineDuration) { 873637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // If the animation was clamped, we reached the edge 874637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal; 875c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO Better compute speed when edge was reached 876637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) mCurrVelocity; 877637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(mVelocity); 878637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 879637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 880637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 881637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Normal stop, no need to continue 882637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 883637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 884637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 885c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: 886637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 887c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne startSpringback(mFinal, mStart, 0); 888637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 889c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: 890c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne return false; 891637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 892637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 893637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell update(); 894637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 895637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 896637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 897637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 898637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Update the current position and velocity for current time. Returns 899637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * true if update has been done and false if animation duration has been 900637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * reached. 901637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 902637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean update() { 903637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 904637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long currentTime = time - mStartTime; 905637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 906637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (currentTime > mDuration) { 907637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 908637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 909637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 910637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double distance = 0.0; 911637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 912c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: { 913637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = (float) currentTime / mSplineDuration; 914637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * t); 915637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float distanceCoef = 1.f; 916637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float velocityCoef = 0.f; 917637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 918637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = (float) index / NB_SAMPLES; 919637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = (float) (index + 1) / NB_SAMPLES; 920637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_inf = SPLINE_POSITION[index]; 921637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float d_sup = SPLINE_POSITION[index + 1]; 922637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); 923637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distanceCoef = d_inf + (t - t_inf) * velocityCoef; 924637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 925637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 926637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = distanceCoef * mSplineDistance; 927c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = velocityCoef * mSplineDistance / mSplineDuration * 1000.0f; 928637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 929637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 930637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 931c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: { 932637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t = currentTime / 1000.0f; 933637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity + mDeceleration * t; 934637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell distance = mVelocity * t + mDeceleration * t * t / 2.0f; 935637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 936637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 937637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 938c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: { 939c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t = (float) (currentTime) / mDuration; 940c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float t2 = t * t; 941c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 942c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = sign * mOver * (3.0f * t2 - 2.0f * t * t2); 943c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mCurrVelocity = sign * mOver * 6.0f * (- t + t2); 944637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 945637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 946637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 947637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 948637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + (int) Math.round(distance); 94934961cc2e5cd483b15dfd1ff1eb33b01b849e0dcGilles Debunne 950637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 951637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 952637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 953637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell} 954