AnticipateOvershootInterpolator.java revision e5e92602a41a4ddc7b42cd1c171a0edfbd09b8da
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.view.animation.HasNativeInterpolator; 26import com.android.internal.view.animation.NativeInterpolatorFactory; 27import com.android.internal.view.animation.NativeInterpolatorFactoryHelper; 28 29import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension; 30import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension; 31import static com.android.internal.R.styleable.AnticipateOvershootInterpolator; 32 33/** 34 * An interpolator where the change starts backward then flings forward and overshoots 35 * the target value and finally goes back to the final value. 36 */ 37@HasNativeInterpolator 38public class AnticipateOvershootInterpolator implements Interpolator, NativeInterpolatorFactory { 39 private final float mTension; 40 41 public AnticipateOvershootInterpolator() { 42 mTension = 2.0f * 1.5f; 43 } 44 45 /** 46 * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 47 * there is no anticipation/overshoot and the interpolator becomes 48 * a simple acceleration/deceleration interpolator. 49 */ 50 public AnticipateOvershootInterpolator(float tension) { 51 mTension = tension * 1.5f; 52 } 53 54 /** 55 * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 56 * there is no anticipation/overshoot and the interpolator becomes 57 * a simple acceleration/deceleration interpolator. 58 * @param extraTension Amount by which to multiply the tension. For instance, 59 * to get the same overshoot as an OvershootInterpolator with 60 * a tension of 2.0f, you would use an extraTension of 1.5f. 61 */ 62 public AnticipateOvershootInterpolator(float tension, float extraTension) { 63 mTension = tension * extraTension; 64 } 65 66 public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) { 67 this(context.getResources(), context.getTheme(), attrs); 68 } 69 70 /** @hide */ 71 public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) { 72 TypedArray a; 73 if (theme != null) { 74 a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0); 75 } else { 76 a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator); 77 } 78 79 mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) * 80 a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f); 81 82 a.recycle(); 83 } 84 85 private static float a(float t, float s) { 86 return t * t * ((s + 1) * t - s); 87 } 88 89 private static float o(float t, float s) { 90 return t * t * ((s + 1) * t + s); 91 } 92 93 public float getInterpolation(float t) { 94 // a(t, s) = t * t * ((s + 1) * t - s) 95 // o(t, s) = t * t * ((s + 1) * t + s) 96 // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5 97 // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0 98 if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension); 99 else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f); 100 } 101 102 /** @hide */ 103 @Override 104 public long createNativeInterpolator() { 105 return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension); 106 } 107} 108