2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.view.animation;
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension;
23 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension;
24 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator;
27 * An interpolator where the change starts backward then flings forward and overshoots
28 * the target value and finally goes back to the final value.
30 public class AnticipateOvershootInterpolator implements Interpolator {
31 private final float mTension;
33 public AnticipateOvershootInterpolator() {
34 mTension = 2.0f * 1.5f;
38 * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
39 * there is no anticipation/overshoot and the interpolator becomes
40 * a simple acceleration/deceleration interpolator.
42 public AnticipateOvershootInterpolator(float tension) {
43 mTension = tension * 1.5f;
47 * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
48 * there is no anticipation/overshoot and the interpolator becomes
49 * a simple acceleration/deceleration interpolator.
50 * @param extraTension Amount by which to multiply the tension. For instance,
51 * to get the same overshoot as an OvershootInterpolator with
52 * a tension of 2.0f, you would use an extraTension of 1.5f.
54 public AnticipateOvershootInterpolator(float tension, float extraTension) {
55 mTension = tension * extraTension;
58 public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
59 TypedArray a = context.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator);
61 mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
62 a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
67 private static float a(float t, float s) {
68 return t * t * ((s + 1) * t - s);
71 private static float o(float t, float s) {
72 return t * t * ((s + 1) * t + s);
75 public float getInterpolation(float t) {
76 // a(t, s) = t * t * ((s + 1) * t - s)
77 // o(t, s) = t * t * ((s + 1) * t + s)
78 // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
79 // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
80 if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
81 else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);