1 /*
2  * Copyright (C) 2009 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 android.view.animation;
18 
19 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator;
20 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension;
21 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension;
22 
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.content.res.Resources.Theme;
26 import android.content.res.TypedArray;
27 import android.graphics.animation.HasNativeInterpolator;
28 import android.graphics.animation.NativeInterpolator;
29 import android.graphics.animation.NativeInterpolatorFactory;
30 import android.util.AttributeSet;
31 
32 
33 /**
34  * An interpolator where the change starts backward then flings forward and overshoots
35  * the target value and finally goes back to the final value.
36  */
37 @HasNativeInterpolator
38 public class AnticipateOvershootInterpolator extends BaseInterpolator
39         implements NativeInterpolator {
40     private final float mTension;
41 
AnticipateOvershootInterpolator()42     public AnticipateOvershootInterpolator() {
43         mTension = 2.0f * 1.5f;
44     }
45 
46     /**
47      * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
48      *                there is no anticipation/overshoot and the interpolator becomes
49      *                a simple acceleration/deceleration interpolator.
50      */
AnticipateOvershootInterpolator(float tension)51     public AnticipateOvershootInterpolator(float tension) {
52         mTension = tension * 1.5f;
53     }
54 
55     /**
56      * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
57      *                there is no anticipation/overshoot and the interpolator becomes
58      *                a simple acceleration/deceleration interpolator.
59      * @param extraTension Amount by which to multiply the tension. For instance,
60      *                     to get the same overshoot as an OvershootInterpolator with
61      *                     a tension of 2.0f, you would use an extraTension of 1.5f.
62      */
AnticipateOvershootInterpolator(float tension, float extraTension)63     public AnticipateOvershootInterpolator(float tension, float extraTension) {
64         mTension = tension * extraTension;
65     }
66 
AnticipateOvershootInterpolator(Context context, AttributeSet attrs)67     public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
68         this(context.getResources(), context.getTheme(), attrs);
69     }
70 
71     /** @hide */
AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs)72     public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
73         TypedArray a;
74         if (theme != null) {
75             a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0);
76         } else {
77             a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator);
78         }
79 
80         mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
81                 a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
82         setChangingConfiguration(a.getChangingConfigurations());
83         a.recycle();
84     }
85 
a(float t, float s)86     private static float a(float t, float s) {
87         return t * t * ((s + 1) * t - s);
88     }
89 
o(float t, float s)90     private static float o(float t, float s) {
91         return t * t * ((s + 1) * t + s);
92     }
93 
getInterpolation(float t)94     public float getInterpolation(float t) {
95         // a(t, s) = t * t * ((s + 1) * t - s)
96         // o(t, s) = t * t * ((s + 1) * t + s)
97         // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
98         // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
99         if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
100         else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
101     }
102 
103     /** @hide */
104     @Override
createNativeInterpolator()105     public long createNativeInterpolator() {
106         return NativeInterpolatorFactory.createAnticipateOvershootInterpolator(mTension);
107     }
108 }
109