• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.launcher3.anim;
18 
19 import android.graphics.Path;
20 import android.view.animation.AccelerateDecelerateInterpolator;
21 import android.view.animation.AccelerateInterpolator;
22 import android.view.animation.DecelerateInterpolator;
23 import android.view.animation.Interpolator;
24 import android.view.animation.LinearInterpolator;
25 import android.view.animation.OvershootInterpolator;
26 import android.view.animation.PathInterpolator;
27 
28 import com.android.launcher3.Utilities;
29 
30 /**
31  * Common interpolators used in Launcher
32  */
33 public class Interpolators {
34 
35     public static final Interpolator LINEAR = new LinearInterpolator();
36 
37     public static final Interpolator ACCEL = new AccelerateInterpolator();
38     public static final Interpolator ACCEL_0_5 = new AccelerateInterpolator(0.5f);
39     public static final Interpolator ACCEL_0_75 = new AccelerateInterpolator(0.75f);
40     public static final Interpolator ACCEL_1_5 = new AccelerateInterpolator(1.5f);
41     public static final Interpolator ACCEL_2 = new AccelerateInterpolator(2);
42 
43     public static final Interpolator DEACCEL = new DecelerateInterpolator();
44     public static final Interpolator DEACCEL_1_5 = new DecelerateInterpolator(1.5f);
45     public static final Interpolator DEACCEL_1_7 = new DecelerateInterpolator(1.7f);
46     public static final Interpolator DEACCEL_2 = new DecelerateInterpolator(2);
47     public static final Interpolator DEACCEL_2_5 = new DecelerateInterpolator(2.5f);
48     public static final Interpolator DEACCEL_3 = new DecelerateInterpolator(3f);
49 
50     public static final Interpolator ACCEL_DEACCEL = new AccelerateDecelerateInterpolator();
51 
52     public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
53 
54     public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
55     public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);
56 
57     public static final Interpolator DECELERATED_EASE = new PathInterpolator(0, 0, .2f, 1f);
58     public static final Interpolator ACCELERATED_EASE = new PathInterpolator(0.4f, 0, 1f, 1f);
59 
60     public static final Interpolator EXAGGERATED_EASE;
61 
62     public static final Interpolator INSTANT = t -> 1;
63     /**
64      * All values of t map to 0 until t == 1. This is primarily useful for setting view visibility,
65      * which should only happen at the very end of the animation (when it's already hidden).
66      */
67     public static final Interpolator FINAL_FRAME = t -> t < 1 ? 0 : 1;
68 
69     static {
70         Path exaggeratedEase = new Path();
71         exaggeratedEase.moveTo(0, 0);
72         exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
73         exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
74         EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
75     }
76 
77     public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
78     public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f);
79 
80     public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR =
81             new PathInterpolator(0.3f, 0f, 0.1f, 1f);
82     public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR_ACCEL_DEACCEL =
83             v -> ACCEL_DEACCEL.getInterpolation(TOUCH_RESPONSE_INTERPOLATOR.getInterpolation(v));
84 
85 
86     /**
87      * Inversion of ZOOM_OUT, compounded with an ease-out.
88      */
89     public static final Interpolator ZOOM_IN = new Interpolator() {
90         @Override
91         public float getInterpolation(float v) {
92             return DEACCEL_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
93         }
94     };
95 
96     public static final Interpolator ZOOM_OUT = new Interpolator() {
97 
98         private static final float FOCAL_LENGTH = 0.35f;
99 
100         @Override
101         public float getInterpolation(float v) {
102             return zInterpolate(v);
103         }
104 
105         /**
106          * This interpolator emulates the rate at which the perceived scale of an object changes
107          * as its distance from a camera increases. When this interpolator is applied to a scale
108          * animation on a view, it evokes the sense that the object is shrinking due to moving away
109          * from the camera.
110          */
111         private float zInterpolate(float input) {
112             return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
113                     (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
114         }
115     };
116 
117     public static final Interpolator SCROLL = new Interpolator() {
118         @Override
119         public float getInterpolation(float t) {
120             t -= 1.0f;
121             return t*t*t*t*t + 1;
122         }
123     };
124 
125     public static final Interpolator SCROLL_CUBIC = new Interpolator() {
126         @Override
127         public float getInterpolation(float t) {
128             t -= 1.0f;
129             return t*t*t + 1;
130         }
131     };
132 
133     private static final float FAST_FLING_PX_MS = 10;
134 
scrollInterpolatorForVelocity(float velocity)135     public static Interpolator scrollInterpolatorForVelocity(float velocity) {
136         return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
137     }
138 
139     /**
140      * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms).
141      * @param velocity The start velocity of the animation we want to overshoot.
142      */
overshootInterpolatorForVelocity(float velocity)143     public static Interpolator overshootInterpolatorForVelocity(float velocity) {
144         return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f));
145     }
146 
147     /**
148      * Runs the given interpolator such that the entire progress is set between the given bounds.
149      * That is, we set the interpolation to 0 until lowerBound and reach 1 by upperBound.
150      */
clampToProgress(Interpolator interpolator, float lowerBound, float upperBound)151     public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
152             float upperBound) {
153         if (upperBound < lowerBound) {
154             throw new IllegalArgumentException(
155                     String.format("upperBound (%f) must be greater than lowerBound (%f)",
156                             upperBound, lowerBound));
157         }
158         return t -> {
159             if (t == lowerBound && t == upperBound) {
160                 return t == 0f ? 0 : 1;
161             }
162             if (t < lowerBound) {
163                 return 0;
164             }
165             if (t > upperBound) {
166                 return 1;
167             }
168             return interpolator.getInterpolation((t - lowerBound) / (upperBound - lowerBound));
169         };
170     }
171 
172     /**
173      * Runs the given interpolator such that the interpolated value is mapped to the given range.
174      * This is useful, for example, if we only use this interpolator for part of the animation,
175      * such as to take over a user-controlled animation when they let go.
176      */
177     public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound,
178             float upperBound) {
179         return t -> Utilities.mapRange(interpolator.getInterpolation(t), lowerBound, upperBound);
180     }
181 }
182