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 import java.util.List; 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 class IntKeyframeSet extends KeyframeSet implements Keyframes.IntKeyframes { 35 private int firstValue; 36 private int lastValue; 37 private int deltaValue; 38 private boolean firstTime = true; 39 IntKeyframeSet(IntKeyframe... keyframes)40 public IntKeyframeSet(IntKeyframe... keyframes) { 41 super(keyframes); 42 } 43 44 @Override getValue(float fraction)45 public Object getValue(float fraction) { 46 return getIntValue(fraction); 47 } 48 49 @Override clone()50 public IntKeyframeSet clone() { 51 List<Keyframe> keyframes = mKeyframes; 52 int numKeyframes = mKeyframes.size(); 53 IntKeyframe[] newKeyframes = new IntKeyframe[numKeyframes]; 54 for (int i = 0; i < numKeyframes; ++i) { 55 newKeyframes[i] = (IntKeyframe) keyframes.get(i).clone(); 56 } 57 IntKeyframeSet newSet = new IntKeyframeSet(newKeyframes); 58 return newSet; 59 } 60 61 @Override invalidateCache()62 public void invalidateCache() { 63 firstTime = true; 64 } 65 66 @Override getIntValue(float fraction)67 public int getIntValue(float fraction) { 68 if (mNumKeyframes == 2) { 69 if (firstTime) { 70 firstTime = false; 71 firstValue = ((IntKeyframe) mKeyframes.get(0)).getIntValue(); 72 lastValue = ((IntKeyframe) mKeyframes.get(1)).getIntValue(); 73 deltaValue = lastValue - firstValue; 74 } 75 if (mInterpolator != null) { 76 fraction = mInterpolator.getInterpolation(fraction); 77 } 78 if (mEvaluator == null) { 79 return firstValue + (int)(fraction * deltaValue); 80 } else { 81 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).intValue(); 82 } 83 } 84 if (fraction <= 0f) { 85 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0); 86 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(1); 87 int prevValue = prevKeyframe.getIntValue(); 88 int nextValue = nextKeyframe.getIntValue(); 89 float prevFraction = prevKeyframe.getFraction(); 90 float nextFraction = nextKeyframe.getFraction(); 91 final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); 92 if (interpolator != null) { 93 fraction = interpolator.getInterpolation(fraction); 94 } 95 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction); 96 return mEvaluator == null ? 97 prevValue + (int)(intervalFraction * (nextValue - prevValue)) : 98 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)). 99 intValue(); 100 } else if (fraction >= 1f) { 101 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 2); 102 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 1); 103 int prevValue = prevKeyframe.getIntValue(); 104 int nextValue = nextKeyframe.getIntValue(); 105 float prevFraction = prevKeyframe.getFraction(); 106 float nextFraction = nextKeyframe.getFraction(); 107 final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); 108 if (interpolator != null) { 109 fraction = interpolator.getInterpolation(fraction); 110 } 111 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction); 112 return mEvaluator == null ? 113 prevValue + (int)(intervalFraction * (nextValue - prevValue)) : 114 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).intValue(); 115 } 116 IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0); 117 for (int i = 1; i < mNumKeyframes; ++i) { 118 IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(i); 119 if (fraction < nextKeyframe.getFraction()) { 120 final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); 121 if (interpolator != null) { 122 fraction = interpolator.getInterpolation(fraction); 123 } 124 float intervalFraction = (fraction - prevKeyframe.getFraction()) / 125 (nextKeyframe.getFraction() - prevKeyframe.getFraction()); 126 int prevValue = prevKeyframe.getIntValue(); 127 int nextValue = nextKeyframe.getIntValue(); 128 return mEvaluator == null ? 129 prevValue + (int)(intervalFraction * (nextValue - prevValue)) : 130 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)). 131 intValue(); 132 } 133 prevKeyframe = nextKeyframe; 134 } 135 // shouldn't get here 136 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).intValue(); 137 } 138 139 @Override getType()140 public Class getType() { 141 return Integer.class; 142 } 143 } 144 145