• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.animation;
18 
19 import android.graphics.Path;
20 import android.util.MathUtils;
21 
22 import androidx.core.animation.AccelerateDecelerateInterpolator;
23 import androidx.core.animation.AccelerateInterpolator;
24 import androidx.core.animation.BounceInterpolator;
25 import androidx.core.animation.DecelerateInterpolator;
26 import androidx.core.animation.Interpolator;
27 import androidx.core.animation.LinearInterpolator;
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 
70     /*
71      * ============================================================================================
72      * Standard interpolators.
73      * ============================================================================================
74      */
75 
76     /**
77      * The standard interpolator that should be used on every normal animation
78      */
79     public static final Interpolator STANDARD = new PathInterpolator(
80             0.2f, 0f, 0f, 1f);
81 
82     /**
83      * The standard accelerating interpolator that should be used on every regular movement of
84      * content that is disappearing e.g. when moving off screen.
85      */
86     public static final Interpolator STANDARD_ACCELERATE = new PathInterpolator(
87             0.3f, 0f, 1f, 1f);
88 
89     /**
90      * The standard decelerating interpolator that should be used on every regular movement of
91      * content that is appearing e.g. when coming from off screen.
92      */
93     public static final Interpolator STANDARD_DECELERATE = new PathInterpolator(
94             0f, 0f, 0f, 1f);
95 
96     /*
97      * ============================================================================================
98      * Legacy
99      * ============================================================================================
100      */
101 
102     /**
103      * The default legacy interpolator as defined in Material 1. Also known as FAST_OUT_SLOW_IN.
104      */
105     public static final Interpolator LEGACY = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
106 
107     /**
108      * The default legacy accelerating interpolator as defined in Material 1.
109      * Also known as FAST_OUT_LINEAR_IN.
110      */
111     public static final Interpolator LEGACY_ACCELERATE = new PathInterpolator(0.4f, 0f, 1f, 1f);
112 
113     /**
114      * The default legacy decelerating interpolator as defined in Material 1.
115      * Also known as LINEAR_OUT_SLOW_IN.
116      */
117     public static final Interpolator LEGACY_DECELERATE = new PathInterpolator(0f, 0f, 0.2f, 1f);
118 
119     /**
120      * Linear interpolator. Often used if the interpolator is for different properties who need
121      * different interpolations.
122      */
123     public static final Interpolator LINEAR = new LinearInterpolator();
124 
125     /*
126     * ============================================================================================
127     * Custom interpolators
128     * ============================================================================================
129     */
130 
131     public static final Interpolator FAST_OUT_SLOW_IN = LEGACY;
132     public static final Interpolator FAST_OUT_LINEAR_IN = LEGACY_ACCELERATE;
133     public static final Interpolator LINEAR_OUT_SLOW_IN = LEGACY_DECELERATE;
134 
135     /**
136      * Like {@link #FAST_OUT_SLOW_IN}, but used in case the animation is played in reverse (i.e. t
137      * goes from 1 to 0 instead of 0 to 1).
138      */
139     public static final Interpolator FAST_OUT_SLOW_IN_REVERSE =
140             new PathInterpolator(0.8f, 0f, 0.6f, 1f);
141     public static final Interpolator SLOW_OUT_LINEAR_IN = new PathInterpolator(0.8f, 0f, 1f, 1f);
142     public static final Interpolator ALPHA_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
143     public static final Interpolator ALPHA_OUT = new PathInterpolator(0f, 0f, 0.8f, 1f);
144     public static final Interpolator ACCELERATE = new AccelerateInterpolator();
145     public static final Interpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
146     public static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
147     public static final Interpolator CUSTOM_40_40 = new PathInterpolator(0.4f, 0f, 0.6f, 1f);
148     public static final Interpolator ICON_OVERSHOT = new PathInterpolator(0.4f, 0f, 0.2f, 1.4f);
149     public static final Interpolator ICON_OVERSHOT_LESS = new PathInterpolator(0.4f, 0f, 0.2f,
150             1.1f);
151     public static final Interpolator PANEL_CLOSE_ACCELERATED = new PathInterpolator(0.3f, 0, 0.5f,
152             1);
153     public static final Interpolator BOUNCE = new BounceInterpolator();
154     /**
155      * For state transitions on the control panel that lives in GlobalActions.
156      */
157     public static final Interpolator CONTROL_STATE = new PathInterpolator(0.4f, 0f, 0.2f,
158             1.0f);
159 
160     /**
161      * Interpolator to be used when animating a move based on a click. Pair with enough duration.
162      */
163     public static final Interpolator TOUCH_RESPONSE =
164             new PathInterpolator(0.3f, 0f, 0.1f, 1f);
165 
166     /**
167      * Like {@link #TOUCH_RESPONSE}, but used in case the animation is played in reverse (i.e. t
168      * goes from 1 to 0 instead of 0 to 1).
169      */
170     public static final Interpolator TOUCH_RESPONSE_REVERSE =
171             new PathInterpolator(0.9f, 0f, 0.7f, 1f);
172 
173     /*
174      * ============================================================================================
175      * Functions / Utilities
176      * ============================================================================================
177      */
178 
179     /**
180      * Calculate the amount of overshoot using an exponential falloff function with desired
181      * properties, where the overshoot smoothly transitions at the 1.0f boundary into the
182      * overshoot, retaining its acceleration.
183      *
184      * @param progress a progress value going from 0 to 1
185      * @param overshootAmount the amount > 0 of overshoot desired. A value of 0.1 means the max
186      *                        value of the overall progress will be at 1.1.
187      * @param overshootStart the point in (0,1] where the result should reach 1
188      * @return the interpolated overshoot
189      */
getOvershootInterpolation(float progress, float overshootAmount, float overshootStart)190     public static float getOvershootInterpolation(float progress, float overshootAmount,
191             float overshootStart) {
192         if (overshootAmount == 0.0f || overshootStart == 0.0f) {
193             throw new IllegalArgumentException("Invalid values for overshoot");
194         }
195         float b = MathUtils.log((overshootAmount + 1) / (overshootAmount)) / overshootStart;
196         return MathUtils.max(0.0f,
197                 (float) (1.0f - Math.exp(-b * progress)) * (overshootAmount + 1.0f));
198     }
199 
200     /**
201      * Similar to {@link #getOvershootInterpolation(float, float, float)} but the overshoot
202      * starts immediately here, instead of first having a section of non-overshooting
203      *
204      * @param progress a progress value going from 0 to 1
205      */
getOvershootInterpolation(float progress)206     public static float getOvershootInterpolation(float progress) {
207         return MathUtils.max(0.0f, (float) (1.0f - Math.exp(-4 * progress)));
208     }
209 
210     // Create the default emphasized interpolator
createEmphasizedInterpolator()211     private static PathInterpolator createEmphasizedInterpolator() {
212         Path path = new Path();
213         // Doing the same as fast_out_extra_slow_in
214         path.moveTo(0f, 0f);
215         path.cubicTo(0.05f, 0f, 0.133333f, 0.06f, 0.166666f, 0.4f);
216         path.cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f);
217         return new PathInterpolator(path);
218     }
219 }
220