AnticipateInterpolator.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.TypedArray; 22import android.content.res.Resources.Theme; 23import android.util.AttributeSet; 24 25import com.android.internal.R; 26import com.android.internal.view.animation.HasNativeInterpolator; 27import com.android.internal.view.animation.NativeInterpolatorFactory; 28import com.android.internal.view.animation.NativeInterpolatorFactoryHelper; 29 30/** 31 * An interpolator where the change starts backward then flings forward. 32 */ 33@HasNativeInterpolator 34public class AnticipateInterpolator implements Interpolator, NativeInterpolatorFactory { 35 private final float mTension; 36 37 public AnticipateInterpolator() { 38 mTension = 2.0f; 39 } 40 41 /** 42 * @param tension Amount of anticipation. When tension equals 0.0f, there is 43 * no anticipation and the interpolator becomes a simple 44 * acceleration interpolator. 45 */ 46 public AnticipateInterpolator(float tension) { 47 mTension = tension; 48 } 49 50 public AnticipateInterpolator(Context context, AttributeSet attrs) { 51 this(context.getResources(), context.getTheme(), attrs); 52 } 53 54 /** @hide */ 55 public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) { 56 TypedArray a; 57 if (theme != null) { 58 a = theme.obtainStyledAttributes(attrs, R.styleable.AnticipateInterpolator, 0, 0); 59 } else { 60 a = res.obtainAttributes(attrs, R.styleable.AnticipateInterpolator); 61 } 62 63 mTension = 64 a.getFloat(R.styleable.AnticipateInterpolator_tension, 2.0f); 65 66 a.recycle(); 67 } 68 69 public float getInterpolation(float t) { 70 // a(t) = t * t * ((tension + 1) * t - tension) 71 return t * t * ((mTension + 1) * t - mTension); 72 } 73 74 /** @hide */ 75 @Override 76 public long createNativeInterpolator() { 77 return NativeInterpolatorFactoryHelper.createAnticipateInterpolator(mTension); 78 } 79} 80