blob: 7f11871b0f7416ab777185c9748cfa9eed8acb42 [file] [log] [blame]
Chet Haase17fb4b02010-06-28 17:55:07 -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
19import android.util.Log;
20
Chet Haase17fb4b02010-06-28 17:55:07 -070021import java.lang.reflect.Method;
Chet Haasee2ab7cc2010-12-06 16:10:07 -080022import java.util.ArrayList;
Chet Haase17fb4b02010-06-28 17:55:07 -070023
24/**
Chet Haasea18a86b2010-09-07 13:20:00 -070025 * This subclass of {@link ValueAnimator} provides support for animating properties on target objects.
Chet Haase17fb4b02010-06-28 17:55:07 -070026 * The constructors of this class take parameters to define the target object that will be animated
27 * as well as the name of the property that will be animated. Appropriate set/get functions
28 * are then determined internally and the animation will call these functions as necessary to
29 * animate the property.
Chet Haase6e0ecb42010-11-03 19:41:18 -070030 *
31 * @see #setPropertyName(String)
32 *
Chet Haase17fb4b02010-06-28 17:55:07 -070033 */
Chet Haase2794eb32010-10-12 16:29:28 -070034public final class ObjectAnimator extends ValueAnimator {
Chet Haasee2ab7cc2010-12-06 16:10:07 -080035 private static final boolean DBG = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070036
37 // The target object on which the property exists, set in the constructor
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -080038 private Object mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -070039
40 private String mPropertyName;
41
Chet Haase17fb4b02010-06-28 17:55:07 -070042 /**
43 * Sets the name of the property that will be animated. This name is used to derive
44 * a setter function that will be called to set animated values.
45 * For example, a property name of <code>foo</code> will result
46 * in a call to the function <code>setFoo()</code> on the target object. If either
47 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
48 * also be derived and called.
49 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070050 * <p>For best performance of the mechanism that calls the setter function determined by the
51 * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
52 * and make the setter function for those properties have a <code>void</code> return value. This
53 * will cause the code to take an optimized path for these constrained circumstances. Other
54 * property types and return types will work, but will have more overhead in processing
55 * the requests due to normal reflection mechanisms.</p>
56 *
Chet Haase17fb4b02010-06-28 17:55:07 -070057 * <p>Note that the setter function derived from this property name
58 * must take the same parameter type as the
59 * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
60 * the setter function will fail.</p>
61 *
Chet Haasea18a86b2010-09-07 13:20:00 -070062 * <p>If this ObjectAnimator has been set up to animate several properties together,
Chet Haased953d082010-08-16 17:44:28 -070063 * using more than one PropertyValuesHolder objects, then setting the propertyName simply
64 * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
65 *
Chet Haase17fb4b02010-06-28 17:55:07 -070066 * @param propertyName The name of the property being animated.
67 */
68 public void setPropertyName(String propertyName) {
Chet Haase0e0590b2010-09-26 11:57:28 -070069 // mValues could be null if this is being constructed piecemeal. Just record the
70 // propertyName to be used later when setValues() is called if so.
Chet Haased953d082010-08-16 17:44:28 -070071 if (mValues != null) {
Chet Haase602e4d32010-08-16 08:57:23 -070072 PropertyValuesHolder valuesHolder = mValues[0];
73 String oldName = valuesHolder.getPropertyName();
Chet Haased953d082010-08-16 17:44:28 -070074 valuesHolder.setPropertyName(propertyName);
Chet Haase602e4d32010-08-16 08:57:23 -070075 mValuesMap.remove(oldName);
76 mValuesMap.put(propertyName, valuesHolder);
Chet Haased953d082010-08-16 17:44:28 -070077 }
Chet Haase17fb4b02010-06-28 17:55:07 -070078 mPropertyName = propertyName;
Chet Haase0e0590b2010-09-26 11:57:28 -070079 // New property/values/target should cause re-initialization prior to starting
80 mInitialized = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070081 }
82
83 /**
84 * Gets the name of the property that will be animated. This name will be used to derive
85 * a setter function that will be called to set animated values.
86 * For example, a property name of <code>foo</code> will result
87 * in a call to the function <code>setFoo()</code> on the target object. If either
88 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
89 * also be derived and called.
90 */
91 public String getPropertyName() {
92 return mPropertyName;
93 }
94
95 /**
Chet Haase17fb4b02010-06-28 17:55:07 -070096 * Determine the setter or getter function using the JavaBeans convention of setFoo or
97 * getFoo for a property named 'foo'. This function figures out what the name of the
98 * function should be and uses reflection to find the Method with that name on the
99 * target object.
100 *
101 * @param prefix "set" or "get", depending on whether we need a setter or getter.
102 * @return Method the method associated with mPropertyName.
103 */
Chet Haasefe591562010-07-27 11:15:37 -0700104 private Method getPropertyFunction(String prefix, Class valueType) {
Chet Haase17fb4b02010-06-28 17:55:07 -0700105 // TODO: faster implementation...
106 Method returnVal = null;
107 String firstLetter = mPropertyName.substring(0, 1);
108 String theRest = mPropertyName.substring(1);
109 firstLetter = firstLetter.toUpperCase();
110 String setterName = prefix + firstLetter + theRest;
Chet Haasefe591562010-07-27 11:15:37 -0700111 Class args[] = null;
112 if (valueType != null) {
113 args = new Class[1];
114 args[0] = valueType;
115 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700116 try {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800117 returnVal = mTarget.getClass().getMethod(setterName, args);
Chet Haase17fb4b02010-06-28 17:55:07 -0700118 } catch (NoSuchMethodException e) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700119 Log.e("ObjectAnimator",
Chet Haasefe591562010-07-27 11:15:37 -0700120 "Couldn't find setter/getter for property " + mPropertyName + ": " + e);
Chet Haase17fb4b02010-06-28 17:55:07 -0700121 }
122 return returnVal;
123 }
124
125 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700126 * Creates a new ObjectAnimator object. This default constructor is primarily for
Chet Haased51d3682010-08-11 19:46:48 -0700127 * use internally; the other constructors which take parameters are more generally
128 * useful.
129 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700130 public ObjectAnimator() {
Chet Haased51d3682010-08-11 19:46:48 -0700131 }
132
133 /**
Chet Haased953d082010-08-16 17:44:28 -0700134 * A constructor that takes a single property name and set of values. This constructor is
135 * used in the simple case of animating a single property.
Chet Haase17fb4b02010-06-28 17:55:07 -0700136 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800137 * @param target The object whose property is to be animated. This object should
138 * have a public method on it called <code>setName()</code>, where <code>name</code> is
139 * the value of the <code>propertyName</code> parameter.
Chet Haased953d082010-08-16 17:44:28 -0700140 * @param propertyName The name of the property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700141 */
Chet Haase2794eb32010-10-12 16:29:28 -0700142 private ObjectAnimator(Object target, String propertyName) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800143 mTarget = target;
Chet Haased953d082010-08-16 17:44:28 -0700144 setPropertyName(propertyName);
Chet Haase17fb4b02010-06-28 17:55:07 -0700145 }
146
147 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700148 * Constructs and returns an ObjectAnimator that animates between int values. A single
149 * value implies that that value is the one being animated to. However, this is not typically
150 * useful in a ValueAnimator object because there is no way for the object to determine the
151 * starting value for the animation (unlike ObjectAnimator, which can derive that value
152 * from the target object and property being animated). Therefore, there should typically
153 * be two or more values.
154 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800155 * @param target The object whose property is to be animated. This object should
156 * have a public method on it called <code>setName()</code>, where <code>name</code> is
157 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700158 * @param propertyName The name of the property being animated.
159 * @param values A set of values that the animation will animate between over time.
160 * @return A ValueAnimator object that is set up to animate between the given values.
161 */
162 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
163 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
164 anim.setIntValues(values);
165 return anim;
166 }
167
168 /**
169 * Constructs and returns an ObjectAnimator that animates between float values. A single
170 * value implies that that value is the one being animated to. However, this is not typically
171 * useful in a ValueAnimator object because there is no way for the object to determine the
172 * starting value for the animation (unlike ObjectAnimator, which can derive that value
173 * from the target object and property being animated). Therefore, there should typically
174 * be two or more values.
175 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800176 * @param target The object whose property is to be animated. This object should
177 * have a public method on it called <code>setName()</code>, where <code>name</code> is
178 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700179 * @param propertyName The name of the property being animated.
180 * @param values A set of values that the animation will animate between over time.
181 * @return A ValueAnimator object that is set up to animate between the given values.
182 */
183 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
184 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
185 anim.setFloatValues(values);
186 return anim;
187 }
188
189 /**
Chet Haased953d082010-08-16 17:44:28 -0700190 * A constructor that takes <code>PropertyValueHolder</code> values. This constructor should
Chet Haasea18a86b2010-09-07 13:20:00 -0700191 * be used when animating several properties at once with the same ObjectAnimator, since
Chet Haased953d082010-08-16 17:44:28 -0700192 * PropertyValuesHolder allows you to associate a set of animation values with a property
193 * name.
Chet Haasefe591562010-07-27 11:15:37 -0700194 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800195 * @param target The object whose property is to be animated. This object should
196 * have public methods on it called <code>setName()</code>, where <code>name</code> is
197 * the name of the property passed in as the <code>propertyName</code> parameter for
198 * each of the PropertyValuesHolder objects.
Chet Haase2794eb32010-10-12 16:29:28 -0700199 * @param propertyName The name of the property being animated.
200 * @param evaluator A TypeEvaluator that will be called on each animation frame to
201 * provide the ncessry interpolation between the Object values to derive the animated
202 * value.
Chet Haased953d082010-08-16 17:44:28 -0700203 * @param values The PropertyValuesHolder objects which hold each the property name and values
204 * to animate that property between.
Chet Haasefe591562010-07-27 11:15:37 -0700205 */
Chet Haase2794eb32010-10-12 16:29:28 -0700206 public static ObjectAnimator ofObject(Object target, String propertyName,
207 TypeEvaluator evaluator, Object... values) {
208 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
209 anim.setObjectValues(values);
210 anim.setEvaluator(evaluator);
211 return anim;
212 }
213
214 /**
215 * Constructs and returns an ObjectAnimator that animates between the sets of values
216 * specifed in <code>PropertyValueHolder</code> objects. This variant should
217 * be used when animating several properties at once with the same ObjectAnimator, since
218 * PropertyValuesHolder allows you to associate a set of animation values with a property
219 * name.
220 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800221 * @param target The object whose property is to be animated. This object should
222 * have public methods on it called <code>setName()</code>, where <code>name</code> is
223 * the name of the property passed in as the <code>propertyName</code> parameter for
224 * each of the PropertyValuesHolder objects.
Chet Haase2794eb32010-10-12 16:29:28 -0700225 * @param values A set of PropertyValuesHolder objects whose values will be animated
226 * between over time.
227 * @return A ValueAnimator object that is set up to animate between the given values.
228 */
229 public static ObjectAnimator ofPropertyValuesHolder(Object target,
230 PropertyValuesHolder... values) {
231 ObjectAnimator anim = new ObjectAnimator();
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800232 anim.mTarget = target;
Chet Haase2794eb32010-10-12 16:29:28 -0700233 anim.setValues(values);
234 return anim;
Chet Haase3dd207a2010-07-20 14:00:01 -0700235 }
236
Romain Guy83d6e822010-10-14 10:13:53 -0700237 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700238 public void setIntValues(int... values) {
Romain Guy83d6e822010-10-14 10:13:53 -0700239 if (mValues == null || mValues.length == 0) {
240 // No values yet - this animator is being constructed piecemeal. Init the values with
241 // whatever the current propertyName is
Chet Haase2794eb32010-10-12 16:29:28 -0700242 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
Romain Guy83d6e822010-10-14 10:13:53 -0700243 } else {
Chet Haase2794eb32010-10-12 16:29:28 -0700244 super.setIntValues(values);
245 }
246 }
247
248 @Override
249 public void setFloatValues(float... values) {
250 if (mValues == null || mValues.length == 0) {
251 // No values yet - this animator is being constructed piecemeal. Init the values with
252 // whatever the current propertyName is
253 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
254 } else {
255 super.setFloatValues(values);
256 }
257 }
258
259 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700260 public void setObjectValues(Object... values) {
261 if (mValues == null || mValues.length == 0) {
262 // No values yet - this animator is being constructed piecemeal. Init the values with
263 // whatever the current propertyName is
264 setValues(PropertyValuesHolder.ofObject(mPropertyName, (TypeEvaluator)null, values));
265 } else {
266 super.setObjectValues(values);
Romain Guy83d6e822010-10-14 10:13:53 -0700267 }
Chet Haase0e0590b2010-09-26 11:57:28 -0700268 }
Romain Guy83d6e822010-10-14 10:13:53 -0700269
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800270 @Override
271 public void start() {
272 if (DBG) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800273 Log.d("ObjectAnimator", "Anim target, duration" + mTarget + ", " + getDuration());
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800274 for (int i = 0; i < mValues.length; ++i) {
275 PropertyValuesHolder pvh = mValues[i];
276 ArrayList<Keyframe> keyframes = pvh.mKeyframeSet.mKeyframes;
277 Log.d("ObjectAnimator", " Values[" + i + "]: " +
278 pvh.getPropertyName() + ", " + keyframes.get(0).getValue() + ", " +
279 keyframes.get(pvh.mKeyframeSet.mNumKeyframes - 1).getValue());
280 }
281 }
282 super.start();
283 }
284
Chet Haase3dd207a2010-07-20 14:00:01 -0700285 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700286 * This function is called immediately before processing the first animation
287 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
288 * function is called after that delay ends.
289 * It takes care of the final initialization steps for the
290 * animation. This includes setting mEvaluator, if the user has not yet
291 * set it up, and the setter/getter methods, if the user did not supply
292 * them.
293 *
294 * <p>Overriders of this method should call the superclass method to cause
295 * internal mechanisms to be set up correctly.</p>
296 */
297 @Override
298 void initAnimation() {
Chet Haase21cd1382010-09-01 17:42:29 -0700299 if (!mInitialized) {
300 // mValueType may change due to setter/getter setup; do this before calling super.init(),
301 // which uses mValueType to set up the default type evaluator.
302 int numValues = mValues.length;
303 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800304 mValues[i].setupSetterAndGetter(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700305 }
306 super.initAnimation();
Chet Haase17fb4b02010-06-28 17:55:07 -0700307 }
308 }
309
Chet Haase2794eb32010-10-12 16:29:28 -0700310 /**
311 * Sets the length of the animation. The default duration is 300 milliseconds.
312 *
313 * @param duration The length of the animation, in milliseconds.
314 * @return ObjectAnimator The object called with setDuration(). This return
315 * value makes it easier to compose statements together that construct and then set the
316 * duration, as in
317 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
318 */
319 @Override
320 public ObjectAnimator setDuration(long duration) {
321 super.setDuration(duration);
322 return this;
323 }
324
Chet Haase17fb4b02010-06-28 17:55:07 -0700325
326 /**
327 * The target object whose property will be animated by this animation
328 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800329 * @return The object being animated
Chet Haase17fb4b02010-06-28 17:55:07 -0700330 */
331 public Object getTarget() {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800332 return mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -0700333 }
334
335 /**
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800336 * Sets the target object whose property will be animated by this animation
Chet Haasef54a8d72010-07-22 14:44:59 -0700337 *
338 * @param target The object being animated
339 */
Chet Haase21cd1382010-09-01 17:42:29 -0700340 @Override
Chet Haasef54a8d72010-07-22 14:44:59 -0700341 public void setTarget(Object target) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800342 if (mTarget != target) {
343 mTarget = target;
344 if (mTarget != null && target != null && mTarget.getClass() == target.getClass()) {
Chet Haase70d4ba12010-10-06 09:46:45 -0700345 return;
346 }
347 // New target type should cause re-initialization prior to starting
348 mInitialized = false;
349 }
Chet Haasef54a8d72010-07-22 14:44:59 -0700350 }
351
Chet Haase21cd1382010-09-01 17:42:29 -0700352 @Override
353 public void setupStartValues() {
354 initAnimation();
355 int numValues = mValues.length;
356 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800357 mValues[i].setupStartValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700358 }
359 }
360
361 @Override
362 public void setupEndValues() {
363 initAnimation();
364 int numValues = mValues.length;
365 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800366 mValues[i].setupEndValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700367 }
368 }
369
Chet Haasef54a8d72010-07-22 14:44:59 -0700370 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700371 * This method is called with the elapsed fraction of the animation during every
372 * animation frame. This function turns the elapsed fraction into an interpolated fraction
373 * and then into an animated value (from the evaluator. The function is called mostly during
374 * animation updates, but it is also called when the <code>end()</code>
375 * function is called, to set the final value on the property.
376 *
377 * <p>Overrides of this method must call the superclass to perform the calculation
378 * of the animated value.</p>
379 *
380 * @param fraction The elapsed fraction of the animation.
381 */
382 @Override
383 void animateValue(float fraction) {
384 super.animateValue(fraction);
Chet Haase602e4d32010-08-16 08:57:23 -0700385 int numValues = mValues.length;
386 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800387 mValues[i].setAnimatedValue(mTarget);
Chet Haase17fb4b02010-06-28 17:55:07 -0700388 }
389 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700390
391 @Override
Chet Haasea18a86b2010-09-07 13:20:00 -0700392 public ObjectAnimator clone() {
393 final ObjectAnimator anim = (ObjectAnimator) super.clone();
Chet Haase49afa5b2010-08-23 11:39:53 -0700394 return anim;
395 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700396}