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.IntKeyframe;
25 * This class holds a collection of IntKeyframe 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 * float, 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 IntKeyframeSet extends KeyframeSet {
36 private int firstValue;
37 private int lastValue;
38 private int deltaValue;
39 private boolean firstTime = true;
41 public IntKeyframeSet(IntKeyframe... keyframes) {
46 public Object getValue(float fraction) {
47 return getIntValue(fraction);
51 public IntKeyframeSet clone() {
52 ArrayList<Keyframe> keyframes = mKeyframes;
53 int numKeyframes = mKeyframes.size();
54 IntKeyframe[] newKeyframes = new IntKeyframe[numKeyframes];
55 for (int i = 0; i < numKeyframes; ++i) {
56 newKeyframes[i] = (IntKeyframe) keyframes.get(i).clone();
58 IntKeyframeSet newSet = new IntKeyframeSet(newKeyframes);
62 public int getIntValue(float fraction) {
63 if (mNumKeyframes == 2) {
66 firstValue = ((IntKeyframe) mKeyframes.get(0)).getIntValue();
67 lastValue = ((IntKeyframe) mKeyframes.get(1)).getIntValue();
68 deltaValue = lastValue - firstValue;
70 if (mInterpolator != null) {
71 fraction = mInterpolator.getInterpolation(fraction);
73 if (mEvaluator == null) {
74 return firstValue + (int)(fraction * deltaValue);
76 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).intValue();
80 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
81 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(1);
82 int prevValue = prevKeyframe.getIntValue();
83 int nextValue = nextKeyframe.getIntValue();
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 + (int)(intervalFraction * (nextValue - prevValue)) :
93 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
95 } else if (fraction >= 1f) {
96 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 2);
97 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 1);
98 int prevValue = prevKeyframe.getIntValue();
99 int nextValue = nextKeyframe.getIntValue();
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 + (int)(intervalFraction * (nextValue - prevValue)) :
109 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).intValue();
111 IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
112 for (int i = 1; i < mNumKeyframes; ++i) {
113 IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(i);
114 if (fraction < nextKeyframe.getFraction()) {
115 final /*Time*/Interpolator interpolator = nextKeyframe.getInterpolator();
116 if (interpolator != null) {
117 fraction = interpolator.getInterpolation(fraction);
119 float intervalFraction = (fraction - prevKeyframe.getFraction()) /
120 (nextKeyframe.getFraction() - prevKeyframe.getFraction());
121 int prevValue = prevKeyframe.getIntValue();
122 int nextValue = nextKeyframe.getIntValue();
123 return mEvaluator == null ?
124 prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
125 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
128 prevKeyframe = nextKeyframe;
130 // shouldn't get here
131 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).intValue();