Home | History | Annotate | Download | only in anim
      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.AccelerateInterpolator;
     21 import android.view.animation.DecelerateInterpolator;
     22 import android.view.animation.Interpolator;
     23 import android.view.animation.LinearInterpolator;
     24 import android.view.animation.OvershootInterpolator;
     25 import android.view.animation.PathInterpolator;
     26 
     27 
     28 /**
     29  * Common interpolators used in Launcher
     30  */
     31 public class Interpolators {
     32 
     33     public static final Interpolator LINEAR = new LinearInterpolator();
     34 
     35     public static final Interpolator ACCEL = new AccelerateInterpolator();
     36     public static final Interpolator ACCEL_1_5 = new AccelerateInterpolator(1.5f);
     37     public static final Interpolator ACCEL_2 = new AccelerateInterpolator(2);
     38 
     39     public static final Interpolator DEACCEL = new DecelerateInterpolator();
     40     public static final Interpolator DEACCEL_1_5 = new DecelerateInterpolator(1.5f);
     41     public static final Interpolator DEACCEL_1_7 = new DecelerateInterpolator(1.7f);
     42     public static final Interpolator DEACCEL_2 = new DecelerateInterpolator(2);
     43     public static final Interpolator DEACCEL_2_5 = new DecelerateInterpolator(2.5f);
     44     public static final Interpolator DEACCEL_3 = new DecelerateInterpolator(3f);
     45 
     46     public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
     47 
     48     public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
     49     public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);
     50 
     51     public static final Interpolator EXAGGERATED_EASE;
     52 
     53     static {
     54         Path exaggeratedEase = new Path();
     55         exaggeratedEase.moveTo(0, 0);
     56         exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
     57         exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
     58         EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
     59     }
     60 
     61     public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
     62 
     63     public static final Interpolator TOUCH_RESPONSE_INTERPOLATOR =
     64             new PathInterpolator(0.3f, 0f, 0.1f, 1f);
     65 
     66     /**
     67      * Inversion of ZOOM_OUT, compounded with an ease-out.
     68      */
     69     public static final Interpolator ZOOM_IN = new Interpolator() {
     70         @Override
     71         public float getInterpolation(float v) {
     72             return DEACCEL_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
     73         }
     74     };
     75 
     76     public static final Interpolator ZOOM_OUT = new Interpolator() {
     77 
     78         private static final float FOCAL_LENGTH = 0.35f;
     79 
     80         @Override
     81         public float getInterpolation(float v) {
     82             return zInterpolate(v);
     83         }
     84 
     85         /**
     86          * This interpolator emulates the rate at which the perceived scale of an object changes
     87          * as its distance from a camera increases. When this interpolator is applied to a scale
     88          * animation on a view, it evokes the sense that the object is shrinking due to moving away
     89          * from the camera.
     90          */
     91         private float zInterpolate(float input) {
     92             return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
     93                     (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
     94         }
     95     };
     96 
     97     public static final Interpolator SCROLL = new Interpolator() {
     98         @Override
     99         public float getInterpolation(float t) {
    100             t -= 1.0f;
    101             return t*t*t*t*t + 1;
    102         }
    103     };
    104 
    105     public static final Interpolator SCROLL_CUBIC = new Interpolator() {
    106         @Override
    107         public float getInterpolation(float t) {
    108             t -= 1.0f;
    109             return t*t*t + 1;
    110         }
    111     };
    112 
    113     private static final float FAST_FLING_PX_MS = 10;
    114 
    115     public static Interpolator scrollInterpolatorForVelocity(float velocity) {
    116         return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
    117     }
    118 
    119     /**
    120      * Runs the given interpolator such that the entire progress is set between the given bounds.
    121      * That is, we set the interpolation to 0 until lowerBound and reach 1 by upperBound.
    122      */
    123     public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
    124             float upperBound) {
    125         if (upperBound <= lowerBound) {
    126             throw new IllegalArgumentException("lowerBound must be less than upperBound");
    127         }
    128         return t -> {
    129             if (t < lowerBound) {
    130                 return 0;
    131             }
    132             if (t > upperBound) {
    133                 return 1;
    134             }
    135             return interpolator.getInterpolation((t - lowerBound) / (upperBound - lowerBound));
    136         };
    137     }
    138 }