1 /*
2  * Copyright (C) 2015 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.volume;
18 
19 import android.animation.TimeInterpolator;
20 
21 public class SystemUIInterpolators {
22     public static final class LogDecelerateInterpolator implements TimeInterpolator {
23         private final float mBase;
24         private final float mDrift;
25         private final float mTimeScale;
26         private final float mOutputScale;
27 
LogDecelerateInterpolator()28         public LogDecelerateInterpolator() {
29             this(400f, 1.4f, 0);
30         }
31 
LogDecelerateInterpolator(float base, float timeScale, float drift)32         private LogDecelerateInterpolator(float base, float timeScale, float drift) {
33             mBase = base;
34             mDrift = drift;
35             mTimeScale = 1f / timeScale;
36 
37             mOutputScale = 1f / computeLog(1f);
38         }
39 
computeLog(float t)40         private float computeLog(float t) {
41             return 1f - (float) Math.pow(mBase, -t * mTimeScale) + (mDrift * t);
42         }
43 
44         @Override
getInterpolation(float t)45         public float getInterpolation(float t) {
46             return computeLog(t) * mOutputScale;
47         }
48     }
49 
50     public static final class LogAccelerateInterpolator implements TimeInterpolator {
51         private final int mBase;
52         private final int mDrift;
53         private final float mLogScale;
54 
LogAccelerateInterpolator()55         public LogAccelerateInterpolator() {
56             this(100, 0);
57         }
58 
LogAccelerateInterpolator(int base, int drift)59         private LogAccelerateInterpolator(int base, int drift) {
60             mBase = base;
61             mDrift = drift;
62             mLogScale = 1f / computeLog(1, mBase, mDrift);
63         }
64 
computeLog(float t, int base, int drift)65         private static float computeLog(float t, int base, int drift) {
66             return (float) -Math.pow(base, -t) + 1 + (drift * t);
67         }
68 
69         @Override
getInterpolation(float t)70         public float getInterpolation(float t) {
71             return 1 - computeLog(1 - t, mBase, mDrift) * mLogScale;
72         }
73     }
74 
75     public interface Callback {
onAnimatingChanged(boolean animating)76         void onAnimatingChanged(boolean animating);
77     }
78 }
79