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 android.content.Context;
20 import android.content.res.Resources;
21 import android.content.res.Resources.Theme;
22 import android.content.res.TypedArray;
23 import android.graphics.animation.HasNativeInterpolator;
24 import android.graphics.animation.NativeInterpolator;
25 import android.graphics.animation.NativeInterpolatorFactory;
26 import android.util.AttributeSet;
27 
28 import com.android.internal.R;
29 
30 /**
31  * An interpolator where the change starts backward then flings forward.
32  */
33 @HasNativeInterpolator
34 public class AnticipateInterpolator extends BaseInterpolator implements NativeInterpolator {
35     private final float mTension;
36 
AnticipateInterpolator()37     public AnticipateInterpolator() {
38         mTension = 2.0f;
39     }
40 
41     /**
42      * @param tension Amount of anticipation. When tension equals 0.0f, there is
43      *                no anticipation and the interpolator becomes a simple
44      *                acceleration interpolator.
45      */
AnticipateInterpolator(float tension)46     public AnticipateInterpolator(float tension) {
47         mTension = tension;
48     }
49 
AnticipateInterpolator(Context context, AttributeSet attrs)50     public AnticipateInterpolator(Context context, AttributeSet attrs) {
51         this(context.getResources(), context.getTheme(), attrs);
52     }
53 
54     /** @hide */
AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs)55     public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
56         TypedArray a;
57         if (theme != null) {
58             a = theme.obtainStyledAttributes(attrs, R.styleable.AnticipateInterpolator, 0, 0);
59         } else {
60             a = res.obtainAttributes(attrs, R.styleable.AnticipateInterpolator);
61         }
62 
63         mTension = a.getFloat(R.styleable.AnticipateInterpolator_tension, 2.0f);
64         setChangingConfiguration(a.getChangingConfigurations());
65         a.recycle();
66     }
67 
getInterpolation(float t)68     public float getInterpolation(float t) {
69         // a(t) = t * t * ((tension + 1) * t - tension)
70         return t * t * ((mTension + 1) * t - mTension);
71     }
72 
73     /** @hide */
74     @Override
createNativeInterpolator()75     public long createNativeInterpolator() {
76         return NativeInterpolatorFactory.createAnticipateInterpolator(mTension);
77     }
78 }
79