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.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;
     25 
     26 /**
     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.
     29  */
     30 public class AnticipateOvershootInterpolator implements Interpolator {
     31     private final float mTension;
     32 
     33     public AnticipateOvershootInterpolator() {
     34         mTension = 2.0f * 1.5f;
     35     }
     36 
     37     /**
     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.
     41      */
     42     public AnticipateOvershootInterpolator(float tension) {
     43         mTension = tension * 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      * @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.
     53      */
     54     public AnticipateOvershootInterpolator(float tension, float extraTension) {
     55         mTension = tension * extraTension;
     56     }
     57 
     58     public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
     59         TypedArray a = context.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator);
     60 
     61         mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
     62                 a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
     63 
     64         a.recycle();
     65     }
     66 
     67     private static float a(float t, float s) {
     68         return t * t * ((s + 1) * t - s);
     69     }
     70 
     71     private static float o(float t, float s) {
     72         return t * t * ((s + 1) * t + s);
     73     }
     74 
     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);
     82     }
     83 }
     84