blob: 32edd4d8f13730f99a843912238a9f0e8f53cabb [file] [log] [blame]
Chet Haase3dd207a2010-07-20 14:00:01 -07001/*
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
17package android.animation;
18
Chet Haase2794eb32010-10-12 16:29:28 -070019import java.util.Arrays;
Yigit Boyard422dc32014-09-25 12:23:35 -070020import java.util.List;
21
Chet Haase7c608f22010-10-22 17:54:04 -070022import android.animation.Keyframe.IntKeyframe;
23import android.animation.Keyframe.FloatKeyframe;
24import android.animation.Keyframe.ObjectKeyframe;
George Mount984011f2014-08-21 14:28:01 -070025import android.graphics.Path;
Chet Haase20763162013-03-15 13:48:53 -070026import android.util.Log;
Chet Haase3dd207a2010-07-20 14:00:01 -070027
Chet Haase3dd207a2010-07-20 14:00:01 -070028/**
Chet Haasea18a86b2010-09-07 13:20:00 -070029 * This class holds a collection of Keyframe objects and is called by ValueAnimator to calculate
Chet Haase3dd207a2010-07-20 14:00:01 -070030 * values between those keyframes for a given animation. The class internal to the animation
31 * package because it is an implementation detail of how Keyframes are stored and used.
32 */
George Mount984011f2014-08-21 14:28:01 -070033class KeyframeSet implements Keyframes {
Chet Haase3dd207a2010-07-20 14:00:01 -070034
Chet Haase7c608f22010-10-22 17:54:04 -070035 int mNumKeyframes;
Chet Haase3dd207a2010-07-20 14:00:01 -070036
Chet Haase70d4ba12010-10-06 09:46:45 -070037 Keyframe mFirstKeyframe;
38 Keyframe mLastKeyframe;
39 TimeInterpolator mInterpolator; // only used in the 2-keyframe case
Yigit Boyard422dc32014-09-25 12:23:35 -070040 List<Keyframe> mKeyframes; // only used when there are not 2 keyframes
Chet Haase7c608f22010-10-22 17:54:04 -070041 TypeEvaluator mEvaluator;
42
Chet Haase3dd207a2010-07-20 14:00:01 -070043
44 public KeyframeSet(Keyframe... keyframes) {
Chet Haase70d4ba12010-10-06 09:46:45 -070045 mNumKeyframes = keyframes.length;
Yigit Boyard422dc32014-09-25 12:23:35 -070046 // immutable list
47 mKeyframes = Arrays.asList(keyframes);
48 mFirstKeyframe = keyframes[0];
49 mLastKeyframe = keyframes[mNumKeyframes - 1];
Chet Haase70d4ba12010-10-06 09:46:45 -070050 mInterpolator = mLastKeyframe.getInterpolator();
Chet Haase3dd207a2010-07-20 14:00:01 -070051 }
52
Yigit Boyar8619f482014-07-15 17:28:07 -070053 /**
54 * If subclass has variables that it calculates based on the Keyframes, it should reset them
55 * when this method is called because Keyframe contents might have changed.
56 */
George Mount984011f2014-08-21 14:28:01 -070057 @Override
58 public void invalidateCache() {
59 }
60
Yigit Boyard422dc32014-09-25 12:23:35 -070061 public List<Keyframe> getKeyframes() {
George Mount984011f2014-08-21 14:28:01 -070062 return mKeyframes;
Yigit Boyar8619f482014-07-15 17:28:07 -070063 }
64
Chet Haase2794eb32010-10-12 16:29:28 -070065 public static KeyframeSet ofInt(int... values) {
66 int numKeyframes = values.length;
Chet Haase7c608f22010-10-22 17:54:04 -070067 IntKeyframe keyframes[] = new IntKeyframe[Math.max(numKeyframes,2)];
Chet Haase2794eb32010-10-12 16:29:28 -070068 if (numKeyframes == 1) {
Chet Haase7c608f22010-10-22 17:54:04 -070069 keyframes[0] = (IntKeyframe) Keyframe.ofInt(0f);
70 keyframes[1] = (IntKeyframe) Keyframe.ofInt(1f, values[0]);
Chet Haase2794eb32010-10-12 16:29:28 -070071 } else {
Chet Haase7c608f22010-10-22 17:54:04 -070072 keyframes[0] = (IntKeyframe) Keyframe.ofInt(0f, values[0]);
Chet Haase2794eb32010-10-12 16:29:28 -070073 for (int i = 1; i < numKeyframes; ++i) {
Chet Haase20763162013-03-15 13:48:53 -070074 keyframes[i] =
75 (IntKeyframe) Keyframe.ofInt((float) i / (numKeyframes - 1), values[i]);
Chet Haase2794eb32010-10-12 16:29:28 -070076 }
77 }
Chet Haase7c608f22010-10-22 17:54:04 -070078 return new IntKeyframeSet(keyframes);
Chet Haase2794eb32010-10-12 16:29:28 -070079 }
80
81 public static KeyframeSet ofFloat(float... values) {
Chet Haase20763162013-03-15 13:48:53 -070082 boolean badValue = false;
Chet Haase2794eb32010-10-12 16:29:28 -070083 int numKeyframes = values.length;
Chet Haase7c608f22010-10-22 17:54:04 -070084 FloatKeyframe keyframes[] = new FloatKeyframe[Math.max(numKeyframes,2)];
Chet Haase2794eb32010-10-12 16:29:28 -070085 if (numKeyframes == 1) {
Chet Haase7c608f22010-10-22 17:54:04 -070086 keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f);
87 keyframes[1] = (FloatKeyframe) Keyframe.ofFloat(1f, values[0]);
Chet Haase20763162013-03-15 13:48:53 -070088 if (Float.isNaN(values[0])) {
89 badValue = true;
90 }
Chet Haase2794eb32010-10-12 16:29:28 -070091 } else {
Chet Haase7c608f22010-10-22 17:54:04 -070092 keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f, values[0]);
Chet Haase2794eb32010-10-12 16:29:28 -070093 for (int i = 1; i < numKeyframes; ++i) {
Chet Haase20763162013-03-15 13:48:53 -070094 keyframes[i] =
95 (FloatKeyframe) Keyframe.ofFloat((float) i / (numKeyframes - 1), values[i]);
96 if (Float.isNaN(values[i])) {
97 badValue = true;
98 }
Chet Haase2794eb32010-10-12 16:29:28 -070099 }
100 }
Chet Haase20763162013-03-15 13:48:53 -0700101 if (badValue) {
102 Log.w("Animator", "Bad value (NaN) in float animator");
103 }
Chet Haase7c608f22010-10-22 17:54:04 -0700104 return new FloatKeyframeSet(keyframes);
Chet Haase2794eb32010-10-12 16:29:28 -0700105 }
106
Chet Haase7c608f22010-10-22 17:54:04 -0700107 public static KeyframeSet ofKeyframe(Keyframe... keyframes) {
108 // if all keyframes of same primitive type, create the appropriate KeyframeSet
109 int numKeyframes = keyframes.length;
110 boolean hasFloat = false;
111 boolean hasInt = false;
112 boolean hasOther = false;
113 for (int i = 0; i < numKeyframes; ++i) {
114 if (keyframes[i] instanceof FloatKeyframe) {
115 hasFloat = true;
116 } else if (keyframes[i] instanceof IntKeyframe) {
117 hasInt = true;
118 } else {
119 hasOther = true;
Chet Haase2794eb32010-10-12 16:29:28 -0700120 }
121 }
Chet Haase7c608f22010-10-22 17:54:04 -0700122 if (hasFloat && !hasInt && !hasOther) {
123 FloatKeyframe floatKeyframes[] = new FloatKeyframe[numKeyframes];
124 for (int i = 0; i < numKeyframes; ++i) {
125 floatKeyframes[i] = (FloatKeyframe) keyframes[i];
Chet Haase2794eb32010-10-12 16:29:28 -0700126 }
Chet Haase7c608f22010-10-22 17:54:04 -0700127 return new FloatKeyframeSet(floatKeyframes);
128 } else if (hasInt && !hasFloat && !hasOther) {
129 IntKeyframe intKeyframes[] = new IntKeyframe[numKeyframes];
130 for (int i = 0; i < numKeyframes; ++i) {
131 intKeyframes[i] = (IntKeyframe) keyframes[i];
132 }
133 return new IntKeyframeSet(intKeyframes);
134 } else {
135 return new KeyframeSet(keyframes);
Chet Haase2794eb32010-10-12 16:29:28 -0700136 }
Chet Haase2794eb32010-10-12 16:29:28 -0700137 }
138
139 public static KeyframeSet ofObject(Object... values) {
140 int numKeyframes = values.length;
Chet Haase7c608f22010-10-22 17:54:04 -0700141 ObjectKeyframe keyframes[] = new ObjectKeyframe[Math.max(numKeyframes,2)];
Chet Haase2794eb32010-10-12 16:29:28 -0700142 if (numKeyframes == 1) {
Chet Haase7c608f22010-10-22 17:54:04 -0700143 keyframes[0] = (ObjectKeyframe) Keyframe.ofObject(0f);
144 keyframes[1] = (ObjectKeyframe) Keyframe.ofObject(1f, values[0]);
Chet Haase2794eb32010-10-12 16:29:28 -0700145 } else {
Chet Haase7c608f22010-10-22 17:54:04 -0700146 keyframes[0] = (ObjectKeyframe) Keyframe.ofObject(0f, values[0]);
Chet Haase2794eb32010-10-12 16:29:28 -0700147 for (int i = 1; i < numKeyframes; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -0700148 keyframes[i] = (ObjectKeyframe) Keyframe.ofObject((float) i / (numKeyframes - 1), values[i]);
Chet Haase2794eb32010-10-12 16:29:28 -0700149 }
150 }
151 return new KeyframeSet(keyframes);
152 }
153
George Mount984011f2014-08-21 14:28:01 -0700154 public static PathKeyframes ofPath(Path path) {
155 return new PathKeyframes(path);
156 }
157
George Mountfd3c4742014-09-08 13:23:36 -0700158 public static PathKeyframes ofPath(Path path, float error) {
159 return new PathKeyframes(path, error);
160 }
161
Chet Haase3dd207a2010-07-20 14:00:01 -0700162 /**
Chet Haase7c608f22010-10-22 17:54:04 -0700163 * Sets the TypeEvaluator to be used when calculating animated values. This object
164 * is required only for KeyframeSets that are not either IntKeyframeSet or FloatKeyframeSet,
165 * both of which assume their own evaluator to speed up calculations with those primitive
166 * types.
167 *
168 * @param evaluator The TypeEvaluator to be used to calculate animated values.
169 */
170 public void setEvaluator(TypeEvaluator evaluator) {
171 mEvaluator = evaluator;
172 }
173
174 @Override
George Mount984011f2014-08-21 14:28:01 -0700175 public Class getType() {
176 return mFirstKeyframe.getType();
177 }
178
179 @Override
Chet Haase7c608f22010-10-22 17:54:04 -0700180 public KeyframeSet clone() {
Yigit Boyard422dc32014-09-25 12:23:35 -0700181 List<Keyframe> keyframes = mKeyframes;
Chet Haase7c608f22010-10-22 17:54:04 -0700182 int numKeyframes = mKeyframes.size();
Yigit Boyard422dc32014-09-25 12:23:35 -0700183 final Keyframe[] newKeyframes = new Keyframe[numKeyframes];
Chet Haase7c608f22010-10-22 17:54:04 -0700184 for (int i = 0; i < numKeyframes; ++i) {
185 newKeyframes[i] = keyframes.get(i).clone();
186 }
187 KeyframeSet newSet = new KeyframeSet(newKeyframes);
188 return newSet;
189 }
190
191 /**
Chet Haase3dd207a2010-07-20 14:00:01 -0700192 * Gets the animated value, given the elapsed fraction of the animation (interpolated by the
193 * animation's interpolator) and the evaluator used to calculate in-between values. This
194 * function maps the input fraction to the appropriate keyframe interval and a fraction
195 * between them and returns the interpolated value. Note that the input fraction may fall
196 * outside the [0-1] bounds, if the animation's interpolator made that happen (e.g., a
197 * spring interpolation that might send the fraction past 1.0). We handle this situation by
198 * just using the two keyframes at the appropriate end when the value is outside those bounds.
199 *
200 * @param fraction The elapsed fraction of the animation
Chet Haase3dd207a2010-07-20 14:00:01 -0700201 * @return The animated value.
202 */
Chet Haase7c608f22010-10-22 17:54:04 -0700203 public Object getValue(float fraction) {
Chet Haase70d4ba12010-10-06 09:46:45 -0700204 // Special-case optimization for the common case of only two keyframes
205 if (mNumKeyframes == 2) {
206 if (mInterpolator != null) {
207 fraction = mInterpolator.getInterpolation(fraction);
208 }
Chet Haase7c608f22010-10-22 17:54:04 -0700209 return mEvaluator.evaluate(fraction, mFirstKeyframe.getValue(),
Chet Haase70d4ba12010-10-06 09:46:45 -0700210 mLastKeyframe.getValue());
211 }
Chet Haase3dd207a2010-07-20 14:00:01 -0700212 if (fraction <= 0f) {
Chet Haase3dd207a2010-07-20 14:00:01 -0700213 final Keyframe nextKeyframe = mKeyframes.get(1);
Chet Haasee0ee2e92010-10-07 09:06:18 -0700214 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
Chet Haase3dd207a2010-07-20 14:00:01 -0700215 if (interpolator != null) {
216 fraction = interpolator.getInterpolation(fraction);
217 }
Chet Haase70d4ba12010-10-06 09:46:45 -0700218 final float prevFraction = mFirstKeyframe.getFraction();
219 float intervalFraction = (fraction - prevFraction) /
220 (nextKeyframe.getFraction() - prevFraction);
Chet Haase7c608f22010-10-22 17:54:04 -0700221 return mEvaluator.evaluate(intervalFraction, mFirstKeyframe.getValue(),
Chet Haase3dd207a2010-07-20 14:00:01 -0700222 nextKeyframe.getValue());
223 } else if (fraction >= 1f) {
224 final Keyframe prevKeyframe = mKeyframes.get(mNumKeyframes - 2);
Chet Haase70d4ba12010-10-06 09:46:45 -0700225 final TimeInterpolator interpolator = mLastKeyframe.getInterpolator();
Chet Haase3dd207a2010-07-20 14:00:01 -0700226 if (interpolator != null) {
227 fraction = interpolator.getInterpolation(fraction);
228 }
Chet Haase70d4ba12010-10-06 09:46:45 -0700229 final float prevFraction = prevKeyframe.getFraction();
230 float intervalFraction = (fraction - prevFraction) /
231 (mLastKeyframe.getFraction() - prevFraction);
Chet Haase7c608f22010-10-22 17:54:04 -0700232 return mEvaluator.evaluate(intervalFraction, prevKeyframe.getValue(),
Chet Haase70d4ba12010-10-06 09:46:45 -0700233 mLastKeyframe.getValue());
Chet Haase3dd207a2010-07-20 14:00:01 -0700234 }
Chet Haase70d4ba12010-10-06 09:46:45 -0700235 Keyframe prevKeyframe = mFirstKeyframe;
Chet Haase3dd207a2010-07-20 14:00:01 -0700236 for (int i = 1; i < mNumKeyframes; ++i) {
237 Keyframe nextKeyframe = mKeyframes.get(i);
238 if (fraction < nextKeyframe.getFraction()) {
Chet Haasee0ee2e92010-10-07 09:06:18 -0700239 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
Chet Haase70d4ba12010-10-06 09:46:45 -0700240 final float prevFraction = prevKeyframe.getFraction();
241 float intervalFraction = (fraction - prevFraction) /
242 (nextKeyframe.getFraction() - prevFraction);
Doris Liu6df99052015-03-30 16:03:59 -0700243 // Apply interpolator on the proportional duration.
244 if (interpolator != null) {
245 intervalFraction = interpolator.getInterpolation(intervalFraction);
246 }
Chet Haase7c608f22010-10-22 17:54:04 -0700247 return mEvaluator.evaluate(intervalFraction, prevKeyframe.getValue(),
Chet Haase3dd207a2010-07-20 14:00:01 -0700248 nextKeyframe.getValue());
249 }
250 prevKeyframe = nextKeyframe;
251 }
Chet Haase70d4ba12010-10-06 09:46:45 -0700252 // shouldn't reach here
253 return mLastKeyframe.getValue();
Chet Haase3dd207a2010-07-20 14:00:01 -0700254 }
Chet Haasee9140a72011-02-16 16:23:29 -0800255
256 @Override
257 public String toString() {
258 String returnVal = " ";
259 for (int i = 0; i < mNumKeyframes; ++i) {
260 returnVal += mKeyframes.get(i).getValue() + " ";
261 }
262 return returnVal;
263 }
Chet Haase3dd207a2010-07-20 14:00:01 -0700264}