• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.IntKeyframe;
20 
21 import java.util.ArrayList;
22 
23 /**
24  * This class holds a collection of IntKeyframe 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  * float, 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 IntKeyframeSet extends KeyframeSet {
34     private int firstValue;
35     private int lastValue;
36     private int deltaValue;
37     private boolean firstTime = true;
38 
IntKeyframeSet(IntKeyframe... keyframes)39     public IntKeyframeSet(IntKeyframe... keyframes) {
40         super(keyframes);
41     }
42 
43     @Override
getValue(float fraction)44     public Object getValue(float fraction) {
45         return getIntValue(fraction);
46     }
47 
48     @Override
clone()49     public IntKeyframeSet clone() {
50         ArrayList<Keyframe> keyframes = mKeyframes;
51         int numKeyframes = mKeyframes.size();
52         IntKeyframe[] newKeyframes = new IntKeyframe[numKeyframes];
53         for (int i = 0; i < numKeyframes; ++i) {
54             newKeyframes[i] = (IntKeyframe) keyframes.get(i).clone();
55         }
56         IntKeyframeSet newSet = new IntKeyframeSet(newKeyframes);
57         return newSet;
58     }
59 
getIntValue(float fraction)60     public int getIntValue(float fraction) {
61         if (mNumKeyframes == 2) {
62             if (firstTime) {
63                 firstTime = false;
64                 firstValue = ((IntKeyframe) mKeyframes.get(0)).getIntValue();
65                 lastValue = ((IntKeyframe) mKeyframes.get(1)).getIntValue();
66                 deltaValue = lastValue - firstValue;
67             }
68             if (mInterpolator != null) {
69                 fraction = mInterpolator.getInterpolation(fraction);
70             }
71             if (mEvaluator == null) {
72                 return firstValue + (int)(fraction * deltaValue);
73             } else {
74                 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).intValue();
75             }
76         }
77         if (fraction <= 0f) {
78             final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
79             final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(1);
80             int prevValue = prevKeyframe.getIntValue();
81             int nextValue = nextKeyframe.getIntValue();
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 + (int)(intervalFraction * (nextValue - prevValue)) :
91                     ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
92                             intValue();
93         } else if (fraction >= 1f) {
94             final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 2);
95             final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 1);
96             int prevValue = prevKeyframe.getIntValue();
97             int nextValue = nextKeyframe.getIntValue();
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 + (int)(intervalFraction * (nextValue - prevValue)) :
107                     ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).intValue();
108         }
109         IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
110         for (int i = 1; i < mNumKeyframes; ++i) {
111             IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(i);
112             if (fraction < nextKeyframe.getFraction()) {
113                 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
114                 if (interpolator != null) {
115                     fraction = interpolator.getInterpolation(fraction);
116                 }
117                 float intervalFraction = (fraction - prevKeyframe.getFraction()) /
118                     (nextKeyframe.getFraction() - prevKeyframe.getFraction());
119                 int prevValue = prevKeyframe.getIntValue();
120                 int nextValue = nextKeyframe.getIntValue();
121                 return mEvaluator == null ?
122                         prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
123                         ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
124                                 intValue();
125             }
126             prevKeyframe = nextKeyframe;
127         }
128         // shouldn't get here
129         return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).intValue();
130     }
131 
132 }
133 
134