2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.actionbarsherlock.internal.nineoldandroids.animation;
19 import java.util.ArrayList;
20 import android.view.animation.Interpolator;
22 import com.actionbarsherlock.internal.nineoldandroids.animation.Keyframe.FloatKeyframe;
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.
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>
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;
41 public FloatKeyframeSet(FloatKeyframe... keyframes) {
46 public Object getValue(float fraction) {
47 return getFloatValue(fraction);
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();
58 FloatKeyframeSet newSet = new FloatKeyframeSet(newKeyframes);
62 public float getFloatValue(float fraction) {
63 if (mNumKeyframes == 2) {
66 firstValue = ((FloatKeyframe) mKeyframes.get(0)).getFloatValue();
67 lastValue = ((FloatKeyframe) mKeyframes.get(1)).getFloatValue();
68 deltaValue = lastValue - firstValue;
70 if (mInterpolator != null) {
71 fraction = mInterpolator.getInterpolation(fraction);
73 if (mEvaluator == null) {
74 return firstValue + fraction * deltaValue;
76 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).floatValue();
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);
90 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
91 return mEvaluator == null ?
92 prevValue + intervalFraction * (nextValue - prevValue) :
93 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
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);
106 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
107 return mEvaluator == null ?
108 prevValue + intervalFraction * (nextValue - prevValue) :
109 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
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);
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)).
129 prevKeyframe = nextKeyframe;
131 // shouldn't get here
132 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).floatValue();