DecelerateInterpolator.java revision d422dc358f0100106dc07d7b903201eb9b043b11
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
17package android.view.animation;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.content.res.Resources.Theme;
23import android.util.AttributeSet;
24
25import com.android.internal.R;
26import com.android.internal.view.animation.HasNativeInterpolator;
27import com.android.internal.view.animation.NativeInterpolatorFactory;
28import 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
36public class DecelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
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 makes exaggerates the
45     *        ease-out effect (i.e., it starts even faster and ends evens slower)
46     */
47    public DecelerateInterpolator(float factor) {
48        mFactor = factor;
49    }
50
51    public DecelerateInterpolator(Context context, AttributeSet attrs) {
52        this(context.getResources(), context.getTheme(), attrs);
53    }
54
55    /** @hide */
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
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
83    public long createNativeInterpolator() {
84        return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
85    }
86}
87