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;
218b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.content.res.TypedArray;
22e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghuiimport android.content.res.Resources.Theme;
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 starts backward then flings forward.
328b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
33c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck@HasNativeInterpolator
34d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyarpublic class AnticipateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
358b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private final float mTension;
368b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
378b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateInterpolator() {
388b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = 2.0f;
398b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    /**
428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param tension Amount of anticipation. When tension equals 0.0f, there is
438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                no anticipation and the interpolator becomes a simple
448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                acceleration interpolator.
458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     */
468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateInterpolator(float tension) {
478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = tension;
488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateInterpolator(Context context, AttributeSet attrs) {
51e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        this(context.getResources(), context.getTheme(), attrs);
52e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    }
53e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
54e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    /** @hide */
55e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
56e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        TypedArray a;
57e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        if (theme != null) {
58e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = theme.obtainStyledAttributes(attrs, R.styleable.AnticipateInterpolator, 0, 0);
59e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        } else {
60e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = res.obtainAttributes(attrs, R.styleable.AnticipateInterpolator);
61e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
628b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
63d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar        mTension = a.getFloat(R.styleable.AnticipateInterpolator_tension, 2.0f);
64d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar        setChangingConfiguration(a.getChangingConfigurations());
658b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        a.recycle();
668b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
678b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
688b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public float getInterpolation(float t) {
698b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // a(t) = t * t * ((tension + 1) * t - tension)
708b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * ((mTension + 1) * t - mTension);
718b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
72c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
73c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    /** @hide */
74c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    @Override
75c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    public long createNativeInterpolator() {
76c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck        return NativeInterpolatorFactoryHelper.createAnticipateInterpolator(mTension);
77c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    }
788b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru}
79