AnticipateInterpolator.java revision d422dc358f0100106dc07d7b903201eb9b043b11
1/*
2 * Copyright (C) 2009 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 change starts backward then flings forward.
32 */
33@HasNativeInterpolator
34public class AnticipateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
35    private final float mTension;
36
37    public AnticipateInterpolator() {
38        mTension = 2.0f;
39    }
40
41    /**
42     * @param tension Amount of anticipation. When tension equals 0.0f, there is
43     *                no anticipation and the interpolator becomes a simple
44     *                acceleration interpolator.
45     */
46    public AnticipateInterpolator(float tension) {
47        mTension = tension;
48    }
49
50    public AnticipateInterpolator(Context context, AttributeSet attrs) {
51        this(context.getResources(), context.getTheme(), attrs);
52    }
53
54    /** @hide */
55    public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
56        TypedArray a;
57        if (theme != null) {
58            a = theme.obtainStyledAttributes(attrs, R.styleable.AnticipateInterpolator, 0, 0);
59        } else {
60            a = res.obtainAttributes(attrs, R.styleable.AnticipateInterpolator);
61        }
62
63        mTension = a.getFloat(R.styleable.AnticipateInterpolator_tension, 2.0f);
64        setChangingConfiguration(a.getChangingConfigurations());
65        a.recycle();
66    }
67
68    public float getInterpolation(float t) {
69        // a(t) = t * t * ((tension + 1) * t - tension)
70        return t * t * ((mTension + 1) * t - mTension);
71    }
72
73    /** @hide */
74    @Override
75    public long createNativeInterpolator() {
76        return NativeInterpolatorFactoryHelper.createAnticipateInterpolator(mTension);
77    }
78}
79