AccelerateInterpolator.java revision c8ac775659fd252ce2cc9a61837c170ff70f0a1a
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
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 slowly and
29 * and then accelerates.
30 *
31 */
32@HasNativeInterpolator
33public class AccelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
34    private final float mFactor;
35    private final double mDoubleFactor;
36
37    public AccelerateInterpolator() {
38        mFactor = 1.0f;
39        mDoubleFactor = 2.0;
40    }
41
42    /**
43     * Constructor
44     *
45     * @param factor Degree to which the animation should be eased. Seting
46     *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above
47     *        1.0f  exaggerates the ease-in effect (i.e., it starts even
48     *        slower and ends evens faster)
49     */
50    public AccelerateInterpolator(float factor) {
51        mFactor = factor;
52        mDoubleFactor = 2 * mFactor;
53    }
54
55    public AccelerateInterpolator(Context context, AttributeSet attrs) {
56        TypedArray a =
57            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AccelerateInterpolator);
58
59        mFactor = a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor, 1.0f);
60        mDoubleFactor = 2 * mFactor;
61
62        a.recycle();
63    }
64
65    public float getInterpolation(float input) {
66        if (mFactor == 1.0f) {
67            return input * input;
68        } else {
69            return (float)Math.pow(input, mDoubleFactor);
70        }
71    }
72
73    /** @hide */
74    @Override
75    public long createNativeInterpolator() {
76        return NativeInterpolatorFactoryHelper.createAccelerateInterpolator(mFactor);
77    }
78}
79