Home | History | Annotate | Download | only in animation
      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 
     17 package android.view.animation;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.Resources.Theme;
     22 import android.content.res.TypedArray;
     23 import android.util.AttributeSet;
     24 
     25 import com.android.internal.view.animation.HasNativeInterpolator;
     26 import com.android.internal.view.animation.NativeInterpolatorFactory;
     27 import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
     28 
     29 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension;
     30 import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension;
     31 import 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
     38 public class AnticipateOvershootInterpolator extends BaseInterpolator
     39         implements NativeInterpolatorFactory {
     40     private final float mTension;
     41 
     42     public AnticipateOvershootInterpolator() {
     43         mTension = 2.0f * 1.5f;
     44     }
     45 
     46     /**
     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      */
     51     public AnticipateOvershootInterpolator(float tension) {
     52         mTension = tension * 1.5f;
     53     }
     54 
     55     /**
     56      * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
     57      *                there is no anticipation/overshoot and the interpolator becomes
     58      *                a simple acceleration/deceleration interpolator.
     59      * @param extraTension Amount by which to multiply the tension. For instance,
     60      *                     to get the same overshoot as an OvershootInterpolator with
     61      *                     a tension of 2.0f, you would use an extraTension of 1.5f.
     62      */
     63     public AnticipateOvershootInterpolator(float tension, float extraTension) {
     64         mTension = tension * extraTension;
     65     }
     66 
     67     public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
     68         this(context.getResources(), context.getTheme(), attrs);
     69     }
     70 
     71     /** @hide */
     72     public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
     73         TypedArray a;
     74         if (theme != null) {
     75             a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0);
     76         } else {
     77             a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator);
     78         }
     79 
     80         mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
     81                 a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
     82         setChangingConfiguration(a.getChangingConfigurations());
     83         a.recycle();
     84     }
     85 
     86     private static float a(float t, float s) {
     87         return t * t * ((s + 1) * t - s);
     88     }
     89 
     90     private static float o(float t, float s) {
     91         return t * t * ((s + 1) * t + s);
     92     }
     93 
     94     public float getInterpolation(float t) {
     95         // a(t, s) = t * t * ((s + 1) * t - s)
     96         // o(t, s) = t * t * ((s + 1) * t + s)
     97         // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
     98         // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
     99         if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
    100         else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
    101     }
    102 
    103     /** @hide */
    104     @Override
    105     public long createNativeInterpolator() {
    106         return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension);
    107     }
    108 }
    109