18b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/*
28b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Copyright (C) 2009 The Android Open Source Project
38b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
48b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Licensed under the Apache License, Version 2.0 (the "License");
58b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * you may not use this file except in compliance with the License.
68b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * You may obtain a copy of the License at
78b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
88b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *      http://www.apache.org/licenses/LICENSE-2.0
98b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
108b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Unless required by applicable law or agreed to in writing, software
118b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * distributed under the License is distributed on an "AS IS" BASIS,
128b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * See the License for the specific language governing permissions and
148b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * limitations under the License.
158b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
168b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
178b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Querupackage android.view.animation;
188b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
198b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.content.Context;
20e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghuiimport android.content.res.Resources;
21e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghuiimport android.content.res.Resources.Theme;
228b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.content.res.TypedArray;
238b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.util.AttributeSet;
248b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
25e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghuiimport com.android.internal.R;
26c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.HasNativeInterpolator;
27c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactory;
28c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
29c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
308b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/**
318b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * An interpolator where the change flings forward and overshoots the last value
328b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * then comes back.
338b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
34c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck@HasNativeInterpolator
35c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckpublic class OvershootInterpolator implements Interpolator, NativeInterpolatorFactory {
368b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private final float mTension;
378b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
388b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator() {
398b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = 2.0f;
408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    /**
438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param tension Amount of overshoot. When tension equals 0.0f, there is
448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                no overshoot and the interpolator becomes a simple
458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                deceleration interpolator.
468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     */
478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator(float tension) {
488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = tension;
498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
518b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator(Context context, AttributeSet attrs) {
52e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        this(context.getResources(), context.getTheme(), attrs);
53e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    }
54e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
55e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    /** @hide */
56e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    public OvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
57e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        TypedArray a;
58e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        if (theme != null) {
59e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = theme.obtainStyledAttributes(attrs, R.styleable.OvershootInterpolator, 0, 0);
60e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        } else {
61e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = res.obtainAttributes(attrs, R.styleable.OvershootInterpolator);
62e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
638b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
648b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension =
65e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                a.getFloat(R.styleable.OvershootInterpolator_tension, 2.0f);
668b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
678b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        a.recycle();
688b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
698b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
708b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public float getInterpolation(float t) {
718b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // _o(t) = t * t * ((tension + 1) * t + tension)
728b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // o(t) = _o(t - 1) + 1
738b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        t -= 1.0f;
748b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * ((mTension + 1) * t + mTension) + 1.0f;
758b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
76c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
77c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    /** @hide */
78c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    @Override
79c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    public long createNativeInterpolator() {
80c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck        return NativeInterpolatorFactoryHelper.createOvershootInterpolator(mTension);
81c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    }
828b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru}
83