• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.app.animation;
18 
19 import android.graphics.Path;
20 
21 import androidx.core.animation.AccelerateDecelerateInterpolator;
22 import androidx.core.animation.AccelerateInterpolator;
23 import androidx.core.animation.BounceInterpolator;
24 import androidx.core.animation.DecelerateInterpolator;
25 import androidx.core.animation.Interpolator;
26 import androidx.core.animation.LinearInterpolator;
27 import androidx.core.animation.OvershootInterpolator;
28 import androidx.core.animation.PathInterpolator;
29 
30 /**
31  * Utility class to receive interpolators from. (androidx compatible version)
32  *
33  * This is the androidx compatible version of {@link Interpolators}. Make sure that changes made to
34  * this class are also reflected in {@link Interpolators}.
35  *
36  * Using the androidx versions of {@link androidx.core.animation.ValueAnimator} or
37  * {@link androidx.core.animation.ObjectAnimator} improves animation testability. This file provides
38  * the androidx compatible versions of the interpolators defined in {@link Interpolators}.
39  * AnimatorTestRule can be used in Tests to manipulate the animation under test (e.g. artificially
40  * advancing the time).
41  */
42 public class InterpolatorsAndroidX {
43 
44     /*
45      * ============================================================================================
46      * Emphasized interpolators.
47      * ============================================================================================
48      */
49 
50     /**
51      * The default emphasized interpolator. Used for hero / emphasized movement of content.
52      */
53     public static final Interpolator EMPHASIZED = createEmphasizedInterpolator();
54 
55     /**
56      * The accelerated emphasized interpolator. Used for hero / emphasized movement of content that
57      * is disappearing e.g. when moving off screen.
58      */
59     public static final Interpolator EMPHASIZED_ACCELERATE = new PathInterpolator(
60             0.3f, 0f, 0.8f, 0.15f);
61 
62     /**
63      * The decelerating emphasized interpolator. Used for hero / emphasized movement of content that
64      * is appearing e.g. when coming from off screen
65      */
66     public static final Interpolator EMPHASIZED_DECELERATE = new PathInterpolator(
67             0.05f, 0.7f, 0.1f, 1f);
68 
69     public static final Interpolator EXAGGERATED_EASE;
70     static {
71         Path exaggeratedEase = new Path();
72         exaggeratedEase.moveTo(0, 0);
73         exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
74         exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
75         EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
76     }
77 
78     public static final Interpolator INSTANT = t -> 1;
79     /**
80      * All values of t map to 0 until t == 1. This is primarily useful for setting view visibility,
81      * which should only happen at the very end of the animation (when it's already hidden).
82      */
83     public static final Interpolator FINAL_FRAME = t -> t < 1 ? 0 : 1;
84 
85     public static final Interpolator OVERSHOOT_0_75 = new OvershootInterpolator(0.75f);
86     public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
87     public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f);
88 
89     /*
90      * ============================================================================================
91      * Standard interpolators.
92      * ============================================================================================
93      */
94 
95     /**
96      * The standard interpolator that should be used on every normal animation
97      */
98     public static final Interpolator STANDARD = new PathInterpolator(
99             0.2f, 0f, 0f, 1f);
100 
101     /**
102      * The standard accelerating interpolator that should be used on every regular movement of
103      * content that is disappearing e.g. when moving off screen.
104      */
105     public static final Interpolator STANDARD_ACCELERATE = new PathInterpolator(
106             0.3f, 0f, 1f, 1f);
107 
108     /**
109      * The standard decelerating interpolator that should be used on every regular movement of
110      * content that is appearing e.g. when coming from off screen.
111      */
112     public static final Interpolator STANDARD_DECELERATE = new PathInterpolator(
113             0f, 0f, 0f, 1f);
114 
115     /*
116      * ============================================================================================
117      * Legacy
118      * ============================================================================================
119      */
120 
121     /**
122      * The default legacy interpolator as defined in Material 1. Also known as FAST_OUT_SLOW_IN.
123      */
124     public static final Interpolator LEGACY = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
125 
126     /**
127      * The default legacy accelerating interpolator as defined in Material 1.
128      * Also known as FAST_OUT_LINEAR_IN.
129      */
130     public static final Interpolator LEGACY_ACCELERATE = new PathInterpolator(0.4f, 0f, 1f, 1f);
131 
132     /**
133      * The default legacy decelerating interpolator as defined in Material 1.
134      * Also known as LINEAR_OUT_SLOW_IN.
135      */
136     public static final Interpolator LEGACY_DECELERATE = new PathInterpolator(0f, 0f, 0.2f, 1f);
137 
138     /**
139      * Linear interpolator. Often used if the interpolator is for different properties who need
140      * different interpolations.
141      */
142     public static final Interpolator LINEAR = new LinearInterpolator();
143 
144     /*
145      * ============================================================================================
146      * Custom interpolators
147      * ============================================================================================
148      */
149 
150     public static final Interpolator FAST_OUT_SLOW_IN = LEGACY;
151     public static final Interpolator FAST_OUT_LINEAR_IN = LEGACY_ACCELERATE;
152     public static final Interpolator LINEAR_OUT_SLOW_IN = LEGACY_DECELERATE;
153 
154     /**
155      * Like {@link #FAST_OUT_SLOW_IN}, but used in case the animation is played in reverse (i.e. t
156      * goes from 1 to 0 instead of 0 to 1).
157      */
158     public static final Interpolator FAST_OUT_SLOW_IN_REVERSE =
159             new PathInterpolator(0.8f, 0f, 0.6f, 1f);
160     public static final Interpolator SLOW_OUT_LINEAR_IN = new PathInterpolator(0.8f, 0f, 1f, 1f);
161     public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
162     public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);
163 
164     public static final Interpolator DECELERATED_EASE = new PathInterpolator(0, 0, .2f, 1f);
165     public static final Interpolator ACCELERATED_EASE = new PathInterpolator(0.4f, 0, 1f, 1f);
166     public static final Interpolator PREDICTIVE_BACK_DECELERATED_EASE =
167             new PathInterpolator(0, 0, 0, 1f);
168     public static final Interpolator ALPHA_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
169     public static final Interpolator ALPHA_OUT = new PathInterpolator(0f, 0f, 0.8f, 1f);
170     public static final Interpolator ACCELERATE = new AccelerateInterpolator();
171     public static final Interpolator ACCELERATE_0_5 = new AccelerateInterpolator(0.5f);
172     public static final Interpolator ACCELERATE_0_75 = new AccelerateInterpolator(0.75f);
173     public static final Interpolator ACCELERATE_1_5 = new AccelerateInterpolator(1.5f);
174     public static final Interpolator ACCELERATE_2 = new AccelerateInterpolator(2);
175     public static final Interpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
176     public static final Interpolator DECELERATE = new DecelerateInterpolator();
177     public static final Interpolator DECELERATE_1_5 = new DecelerateInterpolator(1.5f);
178     public static final Interpolator DECELERATE_1_7 = new DecelerateInterpolator(1.7f);
179     public static final Interpolator DECELERATE_2 = new DecelerateInterpolator(2);
180     public static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
181     public static final Interpolator DECELERATE_3 = new DecelerateInterpolator(3f);
182     public static final Interpolator CUSTOM_40_40 = new PathInterpolator(0.4f, 0f, 0.6f, 1f);
183     public static final Interpolator ICON_OVERSHOT = new PathInterpolator(0.4f, 0f, 0.2f, 1.4f);
184     public static final Interpolator ICON_OVERSHOT_LESS = new PathInterpolator(0.4f, 0f, 0.2f,
185             1.1f);
186     public static final Interpolator PANEL_CLOSE_ACCELERATED = new PathInterpolator(0.3f, 0, 0.5f,
187             1);
188     public static final Interpolator BOUNCE = new BounceInterpolator();
189     /**
190      * For state transitions on the control panel that lives in GlobalActions.
191      */
192     public static final Interpolator CONTROL_STATE = new PathInterpolator(0.4f, 0f, 0.2f,
193             1.0f);
194 
195     /**
196      * Interpolator to be used when animating a move based on a click. Pair with enough duration.
197      */
198     public static final Interpolator TOUCH_RESPONSE =
199             new PathInterpolator(0.3f, 0f, 0.1f, 1f);
200 
201     /**
202      * Like {@link #TOUCH_RESPONSE}, but used in case the animation is played in reverse (i.e. t
203      * goes from 1 to 0 instead of 0 to 1).
204      */
205     public static final Interpolator TOUCH_RESPONSE_REVERSE =
206             new PathInterpolator(0.9f, 0f, 0.7f, 1f);
207 
208     public static final Interpolator TOUCH_RESPONSE_ACCEL_DEACCEL =
209             v -> ACCELERATE_DECELERATE.getInterpolation(TOUCH_RESPONSE.getInterpolation(v));
210 
211 
212     /**
213      * Inversion of ZOOM_OUT, compounded with an ease-out.
214      */
215     public static final Interpolator ZOOM_IN = new Interpolator() {
216         @Override
217         public float getInterpolation(float v) {
218             return DECELERATE_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
219         }
220     };
221 
222     public static final Interpolator ZOOM_OUT = new Interpolator() {
223 
224         private static final float FOCAL_LENGTH = 0.35f;
225 
226         @Override
227         public float getInterpolation(float v) {
228             return zInterpolate(v);
229         }
230 
231         /**
232          * This interpolator emulates the rate at which the perceived scale of an object changes
233          * as its distance from a camera increases. When this interpolator is applied to a scale
234          * animation on a view, it evokes the sense that the object is shrinking due to moving away
235          * from the camera.
236          */
237         private float zInterpolate(float input) {
238             return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
239                     (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
240         }
241     };
242 
243     public static final Interpolator SCROLL = new Interpolator() {
244         @Override
245         public float getInterpolation(float t) {
246             t -= 1.0f;
247             return t*t*t*t*t + 1;
248         }
249     };
250 
251     public static final Interpolator SCROLL_CUBIC = new Interpolator() {
252         @Override
253         public float getInterpolation(float t) {
254             t -= 1.0f;
255             return t*t*t + 1;
256         }
257     };
258 
259     private static final float FAST_FLING_PX_MS = 10;
260 
261     /*
262      * ============================================================================================
263      * Functions / Utilities
264      * ============================================================================================
265      */
266 
scrollInterpolatorForVelocity(float velocity)267     public static Interpolator scrollInterpolatorForVelocity(float velocity) {
268         return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
269     }
270 
271     /**
272      * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms).
273      * @param velocity The start velocity of the animation we want to overshoot.
274      */
overshootInterpolatorForVelocity(float velocity)275     public static Interpolator overshootInterpolatorForVelocity(float velocity) {
276         return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f));
277     }
278 
279     /**
280      * Calculate the amount of overshoot using an exponential falloff function with desired
281      * properties, where the overshoot smoothly transitions at the 1.0f boundary into the
282      * overshoot, retaining its acceleration.
283      *
284      * @param progress a progress value going from 0 to 1
285      * @param overshootAmount the amount > 0 of overshoot desired. A value of 0.1 means the max
286      *                        value of the overall progress will be at 1.1.
287      * @param overshootStart the point in (0,1] where the result should reach 1
288      * @return the interpolated overshoot
289      */
getOvershootInterpolation(float progress, float overshootAmount, float overshootStart)290     public static float getOvershootInterpolation(float progress, float overshootAmount,
291             float overshootStart) {
292         if (overshootAmount == 0.0f || overshootStart == 0.0f) {
293             throw new IllegalArgumentException("Invalid values for overshoot");
294         }
295         float b = MathUtils.log((overshootAmount + 1) / (overshootAmount)) / overshootStart;
296         return MathUtils.max(0.0f,
297                 (float) (1.0f - Math.exp(-b * progress)) * (overshootAmount + 1.0f));
298     }
299 
300     /**
301      * Similar to {@link #getOvershootInterpolation(float, float, float)} but the overshoot
302      * starts immediately here, instead of first having a section of non-overshooting
303      *
304      * @param progress a progress value going from 0 to 1
305      */
getOvershootInterpolation(float progress)306     public static float getOvershootInterpolation(float progress) {
307         return MathUtils.max(0.0f, (float) (1.0f - Math.exp(-4 * progress)));
308     }
309 
310     // Create the default emphasized interpolator
createEmphasizedInterpolator()311     private static PathInterpolator createEmphasizedInterpolator() {
312         Path path = new Path();
313         // Doing the same as fast_out_extra_slow_in
314         path.moveTo(0f, 0f);
315         path.cubicTo(0.05f, 0f, 0.133333f, 0.06f, 0.166666f, 0.4f);
316         path.cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f);
317         return new PathInterpolator(path);
318     }
319 
320     /**
321      * Returns a function that runs the given interpolator such that the entire progress is set
322      * between the given bounds. That is, we set the interpolation to 0 until lowerBound and reach
323      * 1 by upperBound.
324      */
clampToProgress(Interpolator interpolator, float lowerBound, float upperBound)325     public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
326             float upperBound) {
327         if (upperBound < lowerBound) {
328             throw new IllegalArgumentException(
329                     String.format("upperBound (%f) must be greater than lowerBound (%f)",
330                             upperBound, lowerBound));
331         }
332         return t -> clampToProgress(interpolator, t, lowerBound, upperBound);
333     }
334 
335     /**
336      * Returns the progress value's progress between the lower and upper bounds. That is, the
337      * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
338      *
339      * Between lowerBound and upperBound, the progress value will be interpolated using the provided
340      * interpolator.
341      */
clampToProgress( Interpolator interpolator, float progress, float lowerBound, float upperBound)342     public static float clampToProgress(
343             Interpolator interpolator, float progress, float lowerBound, float upperBound) {
344         if (upperBound < lowerBound) {
345             throw new IllegalArgumentException(
346                     String.format("upperBound (%f) must be greater than lowerBound (%f)",
347                             upperBound, lowerBound));
348         }
349 
350         if (progress == lowerBound && progress == upperBound) {
351             return progress == 0f ? 0 : 1;
352         }
353         if (progress < lowerBound) {
354             return 0;
355         }
356         if (progress > upperBound) {
357             return 1;
358         }
359         return interpolator.getInterpolation((progress - lowerBound) / (upperBound - lowerBound));
360     }
361 
362     /**
363      * Returns the progress value's progress between the lower and upper bounds. That is, the
364      * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
365      */
clampToProgress(float progress, float lowerBound, float upperBound)366     public static float clampToProgress(float progress, float lowerBound, float upperBound) {
367         return clampToProgress(LINEAR, progress, lowerBound, upperBound);
368     }
369 
mapRange(float value, float min, float max)370     private static float mapRange(float value, float min, float max) {
371         return min + (value * (max - min));
372     }
373 
374     /**
375      * Runs the given interpolator such that the interpolated value is mapped to the given range.
376      * This is useful, for example, if we only use this interpolator for part of the animation,
377      * such as to take over a user-controlled animation when they let go.
378      */
mapToProgress(Interpolator interpolator, float lowerBound, float upperBound)379     public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound,
380             float upperBound) {
381         return t -> mapRange(interpolator.getInterpolation(t), lowerBound, upperBound);
382     }
383 
384     /**
385      * Returns the reverse of the provided interpolator, following the formula: g(x) = 1 - f(1 - x).
386      * In practice, this means that if f is an interpolator used to model a value animating between
387      * m and n, g is the interpolator to use to obtain the specular behavior when animating from n
388      * to m.
389      */
reverse(Interpolator interpolator)390     public static Interpolator reverse(Interpolator interpolator) {
391         return t -> 1 - interpolator.getInterpolation(1 - t);
392     }
393 }