1 /*
2  * Copyright (C) 2006 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.util.AttributeSet;
24 
25 import com.android.internal.R;
26 import com.android.internal.view.animation.HasNativeInterpolator;
27 import com.android.internal.view.animation.NativeInterpolatorFactory;
28 import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
29 
30 /**
31  * An interpolator where the rate of change starts out slowly and
32  * and then accelerates.
33  *
34  */
35 @HasNativeInterpolator
36 public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
37     private final float mFactor;
38     private final double mDoubleFactor;
39 
AccelerateInterpolator()40     public AccelerateInterpolator() {
41         mFactor = 1.0f;
42         mDoubleFactor = 2.0;
43     }
44 
45     /**
46      * Constructor
47      *
48      * @param factor Degree to which the animation should be eased. Seting
49      *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above
50      *        1.0f  exaggerates the ease-in effect (i.e., it starts even
51      *        slower and ends evens faster)
52      */
AccelerateInterpolator(float factor)53     public AccelerateInterpolator(float factor) {
54         mFactor = factor;
55         mDoubleFactor = 2 * mFactor;
56     }
57 
AccelerateInterpolator(Context context, AttributeSet attrs)58     public AccelerateInterpolator(Context context, AttributeSet attrs) {
59         this(context.getResources(), context.getTheme(), attrs);
60     }
61 
62     /** @hide */
AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs)63     public AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
64         TypedArray a;
65         if (theme != null) {
66             a = theme.obtainStyledAttributes(attrs, R.styleable.AccelerateInterpolator, 0, 0);
67         } else {
68             a = res.obtainAttributes(attrs, R.styleable.AccelerateInterpolator);
69         }
70 
71         mFactor = a.getFloat(R.styleable.AccelerateInterpolator_factor, 1.0f);
72         mDoubleFactor = 2 * mFactor;
73         setChangingConfiguration(a.getChangingConfigurations());
74         a.recycle();
75     }
76 
getInterpolation(float input)77     public float getInterpolation(float input) {
78         if (mFactor == 1.0f) {
79             return input * input;
80         } else {
81             return (float)Math.pow(input, mDoubleFactor);
82         }
83     }
84 
85     /** @hide */
86     @Override
createNativeInterpolator()87     public long createNativeInterpolator() {
88         return NativeInterpolatorFactoryHelper.createAccelerateInterpolator(mFactor);
89     }
90 }
91