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) { 7379a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette if (interpolator == null) { 7479a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette mInterpolator = new Scroller.ViscousFluidInterpolator(); 7579a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette } else { 7679a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette mInterpolator = interpolator; 7779a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette } 78c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFlywheel = flywheel; 79990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mScrollerX = new SplineOverScroller(context); 80990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mScrollerY = new SplineOverScroller(context); 81637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 82637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 83fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /** 84fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * Creates an OverScroller with flywheel enabled. 85fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param context The context of this application. 86fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 87fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * be used. 88fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 89fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 90c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 91c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 92c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 93086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link #OverScroller(Context, Interpolator, boolean)} instead. 94fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne */ 95fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne public OverScroller(Context context, Interpolator interpolator, 96fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float bounceCoefficientX, float bounceCoefficientY) { 97c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, true); 98637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 99637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 100637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 101637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Creates an OverScroller. 102637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param context The context of this application. 103637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will 104637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * be used. 105637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the 106637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * velocity which is preserved in the bounce when the horizontal edge is reached. A null value 107c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * means no bounce. This behavior is no longer supported and this coefficient has no effect. 108c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This 109c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * behavior is no longer supported and this coefficient has no effect. 110fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne * @param flywheel If true, successive fling motions will keep on increasing scroll speed. 111086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville * !deprecated Use {!link OverScroller(Context, Interpolator, boolean)} instead. 112637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 113637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public OverScroller(Context context, Interpolator interpolator, 114637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) { 115c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne this(context, interpolator, flywheel); 116637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 117637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 1180b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell void setInterpolator(Interpolator interpolator) { 11979a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette if (interpolator == null) { 12079a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette mInterpolator = new Scroller.ViscousFluidInterpolator(); 12179a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette } else { 12279a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette mInterpolator = interpolator; 12379a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette } 1240b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell } 1250b8acd81d5029385ec0ae3cafd90acf3036b89acAdam Powell 126637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 127637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The amount of friction applied to flings. The default value 128637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * is {@link ViewConfiguration#getScrollFriction}. 129637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 130637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param friction A scalar dimension-less value representing the coefficient of 131637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * friction. 132637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 133637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void setFriction(float friction) { 134fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerX.setFriction(friction); 135fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mScrollerY.setFriction(friction); 136637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 137637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 138637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 139637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 140637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the scroller has finished scrolling. 141637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 142637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return True if the scroller has finished scrolling, false otherwise. 143637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 144637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final boolean isFinished() { 145637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinished && mScrollerY.mFinished; 146637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 147637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 148637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 149637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Force the finished field to a particular value. Contrary to 150637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #abortAnimation()}, forcing the animation to finished 151637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * does NOT cause the scroller to move to the final x and y 152637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * position. 153637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 154637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finished The new finished value. 155637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 156637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final void forceFinished(boolean finished) { 157637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.mFinished = mScrollerY.mFinished = finished; 158637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 159637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 160637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 161637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current X offset in the scroll. 162637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 163637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new X offset as an absolute distance from the origin. 164637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 165637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrX() { 166637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mCurrentPosition; 167637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 168637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 169637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 170637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the current Y offset in the scroll. 171637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 172637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The new Y offset as an absolute distance from the origin. 173637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 174637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getCurrY() { 175637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mCurrentPosition; 176637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 177637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 178637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 179c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne * Returns the absolute value of the current velocity. 180637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 181637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The original velocity less the deceleration, norm of the X and Y velocity vector. 182637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 183637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public float getCurrVelocity() { 184637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity; 185637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity; 186fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return FloatMath.sqrt(squaredNorm); 187637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 188637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 189637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 190637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start X offset in the scroll. 191637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 192637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start X offset as an absolute distance from the origin. 193637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 194637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartX() { 195637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mStart; 196637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 197637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 198637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 199637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the start Y offset in the scroll. 200637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 201637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The start Y offset as an absolute distance from the origin. 202637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 203637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getStartY() { 204637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mStart; 205637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 206637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 207637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 208637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 209637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 210637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final X offset as an absolute distance from the origin. 211637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 212637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalX() { 213637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerX.mFinal; 214637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 215637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 216637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 217637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns where the scroll will end. Valid only for "fling" scrolls. 218637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 219637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The final Y offset as an absolute distance from the origin. 220637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 221637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getFinalY() { 222637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return mScrollerY.mFinal; 223637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 224637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 225637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 226637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns how long the scroll event will take, in milliseconds. 227637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 228637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The duration of the scroll in milliseconds. 229637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 230637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 231637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 232637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * This function will lie to the best of its ability. 233637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 234637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 235637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public final int getDuration() { 236637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return Math.max(mScrollerX.mDuration, mScrollerY.mDuration); 237637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 238637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 239637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 240637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Extend the scroll animation. This allows a running animation to scroll 241637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}. 242637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 243637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param extend Additional time to scroll in milliseconds. 244637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 245637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 246637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 247637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 248637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScrollers don't necessarily have a fixed duration. 249637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 250637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 251637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 252637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 253637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 254637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void extendDuration(int extend) { 255637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.extendDuration(extend); 256637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.extendDuration(extend); 257637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 258637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 259637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 260637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (X) for this scroller. 261637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 262637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newX The new X offset as an absolute distance from the origin. 263637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 264637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalY(int) 265637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 266637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 267637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 268637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 269637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 270637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 271637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 272637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 273637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalX(int newX) { 274637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.setFinalPosition(newX); 275637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 276637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 277637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 278637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Sets the final position (Y) for this scroller. 279637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 280637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param newY The new Y offset as an absolute distance from the origin. 281637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #extendDuration(int) 282637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #setFinalX(int) 283637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 284637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide Pending removal once nothing depends on it 285637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @deprecated OverScroller's final position may change during an animation. 286637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Instead of setting a new final position and extending 287637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the duration of an existing scroll, use startScroll 288637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to begin a new animation. 289637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 290637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell @Deprecated 291637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void setFinalY(int newY) { 292637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.setFinalPosition(newY); 293637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 294637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 295637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 296637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to know the new location. If it returns true, the 297637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animation is not yet finished. 298637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 299637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean computeScrollOffset() { 300637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (isFinished()) { 301637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 302637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 303637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 304637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mMode) { 305637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case SCROLL_MODE: 306637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell long time = AnimationUtils.currentAnimationTimeMillis(); 307637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Any scroller can be used for time, since they were started 308637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // together in scroll mode. We use X here. 309637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long elapsedTime = time - mScrollerX.mStartTime; 310637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 311637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int duration = mScrollerX.mDuration; 312637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (elapsedTime < duration) { 31379a5feff954b496de5c156a2f228b9cdda43cfa8Alan Viverette final float q = mInterpolator.getInterpolation(elapsedTime / (float) duration); 314637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.updateScroll(q); 315637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.updateScroll(q); 316637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 317637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell abortAnimation(); 318637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 319637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 320637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 321637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell case FLING_MODE: 322637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.mFinished) { 323637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.update()) { 324637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerX.continueWhenFinished()) { 325637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 326637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 327637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 328637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 329637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 330637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.mFinished) { 331637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.update()) { 332637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (!mScrollerY.continueWhenFinished()) { 333637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 334637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 335637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 336637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 337637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 338637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 339637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 340637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 341637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 342637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 343637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 344637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 345637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 346637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * The scroll will use the default value of 250 milliseconds for the 347637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * duration. 348637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 349637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 350637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 351637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 352637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 353637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 354637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 355637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 356637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 357637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 358637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy) { 359637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startScroll(startX, startY, dx, dy, DEFAULT_DURATION); 360637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 361637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 362637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 363637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling by providing a starting point and the distance to travel. 364637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 365637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting horizontal scroll offset in pixels. Positive 366637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * numbers will scroll the content to the left. 367637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting vertical scroll offset in pixels. Positive numbers 368637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * will scroll the content up. 369637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dx Horizontal distance to travel. Positive numbers will scroll the 370637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content to the left. 371637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param dy Vertical distance to travel. Positive numbers will scroll the 372637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * content up. 373637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param duration Duration of the scroll in milliseconds. 374637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 375637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void startScroll(int startX, int startY, int dx, int dy, int duration) { 376637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = SCROLL_MODE; 377637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.startScroll(startX, dx, duration); 378637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.startScroll(startY, dy, duration); 379637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 380637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 381637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 382637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Call this when you want to 'spring back' into a valid coordinate range. 383637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 384637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting X coordinate 385637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting Y coordinate 386637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum valid X value 387637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum valid X value 388637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum valid Y value 389637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Minimum valid Y value 390637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true if a springback was initiated, false if startX and startY were 391637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * already within the valid range. 392637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 393637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean springBack(int startX, int startY, int minX, int maxX, int minY, int maxY) { 394637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 395637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 396637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Make sure both methods are called. 397637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackX = mScrollerX.springback(startX, minX, maxX); 398637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean spingbackY = mScrollerY.springback(startY, minY, maxY); 399637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return spingbackX || spingbackY; 400637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 401637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 402637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 403637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY) { 404637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0); 405637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 406637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 407637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 408637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Start scrolling based on a fling gesture. The distance traveled will 409637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * depend on the initial velocity of the fling. 410637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 411637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting point of the scroll (X) 412637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting point of the scroll (Y) 413637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityX Initial velocity of the fling (X) measured in pixels per 414637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second. 415637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param velocityY Initial velocity of the fling (Y) measured in pixels per 416637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * second 417637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minX Minimum X value. The scroller will not scroll past this point 418637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use minX as 419637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 420637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxX Maximum X value. The scroller will not scroll past this point 421637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overX > 0. If overfling is allowed, it will use maxX as 422637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 423637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param minY Minimum Y value. The scroller will not scroll past this point 424637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use minY as 425637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 426637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param maxY Maximum Y value. The scroller will not scroll past this point 427637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * unless overY > 0. If overfling is allowed, it will use maxY as 428637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * a springback boundary. 429637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Overfling range. If > 0, horizontal overfling in either 430637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 431637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Overfling range. If > 0, vertical overfling in either 432637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * direction will be possible. 433637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 434637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void fling(int startX, int startY, int velocityX, int velocityY, 435637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell int minX, int maxX, int minY, int maxY, int overX, int overY) { 436637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Continue a scroll or fling in progress 437637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFlywheel && !isFinished()) { 438637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityX = mScrollerX.mCurrVelocity; 439637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell float oldVelocityY = mScrollerY.mCurrVelocity; 440637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (Math.signum(velocityX) == Math.signum(oldVelocityX) && 441637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell Math.signum(velocityY) == Math.signum(oldVelocityY)) { 442637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityX += oldVelocityX; 443637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell velocityY += oldVelocityY; 444637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 445637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 446637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 447637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mMode = FLING_MODE; 448637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.fling(startX, velocityX, minX, maxX, overX); 449637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.fling(startY, velocityY, minY, maxY, overY); 450637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 451637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 452637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 453637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a horizontal boundary. 454637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 455637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 456637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 457637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will transition the current motion and 458637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * animate from startX to finalX as appropriate. 459637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 460637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startX Starting/current X position 461637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalX Desired final X position 462637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overX Magnitude of overscroll allowed. This should be the maximum 463637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalX. Absolute value - must be positive. 464637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 465637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyHorizontalEdgeReached(int startX, int finalX, int overX) { 466637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.notifyEdgeReached(startX, finalX, overX); 467637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 468637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 469637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 470637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Notify the scroller that we've reached a vertical boundary. 471637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Normally the information to handle this will already be known 472637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * when the animation is started, such as in a call to one of the 473637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * fling functions. However there are cases where this cannot be known 474637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * in advance. This function will animate a parabolic motion from 475637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * startY to finalY. 476637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 477637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param startY Starting/current Y position 478637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param finalY Desired final Y position 479637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @param overY Magnitude of overscroll allowed. This should be the maximum 480637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * desired distance from finalY. Absolute value - must be positive. 481637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 482637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void notifyVerticalEdgeReached(int startY, int finalY, int overY) { 483637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.notifyEdgeReached(startY, finalY, overY); 484637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 485637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 486637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 487637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns whether the current Scroller is currently returning to a valid position. 488637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Valid bounds were provided by the 489637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method. 490637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 491637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * One should check this value before calling 492637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link #startScroll(int, int, int, int)} as the interpolation currently in progress 493637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * to restore a valid position will then be stopped. The caller has to take into account 494637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * the fact that the started scroll will start from an overscrolled position. 495637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 496637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return true when the current position is overscrolled and in the process of 497637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * interpolating back to a valid value. 498637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 499637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isOverScrolled() { 500637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return ((!mScrollerX.mFinished && 501c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerX.mState != SplineOverScroller.SPLINE) || 502637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell (!mScrollerY.mFinished && 503c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mScrollerY.mState != SplineOverScroller.SPLINE)); 504637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 505637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 506637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 507637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Stops the animation. Contrary to {@link #forceFinished(boolean)}, 508637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * aborting the animating causes the scroller to move to the final x and y 509637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * positions. 510637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 511637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @see #forceFinished(boolean) 512637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 513637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public void abortAnimation() { 514637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerX.finish(); 515637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mScrollerY.finish(); 516637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 517637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 518637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 519637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Returns the time elapsed since the beginning of the scrolling. 520637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 521637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @return The elapsed time in milliseconds. 522637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * 523637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 524637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 525637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public int timePassed() { 526637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 527637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime); 528637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return (int) (time - startTime); 529637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 530637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 531637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /** 532637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * @hide 533637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 534637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell public boolean isScrollingInDirection(float xvel, float yvel) { 535637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dx = mScrollerX.mFinal - mScrollerX.mStart; 536637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int dy = mScrollerY.mFinal - mScrollerY.mStart; 537637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !isFinished() && Math.signum(xvel) == Math.signum(dx) && 538b0c71eb9f50ce06327fa6bb6219f0970e04fd856Jeff Brown Math.signum(yvel) == Math.signum(dy); 539637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 540637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 541fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static class SplineOverScroller { 542637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial position 543fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mStart; 544637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 545637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current position 546fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mCurrentPosition; 547637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 548637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Final position 549fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mFinal; 550637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 551637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Initial velocity 552fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mVelocity; 553637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 554637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Current velocity 555fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mCurrVelocity; 556637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 557637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Constant current deceleration 558fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mDeceleration; 559637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 560637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation starting time, in system milliseconds 561fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private long mStartTime; 562637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 563637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Animation duration, in milliseconds 564fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mDuration; 565637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 566637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration to complete spline component of animation 567fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDuration; 568637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 569637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Distance to travel along spline animation 570fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mSplineDistance; 571637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 572637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Whether the animation is currently in progress 573fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private boolean mFinished; 574637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 575fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // The allowed overshot distance before boundary is reached. 576fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int mOver; 577fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 578fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Fling friction 579fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private float mFlingFriction = ViewConfiguration.getScrollFriction(); 580fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 581fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Current state of the animation. 582c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private int mState = SPLINE; 583637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 584fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // Constant gravity value, used in the deceleration phase. 585fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float GRAVITY = 2000.0f; 586fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 587990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell // A context-specific coefficient adjusted to physical values. 588990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell private float mPhysicalCoeff; 589fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 590a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); 591a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1) 592a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float START_TENSION = 0.5f; 593a181bb4b8cd5dbd3390d95fec07620c272bd350cJustin Ho private static final float END_TENSION = 1.0f; 594fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P1 = START_TENSION * INFLEXION; 595fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); 596fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 597fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final int NB_SAMPLES = 100; 598fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; 599fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; 600fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 601c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int SPLINE = 0; 602c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int CUBIC = 1; 603c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne private static final int BALLISTIC = 2; 604637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 605fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static { 606fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_min = 0.0f; 607fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_min = 0.0f; 608fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne for (int i = 0; i < NB_SAMPLES; i++) { 609fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float alpha = (float) i / NB_SAMPLES; 610fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 611fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x_max = 1.0f; 612fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float x, tx, coef; 613fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 614fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne x = x_min + (x_max - x_min) / 2.0f; 615fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * x * (1.0f - x); 616fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; 617fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(tx - alpha) < 1E-5) break; 618fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (tx > alpha) x_max = x; 619fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else x_min = x; 620fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 621fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; 622fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 623fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y_max = 1.0f; 624fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne float y, dy; 625fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne while (true) { 626fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne y = y_min + (y_max - y_min) / 2.0f; 627fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne coef = 3.0f * y * (1.0f - y); 628fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; 629fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (Math.abs(dy - alpha) < 1E-5) break; 630fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne if (dy > alpha) y_max = y; 631fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne else y_min = y; 632fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 633fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; 634fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 635fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; 636fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 637637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 638fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne void setFriction(float friction) { 639fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mFlingFriction = friction; 640fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 641fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 642990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell SplineOverScroller(Context context) { 643637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 644990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell final float ppi = context.getResources().getDisplayMetrics().density * 160.0f; 645990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell mPhysicalCoeff = SensorManager.GRAVITY_EARTH // g (m/s^2) 646990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * 39.37f // inch/meter 647990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * ppi 648990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell * 0.84f; // look and feel tuning 649637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 650637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 651637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void updateScroll(float q) { 652637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mStart + Math.round(q * (mFinal - mStart)); 653637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 654637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 655637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 656637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Get a signed deceleration that will reduce the velocity. 657637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 658fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne static private float getDeceleration(int velocity) { 659fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return velocity > 0 ? -GRAVITY : GRAVITY; 660637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 661637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 662637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 663637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Modifies mDuration to the duration it takes to get from start to newFinal using the 664637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * spline interpolation. The previous duration was needed to get to oldFinal. 665637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 666fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private void adjustDuration(int start, int oldFinal, int newFinal) { 667637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int oldDistance = oldFinal - start; 668637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int newDistance = newFinal - start; 669fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final float x = Math.abs((float) newDistance / oldDistance); 670637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int index = (int) (NB_SAMPLES * x); 671637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (index < NB_SAMPLES) { 672637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_inf = (float) index / NB_SAMPLES; 673637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float x_sup = (float) (index + 1) / NB_SAMPLES; 674637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_inf = SPLINE_TIME[index]; 675637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float t_sup = SPLINE_TIME[index + 1]; 676637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf); 677637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration *= timeCoef; 678637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 679637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 680637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 681637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void startScroll(int start, int distance, int duration) { 682637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 683637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 684637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = start; 685637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + distance; 686637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 687637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 688637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = duration; 689637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 690637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Unused 691637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = 0.0f; 692637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 693637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 694637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 695637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void finish() { 696637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrentPosition = mFinal; 697637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Not reset since WebView relies on this value for fast fling. 698637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // TODO: restore when WebView uses the fast fling implemented in this class. 699637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mCurrVelocity = 0.0f; 700637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 701637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 702637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 703637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void setFinalPosition(int position) { 704637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = position; 705637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 706637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 707637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 708637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void extendDuration(int extend) { 709637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 710637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int elapsedTime = (int) (time - mStartTime); 711637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = elapsedTime + extend; 712637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 713637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 714637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 715637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean springback(int start, int min, int max) { 716637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 717637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 718637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal = start; 719637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = 0; 720637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 721637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 722637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = 0; 723637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 724637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start < min) { 725637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, min, 0); 726637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else if (start > max) { 727637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, max, 0); 728637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 729637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 730637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return !mFinished; 731637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 732637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 733637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startSpringback(int start, int end, int velocity) { 734c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mStartTime has been set 735637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 736c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = CUBIC; 737c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mStart = start; 738c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = end; 739c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final int delta = start - end; 740c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = getDeceleration(delta); 741c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO take velocity into account 742c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mVelocity = -delta; // only sign is used 743c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = Math.abs(delta); 744c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration)); 745637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 746637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 747637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void fling(int start, int velocity, int min, int max, int over) { 748637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mOver = over; 749637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = false; 750637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mCurrVelocity = mVelocity = velocity; 751637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDuration = mSplineDuration = 0; 752637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime = AnimationUtils.currentAnimationTimeMillis(); 753fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mCurrentPosition = mStart = start; 754637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 755637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > max || start < min) { 756637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startAfterEdge(start, min, max, velocity); 757637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 758637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 759637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 760c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = SPLINE; 761637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell double totalDistance = 0.0; 762637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 763637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (velocity != 0) { 764fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mDuration = mSplineDuration = getSplineFlingDuration(velocity); 765fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne totalDistance = getSplineFlingDistance(velocity); 766637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 767637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 768637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mSplineDistance = (int) (totalDistance * Math.signum(velocity)); 769637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = start + mSplineDistance; 770637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 771637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Clamp to a valid final position 772637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal < min) { 773637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, min); 774637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = min; 775637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 776637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 777637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mFinal > max) { 778637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell adjustDuration(mStart, mFinal, max); 779637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinal = max; 780637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 781637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 782637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 783fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineDeceleration(int velocity) { 784990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff)); 785fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 786fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 787fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private double getSplineFlingDistance(int velocity) { 788fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 789fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 790990dfc6f908c8fc22bb15a3e0a36699e954ec28cAdam Powell return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l); 791fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 792fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 793fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne /* Returns the duration, expressed in milliseconds */ 794fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne private int getSplineFlingDuration(int velocity) { 795fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double l = getSplineDeceleration(velocity); 796fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double decelMinusOne = DECELERATION_RATE - 1.0; 797fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne return (int) (1000.0 * Math.exp(l / decelMinusOne)); 798fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 799fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne 800637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void fitOnBounceCurve(int start, int end, int velocity) { 801637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Simulate a bounce that started from edge 802637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float durationToApex = - velocity / mDeceleration; 803637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToApex = velocity * velocity / 2.0f / Math.abs(mDeceleration); 804637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float distanceToEdge = Math.abs(end - start); 805637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final float totalDuration = (float) Math.sqrt( 806637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration)); 807637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime -= (int) (1000.0f * (totalDuration - durationToApex)); 808637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = end; 809637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) (- mDeceleration * totalDuration); 810637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 811637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 812637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startBounceAfterEdge(int start, int end, int velocity) { 813637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity); 814637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fitOnBounceCurve(start, end, velocity); 815637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 816637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 817637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 818637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void startAfterEdge(int start, int min, int max, int velocity) { 819637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (start > min && start < max) { 820fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne Log.e("OverScroller", "startAfterEdge called from a valid position"); 821637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mFinished = true; 822637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return; 823637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 824637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final boolean positive = start > max; 825637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int edge = positive ? max : min; 826637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final int overDistance = start - edge; 827637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean keepIncreasing = overDistance * velocity >= 0; 828637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (keepIncreasing) { 829637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Will result in a bounce or a to_boundary depending on velocity. 830637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startBounceAfterEdge(start, edge, velocity); 831637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 832fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne final double totalDistance = getSplineFlingDistance(velocity); 833637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (totalDistance > Math.abs(overDistance)) { 834637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell fling(start, velocity, positive ? min : start, positive ? start : max, mOver); 835637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 836637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell startSpringback(start, edge, velocity); 837637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 838637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 839637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 840637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 841637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell void notifyEdgeReached(int start, int end, int over) { 842c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // mState is used to detect successive notifications 843c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (mState == SPLINE) { 844fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mOver = over; 845fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne mStartTime = AnimationUtils.currentAnimationTimeMillis(); 846fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne // We were in fling/scroll mode before: current velocity is such that distance to 847c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // edge is increasing. This ensures that startAfterEdge will not start a new fling. 848fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne startAfterEdge(start, end, end, (int) mCurrVelocity); 849fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne } 850637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 851637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 852637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell private void onEdgeReached() { 853637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached. 854c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne float distance = mVelocity * mVelocity / (2.0f * Math.abs(mDeceleration)); 855c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne final float sign = Math.signum(mVelocity); 856637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 857c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne if (distance > mOver) { 858c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // Default deceleration is not sufficient to slow us down before boundary 859c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDeceleration = - sign * mVelocity * mVelocity / (2.0f * mOver); 860c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne distance = mOver; 861637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 862c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne 863c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mOver = (int) distance; 864c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mState = BALLISTIC; 865c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance); 866c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne mDuration = - (int) (1000.0f * mVelocity / mDeceleration); 867637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 868637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 869637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean continueWhenFinished() { 870637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell switch (mState) { 871c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case SPLINE: 872637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Duration from start to null velocity 873637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell if (mDuration < mSplineDuration) { 874637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // If the animation was clamped, we reached the edge 875637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStart = mFinal; 876c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne // TODO Better compute speed when edge was reached 877637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mVelocity = (int) mCurrVelocity; 878637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mDeceleration = getDeceleration(mVelocity); 879637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 880637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell onEdgeReached(); 881637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } else { 882637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell // Normal stop, no need to continue 883637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return false; 884637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 885637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 886c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case BALLISTIC: 887637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell mStartTime += mDuration; 888c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne startSpringback(mFinal, mStart, 0); 889637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell break; 890c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne case CUBIC: 891c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne return false; 892637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 893637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 894637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell update(); 895637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell return true; 896637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell } 897637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 898637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell /* 899637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Update the current position and velocity for current time. Returns 900637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * true if update has been done and false if animation duration has been 901637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * reached. 902637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */ 903637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell boolean update() { 904637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long time = AnimationUtils.currentAnimationTimeMillis(); 905637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell final long currentTime = time - mStartTime; 906637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell 907311506afbba83c11b862c1f95a24dea83fab0aadAdam Powell if (currentTime == 0) { 908311506afbba83c11b862c1f95a24dea83fab0aadAdam Powell // Skip work but report that we're still going if we have a nonzero duration. 909311506afbba83c11b862c1f95a24dea83fab0aadAdam Powell return mDuration > 0; 910311506afbba83c11b862c1f95a24dea83fab0aadAdam 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