Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
Jorim Jaggi | b6cdcbc | 2014-07-25 21:58:29 +0200 | [diff] [blame] | 19 | import android.animation.Animator; |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 20 | import android.util.DisplayMetrics; |
Jorim Jaggi | 8e2189d | 2019-01-08 12:43:07 +0100 | [diff] [blame] | 21 | import android.util.Log; |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 22 | import android.view.ViewPropertyAnimator; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 23 | import android.view.animation.Interpolator; |
| 24 | import android.view.animation.PathInterpolator; |
| 25 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 26 | import com.android.systemui.Interpolators; |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 27 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 28 | |
| 29 | import javax.inject.Inject; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 30 | |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 31 | /** |
| 32 | * Utility class to calculate general fling animation when the finger is released. |
| 33 | */ |
| 34 | public class FlingAnimationUtils { |
| 35 | |
Jorim Jaggi | 84988eb | 2019-01-10 17:05:12 +0100 | [diff] [blame] | 36 | private static final String TAG = "FlingAnimationUtils"; |
| 37 | |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 38 | private static final float LINEAR_OUT_SLOW_IN_X2 = 0.35f; |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 39 | private static final float LINEAR_OUT_SLOW_IN_X2_MAX = 0.68f; |
Jorim Jaggi | 2580a976 | 2014-06-25 03:08:25 +0200 | [diff] [blame] | 40 | private static final float LINEAR_OUT_FASTER_IN_X2 = 0.5f; |
| 41 | private static final float LINEAR_OUT_FASTER_IN_Y2_MIN = 0.4f; |
| 42 | private static final float LINEAR_OUT_FASTER_IN_Y2_MAX = 0.5f; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 43 | private static final float MIN_VELOCITY_DP_PER_SECOND = 250; |
Jorim Jaggi | efbd7e3 | 2014-05-21 17:14:18 +0200 | [diff] [blame] | 44 | private static final float HIGH_VELOCITY_DP_PER_SECOND = 3000; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 45 | |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 46 | private static final float LINEAR_OUT_SLOW_IN_START_GRADIENT = 0.75f; |
| 47 | private final float mSpeedUpFactor; |
Selim Cinek | 2411f76 | 2016-12-28 17:05:08 +0100 | [diff] [blame] | 48 | private final float mY2; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 49 | |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 50 | private float mMinVelocityPxPerSecond; |
| 51 | private float mMaxLengthSeconds; |
Jorim Jaggi | efbd7e3 | 2014-05-21 17:14:18 +0200 | [diff] [blame] | 52 | private float mHighVelocityPxPerSecond; |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 53 | private float mLinearOutSlowInX2; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 54 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 55 | private AnimatorProperties mAnimatorProperties = new AnimatorProperties(); |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 56 | private PathInterpolator mInterpolator; |
| 57 | private float mCachedStartGradient = -1; |
| 58 | private float mCachedVelocityFactor = -1; |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 59 | |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 60 | public FlingAnimationUtils(DisplayMetrics displayMetrics, float maxLengthSeconds) { |
| 61 | this(displayMetrics, maxLengthSeconds, 0.0f); |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param maxLengthSeconds the longest duration an animation can become in seconds |
| 66 | * @param speedUpFactor a factor from 0 to 1 how much the slow down should be shifted towards |
| 67 | * the end of the animation. 0 means it's at the beginning and no |
| 68 | * acceleration will take place. |
| 69 | */ |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 70 | public FlingAnimationUtils(DisplayMetrics displayMetrics, float maxLengthSeconds, |
| 71 | float speedUpFactor) { |
| 72 | this(displayMetrics, maxLengthSeconds, speedUpFactor, -1.0f, 1.0f); |
Selim Cinek | 2411f76 | 2016-12-28 17:05:08 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param maxLengthSeconds the longest duration an animation can become in seconds |
| 77 | * @param speedUpFactor a factor from 0 to 1 how much the slow down should be shifted towards |
| 78 | * the end of the animation. 0 means it's at the beginning and no |
| 79 | * acceleration will take place. |
| 80 | * @param x2 the x value to take for the second point of the bezier spline. If a value below 0 |
| 81 | * is provided, the value is automatically calculated. |
| 82 | * @param y2 the y value to take for the second point of the bezier spline |
| 83 | */ |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 84 | public FlingAnimationUtils(DisplayMetrics displayMetrics, float maxLengthSeconds, |
| 85 | float speedUpFactor, float x2, float y2) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 86 | mMaxLengthSeconds = maxLengthSeconds; |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 87 | mSpeedUpFactor = speedUpFactor; |
Selim Cinek | 2411f76 | 2016-12-28 17:05:08 +0100 | [diff] [blame] | 88 | if (x2 < 0) { |
| 89 | mLinearOutSlowInX2 = NotificationUtils.interpolate(LINEAR_OUT_SLOW_IN_X2, |
| 90 | LINEAR_OUT_SLOW_IN_X2_MAX, |
| 91 | mSpeedUpFactor); |
| 92 | } else { |
| 93 | mLinearOutSlowInX2 = x2; |
| 94 | } |
| 95 | mY2 = y2; |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 96 | |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 97 | mMinVelocityPxPerSecond = MIN_VELOCITY_DP_PER_SECOND * displayMetrics.density; |
| 98 | mHighVelocityPxPerSecond = HIGH_VELOCITY_DP_PER_SECOND * displayMetrics.density; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 103 | * consistent with the finger motion. |
| 104 | * |
| 105 | * @param animator the animator to apply |
| 106 | * @param currValue the current value |
| 107 | * @param endValue the end value of the animator |
| 108 | * @param velocity the current velocity of the motion |
| 109 | */ |
Jorim Jaggi | b6cdcbc | 2014-07-25 21:58:29 +0200 | [diff] [blame] | 110 | public void apply(Animator animator, float currValue, float endValue, float velocity) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 111 | apply(animator, currValue, endValue, velocity, Math.abs(endValue - currValue)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 116 | * consistent with the finger motion. |
| 117 | * |
| 118 | * @param animator the animator to apply |
| 119 | * @param currValue the current value |
| 120 | * @param endValue the end value of the animator |
| 121 | * @param velocity the current velocity of the motion |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 122 | */ |
| 123 | public void apply(ViewPropertyAnimator animator, float currValue, float endValue, |
| 124 | float velocity) { |
| 125 | apply(animator, currValue, endValue, velocity, Math.abs(endValue - currValue)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 130 | * consistent with the finger motion. |
| 131 | * |
| 132 | * @param animator the animator to apply |
| 133 | * @param currValue the current value |
| 134 | * @param endValue the end value of the animator |
| 135 | * @param velocity the current velocity of the motion |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 136 | * @param maxDistance the maximum distance for this interaction; the maximum animation length |
| 137 | * gets multiplied by the ratio between the actual distance and this value |
| 138 | */ |
Jorim Jaggi | b6cdcbc | 2014-07-25 21:58:29 +0200 | [diff] [blame] | 139 | public void apply(Animator animator, float currValue, float endValue, float velocity, |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 140 | float maxDistance) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 141 | AnimatorProperties properties = getProperties(currValue, endValue, velocity, |
| 142 | maxDistance); |
| 143 | animator.setDuration(properties.duration); |
| 144 | animator.setInterpolator(properties.interpolator); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 149 | * consistent with the finger motion. |
| 150 | * |
| 151 | * @param animator the animator to apply |
| 152 | * @param currValue the current value |
| 153 | * @param endValue the end value of the animator |
| 154 | * @param velocity the current velocity of the motion |
| 155 | * @param maxDistance the maximum distance for this interaction; the maximum animation length |
| 156 | * gets multiplied by the ratio between the actual distance and this value |
| 157 | */ |
| 158 | public void apply(ViewPropertyAnimator animator, float currValue, float endValue, |
| 159 | float velocity, float maxDistance) { |
| 160 | AnimatorProperties properties = getProperties(currValue, endValue, velocity, |
| 161 | maxDistance); |
| 162 | animator.setDuration(properties.duration); |
| 163 | animator.setInterpolator(properties.interpolator); |
| 164 | } |
| 165 | |
| 166 | private AnimatorProperties getProperties(float currValue, |
| 167 | float endValue, float velocity, float maxDistance) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 168 | float maxLengthSeconds = (float) (mMaxLengthSeconds |
| 169 | * Math.sqrt(Math.abs(endValue - currValue) / maxDistance)); |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 170 | float diff = Math.abs(endValue - currValue); |
| 171 | float velAbs = Math.abs(velocity); |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 172 | float velocityFactor = mSpeedUpFactor == 0.0f |
| 173 | ? 1.0f : Math.min(velAbs / HIGH_VELOCITY_DP_PER_SECOND, 1.0f); |
| 174 | float startGradient = NotificationUtils.interpolate(LINEAR_OUT_SLOW_IN_START_GRADIENT, |
Selim Cinek | 2411f76 | 2016-12-28 17:05:08 +0100 | [diff] [blame] | 175 | mY2 / mLinearOutSlowInX2, velocityFactor); |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 176 | float durationSeconds = startGradient * diff / velAbs; |
| 177 | Interpolator slowInInterpolator = getInterpolator(startGradient, velocityFactor); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 178 | if (durationSeconds <= maxLengthSeconds) { |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 179 | mAnimatorProperties.interpolator = slowInInterpolator; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 180 | } else if (velAbs >= mMinVelocityPxPerSecond) { |
| 181 | |
| 182 | // Cross fade between fast-out-slow-in and linear interpolator with current velocity. |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 183 | durationSeconds = maxLengthSeconds; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 184 | VelocityInterpolator velocityInterpolator |
| 185 | = new VelocityInterpolator(durationSeconds, velAbs, diff); |
| 186 | InterpolatorInterpolator superInterpolator = new InterpolatorInterpolator( |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 187 | velocityInterpolator, slowInInterpolator, Interpolators.LINEAR_OUT_SLOW_IN); |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 188 | mAnimatorProperties.interpolator = superInterpolator; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 189 | } else { |
| 190 | |
| 191 | // Just use a normal interpolator which doesn't take the velocity into account. |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 192 | durationSeconds = maxLengthSeconds; |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 193 | mAnimatorProperties.interpolator = Interpolators.FAST_OUT_SLOW_IN; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 194 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 195 | mAnimatorProperties.duration = (long) (durationSeconds * 1000); |
| 196 | return mAnimatorProperties; |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 199 | private Interpolator getInterpolator(float startGradient, float velocityFactor) { |
Jorim Jaggi | 84988eb | 2019-01-10 17:05:12 +0100 | [diff] [blame] | 200 | if (Float.isNaN(velocityFactor)) { |
| 201 | Log.e(TAG, "Invalid velocity factor", new Throwable()); |
| 202 | return Interpolators.LINEAR_OUT_SLOW_IN; |
| 203 | } |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 204 | if (startGradient != mCachedStartGradient |
| 205 | || velocityFactor != mCachedVelocityFactor) { |
| 206 | float speedup = mSpeedUpFactor * (1.0f - velocityFactor); |
Jorim Jaggi | 8e2189d | 2019-01-08 12:43:07 +0100 | [diff] [blame] | 207 | float x1 = speedup; |
| 208 | float y1 = speedup * startGradient; |
| 209 | float x2 = mLinearOutSlowInX2; |
| 210 | float y2 = mY2; |
| 211 | try { |
| 212 | mInterpolator = new PathInterpolator(x1, y1, x2, y2); |
| 213 | } catch (IllegalArgumentException e) { |
| 214 | throw new IllegalArgumentException("Illegal path with " |
| 215 | + "x1=" + x1 + " y1=" + y1 + " x2=" + x2 + " y2=" + y2, e); |
| 216 | } |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 217 | mCachedStartGradient = startGradient; |
| 218 | mCachedVelocityFactor = velocityFactor; |
| 219 | } |
| 220 | return mInterpolator; |
| 221 | } |
| 222 | |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 223 | /** |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 224 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 225 | * consistent with the finger motion for the case when the animation is making something |
| 226 | * disappear. |
| 227 | * |
| 228 | * @param animator the animator to apply |
| 229 | * @param currValue the current value |
| 230 | * @param endValue the end value of the animator |
| 231 | * @param velocity the current velocity of the motion |
| 232 | * @param maxDistance the maximum distance for this interaction; the maximum animation length |
| 233 | * gets multiplied by the ratio between the actual distance and this value |
| 234 | */ |
Jorim Jaggi | b6cdcbc | 2014-07-25 21:58:29 +0200 | [diff] [blame] | 235 | public void applyDismissing(Animator animator, float currValue, float endValue, |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 236 | float velocity, float maxDistance) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 237 | AnimatorProperties properties = getDismissingProperties(currValue, endValue, velocity, |
| 238 | maxDistance); |
| 239 | animator.setDuration(properties.duration); |
| 240 | animator.setInterpolator(properties.interpolator); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Applies the interpolator and length to the animator, such that the fling animation is |
| 245 | * consistent with the finger motion for the case when the animation is making something |
| 246 | * disappear. |
| 247 | * |
| 248 | * @param animator the animator to apply |
| 249 | * @param currValue the current value |
| 250 | * @param endValue the end value of the animator |
| 251 | * @param velocity the current velocity of the motion |
| 252 | * @param maxDistance the maximum distance for this interaction; the maximum animation length |
| 253 | * gets multiplied by the ratio between the actual distance and this value |
| 254 | */ |
| 255 | public void applyDismissing(ViewPropertyAnimator animator, float currValue, float endValue, |
| 256 | float velocity, float maxDistance) { |
| 257 | AnimatorProperties properties = getDismissingProperties(currValue, endValue, velocity, |
| 258 | maxDistance); |
| 259 | animator.setDuration(properties.duration); |
| 260 | animator.setInterpolator(properties.interpolator); |
| 261 | } |
| 262 | |
| 263 | private AnimatorProperties getDismissingProperties(float currValue, float endValue, |
| 264 | float velocity, float maxDistance) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 265 | float maxLengthSeconds = (float) (mMaxLengthSeconds |
| 266 | * Math.pow(Math.abs(endValue - currValue) / maxDistance, 0.5f)); |
| 267 | float diff = Math.abs(endValue - currValue); |
| 268 | float velAbs = Math.abs(velocity); |
Jorim Jaggi | efbd7e3 | 2014-05-21 17:14:18 +0200 | [diff] [blame] | 269 | float y2 = calculateLinearOutFasterInY2(velAbs); |
| 270 | |
Jorim Jaggi | 2580a976 | 2014-06-25 03:08:25 +0200 | [diff] [blame] | 271 | float startGradient = y2 / LINEAR_OUT_FASTER_IN_X2; |
| 272 | Interpolator mLinearOutFasterIn = new PathInterpolator(0, 0, LINEAR_OUT_FASTER_IN_X2, y2); |
Jorim Jaggi | efbd7e3 | 2014-05-21 17:14:18 +0200 | [diff] [blame] | 273 | float durationSeconds = startGradient * diff / velAbs; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 274 | if (durationSeconds <= maxLengthSeconds) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 275 | mAnimatorProperties.interpolator = mLinearOutFasterIn; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 276 | } else if (velAbs >= mMinVelocityPxPerSecond) { |
| 277 | |
| 278 | // Cross fade between linear-out-faster-in and linear interpolator with current |
| 279 | // velocity. |
| 280 | durationSeconds = maxLengthSeconds; |
| 281 | VelocityInterpolator velocityInterpolator |
| 282 | = new VelocityInterpolator(durationSeconds, velAbs, diff); |
| 283 | InterpolatorInterpolator superInterpolator = new InterpolatorInterpolator( |
Selim Cinek | 593ee20 | 2016-12-28 15:01:16 +0100 | [diff] [blame] | 284 | velocityInterpolator, mLinearOutFasterIn, Interpolators.LINEAR_OUT_SLOW_IN); |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 285 | mAnimatorProperties.interpolator = superInterpolator; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 286 | } else { |
| 287 | |
| 288 | // Just use a normal interpolator which doesn't take the velocity into account. |
| 289 | durationSeconds = maxLengthSeconds; |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 290 | mAnimatorProperties.interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 291 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 292 | mAnimatorProperties.duration = (long) (durationSeconds * 1000); |
| 293 | return mAnimatorProperties; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Jorim Jaggi | efbd7e3 | 2014-05-21 17:14:18 +0200 | [diff] [blame] | 297 | * Calculates the y2 control point for a linear-out-faster-in path interpolator depending on the |
| 298 | * velocity. The faster the velocity, the more "linear" the interpolator gets. |
| 299 | * |
| 300 | * @param velocity the velocity of the gesture. |
| 301 | * @return the y2 control point for a cubic bezier path interpolator |
| 302 | */ |
| 303 | private float calculateLinearOutFasterInY2(float velocity) { |
| 304 | float t = (velocity - mMinVelocityPxPerSecond) |
| 305 | / (mHighVelocityPxPerSecond - mMinVelocityPxPerSecond); |
| 306 | t = Math.max(0, Math.min(1, t)); |
| 307 | return (1 - t) * LINEAR_OUT_FASTER_IN_Y2_MIN + t * LINEAR_OUT_FASTER_IN_Y2_MAX; |
| 308 | } |
| 309 | |
| 310 | /** |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 311 | * @return the minimum velocity a gesture needs to have to be considered a fling |
| 312 | */ |
| 313 | public float getMinVelocityPxPerSecond() { |
| 314 | return mMinVelocityPxPerSecond; |
| 315 | } |
| 316 | |
| 317 | /** |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 318 | * An interpolator which interpolates two interpolators with an interpolator. |
| 319 | */ |
| 320 | private static final class InterpolatorInterpolator implements Interpolator { |
| 321 | |
| 322 | private Interpolator mInterpolator1; |
| 323 | private Interpolator mInterpolator2; |
| 324 | private Interpolator mCrossfader; |
| 325 | |
| 326 | InterpolatorInterpolator(Interpolator interpolator1, Interpolator interpolator2, |
| 327 | Interpolator crossfader) { |
| 328 | mInterpolator1 = interpolator1; |
| 329 | mInterpolator2 = interpolator2; |
| 330 | mCrossfader = crossfader; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public float getInterpolation(float input) { |
| 335 | float t = mCrossfader.getInterpolation(input); |
| 336 | return (1 - t) * mInterpolator1.getInterpolation(input) |
| 337 | + t * mInterpolator2.getInterpolation(input); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * An interpolator which interpolates with a fixed velocity. |
| 343 | */ |
| 344 | private static final class VelocityInterpolator implements Interpolator { |
| 345 | |
| 346 | private float mDurationSeconds; |
| 347 | private float mVelocity; |
| 348 | private float mDiff; |
| 349 | |
| 350 | private VelocityInterpolator(float durationSeconds, float velocity, float diff) { |
| 351 | mDurationSeconds = durationSeconds; |
| 352 | mVelocity = velocity; |
| 353 | mDiff = diff; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public float getInterpolation(float input) { |
| 358 | float time = input * mDurationSeconds; |
| 359 | return time * mVelocity / mDiff; |
| 360 | } |
| 361 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 362 | |
| 363 | private static class AnimatorProperties { |
| 364 | Interpolator interpolator; |
| 365 | long duration; |
| 366 | } |
| 367 | |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 368 | public static class Builder { |
| 369 | private final DisplayMetrics mDisplayMetrics; |
| 370 | float mMaxLengthSeconds; |
Dave Mankoff | 59b9496 | 2019-12-16 16:42:15 -0500 | [diff] [blame] | 371 | float mSpeedUpFactor; |
| 372 | float mX2; |
| 373 | float mY2; |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 374 | |
| 375 | @Inject |
| 376 | public Builder(DisplayMetrics displayMetrics) { |
| 377 | mDisplayMetrics = displayMetrics; |
Dave Mankoff | 59b9496 | 2019-12-16 16:42:15 -0500 | [diff] [blame] | 378 | reset(); |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | public Builder setMaxLengthSeconds(float maxLengthSeconds) { |
| 382 | mMaxLengthSeconds = maxLengthSeconds; |
| 383 | return this; |
| 384 | } |
| 385 | |
| 386 | public Builder setSpeedUpFactor(float speedUpFactor) { |
| 387 | mSpeedUpFactor = speedUpFactor; |
| 388 | return this; |
| 389 | } |
| 390 | |
| 391 | public Builder setX2(float x2) { |
| 392 | mX2 = x2; |
| 393 | return this; |
| 394 | } |
| 395 | |
| 396 | public Builder setY2(float y2) { |
| 397 | mY2 = y2; |
| 398 | return this; |
| 399 | } |
| 400 | |
Dave Mankoff | 59b9496 | 2019-12-16 16:42:15 -0500 | [diff] [blame] | 401 | public Builder reset() { |
| 402 | mMaxLengthSeconds = 0; |
| 403 | mSpeedUpFactor = 0.0f; |
| 404 | mX2 = -1.0f; |
| 405 | mY2 = 1.0f; |
| 406 | |
| 407 | return this; |
| 408 | } |
| 409 | |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 410 | public FlingAnimationUtils build() { |
| 411 | return new FlingAnimationUtils(mDisplayMetrics, mMaxLengthSeconds, mSpeedUpFactor, |
| 412 | mX2, mY2); |
| 413 | } |
| 414 | } |
Jorim Jaggi | 87cd5e7 | 2014-05-12 23:29:10 +0200 | [diff] [blame] | 415 | } |