• 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 static com.android.launcher3.Utilities.SINGLE_FRAME_MS;
20 
21 import android.graphics.Path;
22 import android.view.animation.AccelerateDecelerateInterpolator;
23 import android.view.animation.AccelerateInterpolator;
24 import android.view.animation.DecelerateInterpolator;
25 import android.view.animation.Interpolator;
26 import android.view.animation.LinearInterpolator;
27 import android.view.animation.OvershootInterpolator;
28 import android.view.animation.PathInterpolator;
29 
30 import com.android.launcher3.Utilities;
31 
32 
33 /**
34  * Common interpolators used in Launcher
35  */
36 public class Interpolators {
37 
38     public static final Interpolator LINEAR = new LinearInterpolator();
39 
40     public static final Interpolator ACCEL = new AccelerateInterpolator();
41     public static final Interpolator ACCEL_1_5 = new AccelerateInterpolator(1.5f);
42     public static final Interpolator ACCEL_2 = new AccelerateInterpolator(2);
43 
44     public static final Interpolator DEACCEL = new DecelerateInterpolator();
45     public static final Interpolator DEACCEL_1_5 = new DecelerateInterpolator(1.5f);
46     public static final Interpolator DEACCEL_1_7 = new DecelerateInterpolator(1.7f);
47     public static final Interpolator DEACCEL_2 = new DecelerateInterpolator(2);
48     public static final Interpolator DEACCEL_2_5 = new DecelerateInterpolator(2.5f);
49     public static final Interpolator DEACCEL_3 = new DecelerateInterpolator(3f);
50 
51     public static final Interpolator ACCEL_DEACCEL = new AccelerateDecelerateInterpolator();
52 
53     public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
54 
55     public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
56     public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);
57 
58     public static final Interpolator EXAGGERATED_EASE;
59 
60     public static final Interpolator INSTANT = t -> 1;
61 
62     private static final int MIN_SETTLE_DURATION = 200;
63     private static final float OVERSHOOT_FACTOR = 0.9f;
64 
65     static {
66         Path exaggeratedEase = new Path();
67         exaggeratedEase.moveTo(0, 0);
68         exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
69         exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
70         EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
71     }
72 
73     public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
74     public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f);
75 
76     public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR =
77             new PathInterpolator(0.3f, 0f, 0.1f, 1f);
78 
79     /**
80      * Inversion of ZOOM_OUT, compounded with an ease-out.
81      */
82     public static final Interpolator ZOOM_IN = new Interpolator() {
83         @Override
84         public float getInterpolation(float v) {
85             return DEACCEL_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
86         }
87     };
88 
89     public static final Interpolator ZOOM_OUT = new Interpolator() {
90 
91         private static final float FOCAL_LENGTH = 0.35f;
92 
93         @Override
94         public float getInterpolation(float v) {
95             return zInterpolate(v);
96         }
97 
98         /**
99          * This interpolator emulates the rate at which the perceived scale of an object changes
100          * as its distance from a camera increases. When this interpolator is applied to a scale
101          * animation on a view, it evokes the sense that the object is shrinking due to moving away
102          * from the camera.
103          */
104         private float zInterpolate(float input) {
105             return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
106                     (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
107         }
108     };
109 
110     public static final Interpolator SCROLL = new Interpolator() {
111         @Override
112         public float getInterpolation(float t) {
113             t -= 1.0f;
114             return t*t*t*t*t + 1;
115         }
116     };
117 
118     public static final Interpolator SCROLL_CUBIC = new Interpolator() {
119         @Override
120         public float getInterpolation(float t) {
121             t -= 1.0f;
122             return t*t*t + 1;
123         }
124     };
125 
126     private static final float FAST_FLING_PX_MS = 10;
127 
scrollInterpolatorForVelocity(float velocity)128     public static Interpolator scrollInterpolatorForVelocity(float velocity) {
129         return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
130     }
131 
132     /**
133      * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms).
134      * @param velocity The start velocity of the animation we want to overshoot.
135      */
overshootInterpolatorForVelocity(float velocity)136     public static Interpolator overshootInterpolatorForVelocity(float velocity) {
137         return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f));
138     }
139 
140     /**
141      * Runs the given interpolator such that the entire progress is set between the given bounds.
142      * That is, we set the interpolation to 0 until lowerBound and reach 1 by upperBound.
143      */
clampToProgress(Interpolator interpolator, float lowerBound, float upperBound)144     public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
145             float upperBound) {
146         if (upperBound <= lowerBound) {
147             throw new IllegalArgumentException("lowerBound must be less than upperBound");
148         }
149         return t -> {
150             if (t < lowerBound) {
151                 return 0;
152             }
153             if (t > upperBound) {
154                 return 1;
155             }
156             return interpolator.getInterpolation((t - lowerBound) / (upperBound - lowerBound));
157         };
158     }
159 
160     /**
161      * Runs the given interpolator such that the interpolated value is mapped to the given range.
162      * This is useful, for example, if we only use this interpolator for part of the animation,
163      * such as to take over a user-controlled animation when they let go.
164      */
165     public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound,
166             float upperBound) {
167         return t -> Utilities.mapRange(interpolator.getInterpolation(t), lowerBound, upperBound);
168     }
169 
170     /**
171      * Computes parameters necessary for an overshoot effect.
172      */
173     public static class OvershootParams {
174         public Interpolator interpolator;
175         public float start;
176         public float end;
177         public long duration;
178 
179         /**
180          * Given the input params, sets OvershootParams variables to be used by the caller.
181          * @param startProgress The progress from 0 to 1 that the overshoot starts from.
182          * @param overshootPastProgress The progress from 0 to 1 where we overshoot past (should
183          *        either be equal to startProgress or endProgress, depending on if we want to
184          *        overshoot immediately or only once we reach the end).
185          * @param endProgress The final progress from 0 to 1 that we will settle to.
186          * @param velocityPxPerMs The initial velocity that causes this overshoot.
187          * @param totalDistancePx The distance against which progress is calculated.
188          */
189         public OvershootParams(float startProgress, float overshootPastProgress,
190                 float endProgress, float velocityPxPerMs, int totalDistancePx) {
191             velocityPxPerMs = Math.abs(velocityPxPerMs);
192             start = startProgress;
193             int startPx = (int) (start * totalDistancePx);
194             // Overshoot by about half a frame.
195             float overshootBy = OVERSHOOT_FACTOR * velocityPxPerMs *
196                     SINGLE_FRAME_MS / totalDistancePx / 2;
197             overshootBy = Utilities.boundToRange(overshootBy, 0.02f, 0.15f);
198             end = overshootPastProgress + overshootBy;
199             int endPx = (int) (end  * totalDistancePx);
200             int overshootDistance = endPx - startPx;
201             // Calculate deceleration necessary to reach overshoot distance.
202             // Formula: velocityFinal^2 = velocityInitial^2 + 2 * acceleration * distance
203             //          0 = v^2 + 2ad (velocityFinal == 0)
204             //          a = v^2 / -2d
205             float decelerationPxPerMs = velocityPxPerMs * velocityPxPerMs / (2 * overshootDistance);
206             // Calculate time necessary to reach peak of overshoot.
207             // Formula: acceleration = velocity / time
208             //          time = velocity / acceleration
209             duration = (long) (velocityPxPerMs / decelerationPxPerMs);
210 
211             // Now that we're at the top of the overshoot, need to settle back to endProgress.
212             float settleDistance = end - endProgress;
213             int settleDistancePx = (int) (settleDistance * totalDistancePx);
214             // Calculate time necessary for the settle.
215             // Formula: distance = velocityInitial * time + 1/2 * acceleration * time^2
216             //          d = 1/2at^2 (velocityInitial = 0, since we just stopped at the top)
217             //          t = sqrt(2d/a)
218             // Above formula assumes constant acceleration. Since we use ACCEL_DEACCEL, we actually
219             // have acceleration to halfway then deceleration the rest. So the formula becomes:
220             //          t = sqrt(d/a) * 2 (half the distance for accel, half for deaccel)
221             long settleDuration = (long) Math.sqrt(settleDistancePx / decelerationPxPerMs) * 4;
222 
223             settleDuration = Math.max(MIN_SETTLE_DURATION, settleDuration);
224             // How much of the animation to devote to playing the overshoot (the rest is for settle).
225             float overshootFraction = (float) duration / (duration + settleDuration);
226             duration += settleDuration;
227             // Finally, create the interpolator, composed of two interpolators: an overshoot, which
228             // reaches end > 1, and then a settle to endProgress.
229             Interpolator overshoot = Interpolators.clampToProgress(DEACCEL, 0, overshootFraction);
230             // The settle starts at 1, where 1 is the top of the overshoot, and maps to a fraction
231             // such that final progress is endProgress. For example, if we overshot to 1.1 but want
232             // to end at 1, we need to map to 1/1.1.
233             Interpolator settle = Interpolators.clampToProgress(Interpolators.mapToProgress(
234                     ACCEL_DEACCEL, 1, (endProgress - start) / (end - start)), overshootFraction, 1);
235             interpolator = t -> t <= overshootFraction
236                     ? overshoot.getInterpolation(t)
237                     : settle.getInterpolation(t);
238         }
239     }
240 }
241