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;
24c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
25c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.HasNativeInterpolator;
26c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactory;
27c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
28c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
298b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension;
308b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension;
318b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport static com.android.internal.R.styleable.AnticipateOvershootInterpolator;
328b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
338b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/**
348b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * An interpolator where the change starts backward then flings forward and overshoots
358b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * the target value and finally goes back to the final value.
368b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
37c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck@HasNativeInterpolator
38c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckpublic class AnticipateOvershootInterpolator implements Interpolator, NativeInterpolatorFactory {
398b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private final float mTension;
408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateOvershootInterpolator() {
428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = 2.0f * 1.5f;
438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    /**
468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                there is no anticipation/overshoot and the interpolator becomes
488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                a simple acceleration/deceleration interpolator.
498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     */
508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateOvershootInterpolator(float tension) {
518b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = tension * 1.5f;
528b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
538b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
548b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    /**
558b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
568b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                there is no anticipation/overshoot and the interpolator becomes
578b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                a simple acceleration/deceleration interpolator.
588b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param extraTension Amount by which to multiply the tension. For instance,
598b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                     to get the same overshoot as an OvershootInterpolator with
608b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                     a tension of 2.0f, you would use an extraTension of 1.5f.
618b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     */
628b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateOvershootInterpolator(float tension, float extraTension) {
638b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = tension * extraTension;
648b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
658b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
668b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
67e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        this(context.getResources(), context.getTheme(), attrs);
68e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    }
69e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
70e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    /** @hide */
71e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
72e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        TypedArray a;
73e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        if (theme != null) {
74e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0);
75e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        } else {
76e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator);
77e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
788b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
798b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
808b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru                a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
818b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
828b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        a.recycle();
838b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
848b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
858b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private static float a(float t, float s) {
868b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * ((s + 1) * t - s);
878b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
888b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
898b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private static float o(float t, float s) {
908b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * ((s + 1) * t + s);
918b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
928b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
938b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public float getInterpolation(float t) {
948b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // a(t, s) = t * t * ((s + 1) * t - s)
958b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // o(t, s) = t * t * ((s + 1) * t + s)
968b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
978b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
988b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
998b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
1008b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
101c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
102c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    /** @hide */
103c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    @Override
104c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    public long createNativeInterpolator() {
105c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck        return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension);
106c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    }
1078b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru}
108