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 38d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyarpublic class AnticipateOvershootInterpolator extends BaseInterpolator 39d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar implements NativeInterpolatorFactory { 408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru private final float mTension; 418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru public AnticipateOvershootInterpolator() { 438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru mTension = 2.0f * 1.5f; 448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru /** 478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * there is no anticipation/overshoot and the interpolator becomes 498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * a simple acceleration/deceleration interpolator. 508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */ 518b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru public AnticipateOvershootInterpolator(float tension) { 528b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru mTension = tension * 1.5f; 538b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 548b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 558b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru /** 568b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 578b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * there is no anticipation/overshoot and the interpolator becomes 588b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * a simple acceleration/deceleration interpolator. 598b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * @param extraTension Amount by which to multiply the tension. For instance, 608b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * to get the same overshoot as an OvershootInterpolator with 618b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * a tension of 2.0f, you would use an extraTension of 1.5f. 628b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */ 638b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru public AnticipateOvershootInterpolator(float tension, float extraTension) { 648b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru mTension = tension * extraTension; 658b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 668b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 678b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) { 68e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui this(context.getResources(), context.getTheme(), attrs); 69e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui } 70e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui 71e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui /** @hide */ 72e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) { 73e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui TypedArray a; 74e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui if (theme != null) { 75e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0); 76e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui } else { 77e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator); 78e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui } 798b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 808b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) * 818b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f); 82d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar setChangingConfiguration(a.getChangingConfigurations()); 838b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru a.recycle(); 848b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 858b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 868b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru private static float a(float t, float s) { 878b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru return t * t * ((s + 1) * t - s); 888b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 898b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 908b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru private static float o(float t, float s) { 918b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru return t * t * ((s + 1) * t + s); 928b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 938b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru 948b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru public float getInterpolation(float t) { 958b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru // a(t, s) = t * t * ((s + 1) * t - s) 968b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru // o(t, s) = t * t * ((s + 1) * t + s) 978b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5 988b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0 998b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension); 1008b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f); 1018b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru } 102c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck 103c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck /** @hide */ 104c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck @Override 105c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck public long createNativeInterpolator() { 106c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension); 107c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck } 1088b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru} 109