DecelerateInterpolator.java revision c8ac775659fd252ce2cc9a61837c170ff70f0a1a
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.TypedArray;
21import android.util.AttributeSet;
22
23import com.android.internal.view.animation.HasNativeInterpolator;
24import com.android.internal.view.animation.NativeInterpolatorFactory;
25import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
26
27/**
28 * An interpolator where the rate of change starts out quickly and
29 * and then decelerates.
30 *
31 */
32@HasNativeInterpolator
33public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
34    public DecelerateInterpolator() {
35    }
36
37    /**
38     * Constructor
39     *
40     * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
41     *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
42     *        ease-out effect (i.e., it starts even faster and ends evens slower)
43     */
44    public DecelerateInterpolator(float factor) {
45        mFactor = factor;
46    }
47
48    public DecelerateInterpolator(Context context, AttributeSet attrs) {
49        TypedArray a =
50            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator);
51
52        mFactor = a.getFloat(com.android.internal.R.styleable.DecelerateInterpolator_factor, 1.0f);
53
54        a.recycle();
55    }
56
57    public float getInterpolation(float input) {
58        float result;
59        if (mFactor == 1.0f) {
60            result = (float)(1.0f - (1.0f - input) * (1.0f - input));
61        } else {
62            result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
63        }
64        return result;
65    }
66
67    private float mFactor = 1.0f;
68
69    /** @hide */
70    @Override
71    public long createNativeInterpolator() {
72        return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
73    }
74}
75