Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2010 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.animation;
     18 
     19 import android.animation.Keyframe.FloatKeyframe;
     20 
     21 import java.util.ArrayList;
     22 
     23 /**
     24  * This class holds a collection of FloatKeyframe objects and is called by ValueAnimator to calculate
     25  * values between those keyframes for a given animation. The class internal to the animation
     26  * package because it is an implementation detail of how Keyframes are stored and used.
     27  *
     28  * <p>This type-specific subclass of KeyframeSet, along with the other type-specific subclass for
     29  * int, exists to speed up the getValue() method when there is no custom
     30  * TypeEvaluator set for the animation, so that values can be calculated without autoboxing to the
     31  * Object equivalents of these primitive types.</p>
     32  */
     33 class FloatKeyframeSet extends KeyframeSet {
     34     private float firstValue;
     35     private float lastValue;
     36     private float deltaValue;
     37     private boolean firstTime = true;
     38 
     39     public FloatKeyframeSet(FloatKeyframe... keyframes) {
     40         super(keyframes);
     41     }
     42 
     43     @Override
     44     public Object getValue(float fraction) {
     45         return getFloatValue(fraction);
     46     }
     47 
     48     @Override
     49     public FloatKeyframeSet clone() {
     50         ArrayList<Keyframe> keyframes = mKeyframes;
     51         int numKeyframes = mKeyframes.size();
     52         FloatKeyframe[] newKeyframes = new FloatKeyframe[numKeyframes];
     53         for (int i = 0; i < numKeyframes; ++i) {
     54             newKeyframes[i] = (FloatKeyframe) keyframes.get(i).clone();
     55         }
     56         FloatKeyframeSet newSet = new FloatKeyframeSet(newKeyframes);
     57         return newSet;
     58     }
     59 
     60     public float getFloatValue(float fraction) {
     61         if (mNumKeyframes == 2) {
     62             if (firstTime) {
     63                 firstTime = false;
     64                 firstValue = ((FloatKeyframe) mKeyframes.get(0)).getFloatValue();
     65                 lastValue = ((FloatKeyframe) mKeyframes.get(1)).getFloatValue();
     66                 deltaValue = lastValue - firstValue;
     67             }
     68             if (mInterpolator != null) {
     69                 fraction = mInterpolator.getInterpolation(fraction);
     70             }
     71             if (mEvaluator == null) {
     72                 return firstValue + fraction * deltaValue;
     73             } else {
     74                 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).floatValue();
     75             }
     76         }
     77         if (fraction <= 0f) {
     78             final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0);
     79             final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(1);
     80             float prevValue = prevKeyframe.getFloatValue();
     81             float nextValue = nextKeyframe.getFloatValue();
     82             float prevFraction = prevKeyframe.getFraction();
     83             float nextFraction = nextKeyframe.getFraction();
     84             final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
     85             if (interpolator != null) {
     86                 fraction = interpolator.getInterpolation(fraction);
     87             }
     88             float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
     89             return mEvaluator == null ?
     90                     prevValue + intervalFraction * (nextValue - prevValue) :
     91                     ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
     92                             floatValue();
     93         } else if (fraction >= 1f) {
     94             final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 2);
     95             final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 1);
     96             float prevValue = prevKeyframe.getFloatValue();
     97             float nextValue = nextKeyframe.getFloatValue();
     98             float prevFraction = prevKeyframe.getFraction();
     99             float nextFraction = nextKeyframe.getFraction();
    100             final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
    101             if (interpolator != null) {
    102                 fraction = interpolator.getInterpolation(fraction);
    103             }
    104             float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
    105             return mEvaluator == null ?
    106                     prevValue + intervalFraction * (nextValue - prevValue) :
    107                     ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
    108                             floatValue();
    109         }
    110         FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0);
    111         for (int i = 1; i < mNumKeyframes; ++i) {
    112             FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(i);
    113             if (fraction < nextKeyframe.getFraction()) {
    114                 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
    115                 if (interpolator != null) {
    116                     fraction = interpolator.getInterpolation(fraction);
    117                 }
    118                 float intervalFraction = (fraction - prevKeyframe.getFraction()) /
    119                     (nextKeyframe.getFraction() - prevKeyframe.getFraction());
    120                 float prevValue = prevKeyframe.getFloatValue();
    121                 float nextValue = nextKeyframe.getFloatValue();
    122                 return mEvaluator == null ?
    123                         prevValue + intervalFraction * (nextValue - prevValue) :
    124                         ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
    125                             floatValue();
    126             }
    127             prevKeyframe = nextKeyframe;
    128         }
    129         // shouldn't get here
    130         return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).floatValue();
    131     }
    132 
    133 }
    134 
    135