1 /*
2  * Copyright (C) 2007 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 quickly and
32  * and then decelerates.
33  *
34  */
35 @HasNativeInterpolator
36 public class DecelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
DecelerateInterpolator()37     public DecelerateInterpolator() {
38     }
39 
40     /**
41      * Constructor
42      *
43      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
44      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f exaggerates the
45      *        ease-out effect (i.e., it starts even faster and ends evens slower).
46      */
DecelerateInterpolator(float factor)47     public DecelerateInterpolator(float factor) {
48         mFactor = factor;
49     }
50 
DecelerateInterpolator(Context context, AttributeSet attrs)51     public DecelerateInterpolator(Context context, AttributeSet attrs) {
52         this(context.getResources(), context.getTheme(), attrs);
53     }
54 
55     /** @hide */
DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs)56     public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
57         TypedArray a;
58         if (theme != null) {
59             a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);
60         } else {
61             a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);
62         }
63 
64         mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);
65         setChangingConfiguration(a.getChangingConfigurations());
66         a.recycle();
67     }
68 
getInterpolation(float input)69     public float getInterpolation(float input) {
70         float result;
71         if (mFactor == 1.0f) {
72             result = (float)(1.0f - (1.0f - input) * (1.0f - input));
73         } else {
74             result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
75         }
76         return result;
77     }
78 
79     private float mFactor = 1.0f;
80 
81     /** @hide */
82     @Override
createNativeInterpolator()83     public long createNativeInterpolator() {
84         return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
85     }
86 }
87