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