e9215e7f8cd25bf17290a5e456d0c9495326ea0c
[pub/Android/ownCloud.git] / actionbarsherlock / src / com / actionbarsherlock / internal / nineoldandroids / animation / IntKeyframeSet.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.IntKeyframe;
23
24 /**
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.
28 *
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>
33 */
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;
40
41 public IntKeyframeSet(IntKeyframe... keyframes) {
42 super(keyframes);
43 }
44
45 @Override
46 public Object getValue(float fraction) {
47 return getIntValue(fraction);
48 }
49
50 @Override
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();
57 }
58 IntKeyframeSet newSet = new IntKeyframeSet(newKeyframes);
59 return newSet;
60 }
61
62 public int getIntValue(float fraction) {
63 if (mNumKeyframes == 2) {
64 if (firstTime) {
65 firstTime = false;
66 firstValue = ((IntKeyframe) mKeyframes.get(0)).getIntValue();
67 lastValue = ((IntKeyframe) mKeyframes.get(1)).getIntValue();
68 deltaValue = lastValue - firstValue;
69 }
70 if (mInterpolator != null) {
71 fraction = mInterpolator.getInterpolation(fraction);
72 }
73 if (mEvaluator == null) {
74 return firstValue + (int)(fraction * deltaValue);
75 } else {
76 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).intValue();
77 }
78 }
79 if (fraction <= 0f) {
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);
89 }
90 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
91 return mEvaluator == null ?
92 prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
93 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
94 intValue();
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);
105 }
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();
110 }
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);
118 }
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)).
126 intValue();
127 }
128 prevKeyframe = nextKeyframe;
129 }
130 // shouldn't get here
131 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).intValue();
132 }
133
134 }
135