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 public static final Interpolator PREDICTIVE_BACK_DECELERATED_EASE = 60 new PathInterpolator(0, 0, 0, 1f); 61 62 /** 63 * The default emphasized interpolator. Used for hero / emphasized movement of content. 64 */ 65 public static final Interpolator EMPHASIZED = createEmphasizedInterpolator(); 66 public static final Interpolator EMPHASIZED_ACCELERATE = new PathInterpolator( 67 0.3f, 0f, 0.8f, 0.15f); 68 public static final Interpolator EMPHASIZED_DECELERATE = new PathInterpolator( 69 0.05f, 0.7f, 0.1f, 1f); 70 71 public static final Interpolator EXAGGERATED_EASE; 72 73 public static final Interpolator INSTANT = t -> 1; 74 /** 75 * All values of t map to 0 until t == 1. This is primarily useful for setting view visibility, 76 * which should only happen at the very end of the animation (when it's already hidden). 77 */ 78 public static final Interpolator FINAL_FRAME = t -> t < 1 ? 0 : 1; 79 80 static { 81 Path exaggeratedEase = new Path(); 82 exaggeratedEase.moveTo(0, 0); 83 exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f); 84 exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f); 85 EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase); 86 } 87 88 public static final Interpolator OVERSHOOT_0_75 = new OvershootInterpolator(0.75f); 89 public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f); 90 public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f); 91 92 public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR = 93 new PathInterpolator(0.3f, 0f, 0.1f, 1f); 94 public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR_ACCEL_DEACCEL = 95 v -> ACCEL_DEACCEL.getInterpolation(TOUCH_RESPONSE_INTERPOLATOR.getInterpolation(v)); 96 97 /** 98 * Inversion of ZOOM_OUT, compounded with an ease-out. 99 */ 100 public static final Interpolator ZOOM_IN = new Interpolator() { 101 @Override 102 public float getInterpolation(float v) { 103 return DEACCEL_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v)); 104 } 105 }; 106 107 public static final Interpolator ZOOM_OUT = new Interpolator() { 108 109 private static final float FOCAL_LENGTH = 0.35f; 110 111 @Override 112 public float getInterpolation(float v) { 113 return zInterpolate(v); 114 } 115 116 /** 117 * This interpolator emulates the rate at which the perceived scale of an object changes 118 * as its distance from a camera increases. When this interpolator is applied to a scale 119 * animation on a view, it evokes the sense that the object is shrinking due to moving away 120 * from the camera. 121 */ 122 private float zInterpolate(float input) { 123 return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) / 124 (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f)); 125 } 126 }; 127 128 public static final Interpolator SCROLL = new Interpolator() { 129 @Override 130 public float getInterpolation(float t) { 131 t -= 1.0f; 132 return t*t*t*t*t + 1; 133 } 134 }; 135 136 public static final Interpolator SCROLL_CUBIC = new Interpolator() { 137 @Override 138 public float getInterpolation(float t) { 139 t -= 1.0f; 140 return t*t*t + 1; 141 } 142 }; 143 144 private static final float FAST_FLING_PX_MS = 10; 145 scrollInterpolatorForVelocity(float velocity)146 public static Interpolator scrollInterpolatorForVelocity(float velocity) { 147 return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC; 148 } 149 150 /** 151 * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms). 152 * @param velocity The start velocity of the animation we want to overshoot. 153 */ overshootInterpolatorForVelocity(float velocity)154 public static Interpolator overshootInterpolatorForVelocity(float velocity) { 155 return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f)); 156 } 157 158 /** 159 * Returns a function that runs the given interpolator such that the entire progress is set 160 * between the given bounds. That is, we set the interpolation to 0 until lowerBound and reach 161 * 1 by upperBound. 162 */ clampToProgress(Interpolator interpolator, float lowerBound, float upperBound)163 public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound, 164 float upperBound) { 165 if (upperBound < lowerBound) { 166 throw new IllegalArgumentException( 167 String.format("upperBound (%f) must be greater than lowerBound (%f)", 168 upperBound, lowerBound)); 169 } 170 return t -> clampToProgress(interpolator, t, lowerBound, upperBound); 171 } 172 173 /** 174 * Returns the progress value's progress between the lower and upper bounds. That is, the 175 * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound. 176 * 177 * Between lowerBound and upperBound, the progress value will be interpolated using the provided 178 * interpolator. 179 */ clampToProgress( Interpolator interpolator, float progress, float lowerBound, float upperBound)180 public static float clampToProgress( 181 Interpolator interpolator, float progress, float lowerBound, float upperBound) { 182 if (upperBound < lowerBound) { 183 throw new IllegalArgumentException( 184 String.format("upperBound (%f) must be greater than lowerBound (%f)", 185 upperBound, lowerBound)); 186 } 187 188 if (progress == lowerBound && progress == upperBound) { 189 return progress == 0f ? 0 : 1; 190 } 191 if (progress < lowerBound) { 192 return 0; 193 } 194 if (progress > upperBound) { 195 return 1; 196 } 197 return interpolator.getInterpolation((progress - lowerBound) / (upperBound - lowerBound)); 198 } 199 200 /** 201 * Returns the progress value's progress between the lower and upper bounds. That is, the 202 * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound. 203 */ clampToProgress(float progress, float lowerBound, float upperBound)204 public static float clampToProgress(float progress, float lowerBound, float upperBound) { 205 return clampToProgress(Interpolators.LINEAR, progress, lowerBound, upperBound); 206 } 207 208 /** 209 * Runs the given interpolator such that the interpolated value is mapped to the given range. 210 * This is useful, for example, if we only use this interpolator for part of the animation, 211 * such as to take over a user-controlled animation when they let go. 212 */ mapToProgress(Interpolator interpolator, float lowerBound, float upperBound)213 public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound, 214 float upperBound) { 215 return t -> Utilities.mapRange(interpolator.getInterpolation(t), lowerBound, upperBound); 216 } 217 218 /** 219 * Returns the reverse of the provided interpolator, following the formula: g(x) = 1 - f(1 - x). 220 * In practice, this means that if f is an interpolator used to model a value animating between 221 * m and n, g is the interpolator to use to obtain the specular behavior when animating from n 222 * to m. 223 */ reverse(Interpolator interpolator)224 public static Interpolator reverse(Interpolator interpolator) { 225 return t -> 1 - interpolator.getInterpolation(1 - t); 226 } 227 228 // Create the default emphasized interpolator createEmphasizedInterpolator()229 private static PathInterpolator createEmphasizedInterpolator() { 230 Path path = new Path(); 231 // Doing the same as fast_out_extra_slow_in 232 path.moveTo(0f, 0f); 233 path.cubicTo(0.05f, 0f, 0.133333f, 0.06f, 0.166666f, 0.4f); 234 path.cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f); 235 return new PathInterpolator(path); 236 } 237 } 238