OvershootInterpolator.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.Resources.Theme;
22import android.content.res.TypedArray;
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 flings forward and overshoots the last value
32 * then comes back.
33 */
34@HasNativeInterpolator
35public class OvershootInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
36    private final float mTension;
37
38    public OvershootInterpolator() {
39        mTension = 2.0f;
40    }
41
42    /**
43     * @param tension Amount of overshoot. When tension equals 0.0f, there is
44     *                no overshoot and the interpolator becomes a simple
45     *                deceleration interpolator.
46     */
47    public OvershootInterpolator(float tension) {
48        mTension = tension;
49    }
50
51    public OvershootInterpolator(Context context, AttributeSet attrs) {
52        this(context.getResources(), context.getTheme(), attrs);
53    }
54
55    /** @hide */
56    public OvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
57        TypedArray a;
58        if (theme != null) {
59            a = theme.obtainStyledAttributes(attrs, R.styleable.OvershootInterpolator, 0, 0);
60        } else {
61            a = res.obtainAttributes(attrs, R.styleable.OvershootInterpolator);
62        }
63
64        mTension = a.getFloat(R.styleable.OvershootInterpolator_tension, 2.0f);
65        setChangingConfiguration(a.getChangingConfigurations());
66        a.recycle();
67    }
68
69    public float getInterpolation(float t) {
70        // _o(t) = t * t * ((tension + 1) * t + tension)
71        // o(t) = _o(t - 1) + 1
72        t -= 1.0f;
73        return t * t * ((mTension + 1) * t + mTension) + 1.0f;
74    }
75
76    /** @hide */
77    @Override
78    public long createNativeInterpolator() {
79        return NativeInterpolatorFactoryHelper.createOvershootInterpolator(mTension);
80    }
81}
82