blob: 173ee73f337cd18aaa56b8c2f8a921068d09206a [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;
Chet Haaseb39f0512011-05-24 14:36:40 -070020import android.util.Property;
Chet Haase17fb4b02010-06-28 17:55:07 -070021
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 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080031 * <div class="special reference">
32 * <h3>Developer Guides</h3>
33 * <p>For more information about animating with {@code ObjectAnimator}, read the
34 * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#object-animator">Property
35 * Animation</a> developer guide.</p>
36 * </div>
37 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070038 * @see #setPropertyName(String)
39 *
Chet Haase17fb4b02010-06-28 17:55:07 -070040 */
Chet Haase2794eb32010-10-12 16:29:28 -070041public final class ObjectAnimator extends ValueAnimator {
Chet Haasee2ab7cc2010-12-06 16:10:07 -080042 private static final boolean DBG = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070043
44 // The target object on which the property exists, set in the constructor
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -080045 private Object mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -070046
47 private String mPropertyName;
48
Chet Haaseb39f0512011-05-24 14:36:40 -070049 private Property mProperty;
50
Chet Haasebe19e032013-03-15 17:08:55 -070051 private boolean mAutoCancel = false;
52
Chet Haase17fb4b02010-06-28 17:55:07 -070053 /**
54 * Sets the name of the property that will be animated. This name is used to derive
55 * a setter function that will be called to set animated values.
56 * For example, a property name of <code>foo</code> will result
57 * in a call to the function <code>setFoo()</code> on the target object. If either
58 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
59 * also be derived and called.
60 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070061 * <p>For best performance of the mechanism that calls the setter function determined by the
62 * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
63 * and make the setter function for those properties have a <code>void</code> return value. This
64 * will cause the code to take an optimized path for these constrained circumstances. Other
65 * property types and return types will work, but will have more overhead in processing
66 * the requests due to normal reflection mechanisms.</p>
67 *
Chet Haase17fb4b02010-06-28 17:55:07 -070068 * <p>Note that the setter function derived from this property name
69 * must take the same parameter type as the
70 * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
71 * the setter function will fail.</p>
72 *
Chet Haasea18a86b2010-09-07 13:20:00 -070073 * <p>If this ObjectAnimator has been set up to animate several properties together,
Chet Haased953d082010-08-16 17:44:28 -070074 * using more than one PropertyValuesHolder objects, then setting the propertyName simply
75 * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
76 *
Chet Haaseb39f0512011-05-24 14:36:40 -070077 * @param propertyName The name of the property being animated. Should not be null.
Chet Haase17fb4b02010-06-28 17:55:07 -070078 */
79 public void setPropertyName(String propertyName) {
Chet Haase0e0590b2010-09-26 11:57:28 -070080 // mValues could be null if this is being constructed piecemeal. Just record the
81 // propertyName to be used later when setValues() is called if so.
Chet Haased953d082010-08-16 17:44:28 -070082 if (mValues != null) {
Chet Haase602e4d32010-08-16 08:57:23 -070083 PropertyValuesHolder valuesHolder = mValues[0];
84 String oldName = valuesHolder.getPropertyName();
Chet Haased953d082010-08-16 17:44:28 -070085 valuesHolder.setPropertyName(propertyName);
Chet Haase602e4d32010-08-16 08:57:23 -070086 mValuesMap.remove(oldName);
87 mValuesMap.put(propertyName, valuesHolder);
Chet Haased953d082010-08-16 17:44:28 -070088 }
Chet Haase17fb4b02010-06-28 17:55:07 -070089 mPropertyName = propertyName;
Chet Haase0e0590b2010-09-26 11:57:28 -070090 // New property/values/target should cause re-initialization prior to starting
91 mInitialized = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070092 }
93
94 /**
Chet Haaseb39f0512011-05-24 14:36:40 -070095 * Sets the property that will be animated. Property objects will take precedence over
96 * properties specified by the {@link #setPropertyName(String)} method. Animations should
97 * be set up to use one or the other, not both.
98 *
99 * @param property The property being animated. Should not be null.
100 */
101 public void setProperty(Property property) {
102 // mValues could be null if this is being constructed piecemeal. Just record the
103 // propertyName to be used later when setValues() is called if so.
104 if (mValues != null) {
105 PropertyValuesHolder valuesHolder = mValues[0];
106 String oldName = valuesHolder.getPropertyName();
107 valuesHolder.setProperty(property);
108 mValuesMap.remove(oldName);
109 mValuesMap.put(mPropertyName, valuesHolder);
110 }
111 if (mProperty != null) {
112 mPropertyName = property.getName();
113 }
114 mProperty = property;
115 // New property/values/target should cause re-initialization prior to starting
116 mInitialized = false;
117 }
118
119 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700120 * Gets the name of the property that will be animated. This name will be used to derive
121 * a setter function that will be called to set animated values.
122 * For example, a property name of <code>foo</code> will result
123 * in a call to the function <code>setFoo()</code> on the target object. If either
124 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
125 * also be derived and called.
126 */
127 public String getPropertyName() {
128 return mPropertyName;
129 }
130
131 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700132 * Creates a new ObjectAnimator object. This default constructor is primarily for
Chet Haased51d3682010-08-11 19:46:48 -0700133 * use internally; the other constructors which take parameters are more generally
134 * useful.
135 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700136 public ObjectAnimator() {
Chet Haased51d3682010-08-11 19:46:48 -0700137 }
138
139 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700140 * Private utility constructor that initializes the target object and name of the
141 * property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700142 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800143 * @param target The object whose property is to be animated. This object should
144 * have a public method on it called <code>setName()</code>, where <code>name</code> is
145 * the value of the <code>propertyName</code> parameter.
Chet Haased953d082010-08-16 17:44:28 -0700146 * @param propertyName The name of the property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700147 */
Chet Haase2794eb32010-10-12 16:29:28 -0700148 private ObjectAnimator(Object target, String propertyName) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800149 mTarget = target;
Chet Haased953d082010-08-16 17:44:28 -0700150 setPropertyName(propertyName);
Chet Haase17fb4b02010-06-28 17:55:07 -0700151 }
152
153 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700154 * Private utility constructor that initializes the target object and property being animated.
155 *
156 * @param target The object whose property is to be animated.
157 * @param property The property being animated.
158 */
159 private <T> ObjectAnimator(T target, Property<T, ?> property) {
160 mTarget = target;
161 setProperty(property);
162 }
163
164 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700165 * Constructs and returns an ObjectAnimator that animates between int values. A single
Chet Haaseb39f0512011-05-24 14:36:40 -0700166 * value implies that that value is the one being animated to. Two values imply a starting
167 * and ending values. More than two values imply a starting value, values to animate through
168 * along the way, and an ending value (these values will be distributed evenly across
169 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700170 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800171 * @param target The object whose property is to be animated. This object should
172 * have a public method on it called <code>setName()</code>, where <code>name</code> is
173 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700174 * @param propertyName The name of the property being animated.
175 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700176 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700177 */
178 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
179 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
180 anim.setIntValues(values);
181 return anim;
182 }
183
184 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700185 * Constructs and returns an ObjectAnimator that animates between int values. A single
186 * value implies that that value is the one being animated to. Two values imply a starting
187 * and ending values. More than two values imply a starting value, values to animate through
188 * along the way, and an ending value (these values will be distributed evenly across
189 * the duration of the animation).
190 *
191 * @param target The object whose property is to be animated.
192 * @param property The property being animated.
193 * @param values A set of values that the animation will animate between over time.
194 * @return An ObjectAnimator object that is set up to animate between the given values.
195 */
196 public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values) {
197 ObjectAnimator anim = new ObjectAnimator(target, property);
198 anim.setIntValues(values);
199 return anim;
200 }
201
202 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700203 * Constructs and returns an ObjectAnimator that animates between float values. A single
Chet Haaseb39f0512011-05-24 14:36:40 -0700204 * value implies that that value is the one being animated to. Two values imply a starting
205 * and ending values. More than two values imply a starting value, values to animate through
206 * along the way, and an ending value (these values will be distributed evenly across
207 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700208 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800209 * @param target The object whose property is to be animated. This object should
210 * have a public method on it called <code>setName()</code>, where <code>name</code> is
211 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700212 * @param propertyName The name of the property being animated.
213 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700214 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700215 */
216 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
217 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
218 anim.setFloatValues(values);
219 return anim;
220 }
221
222 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700223 * Constructs and returns an ObjectAnimator that animates between float values. A single
224 * value implies that that value is the one being animated to. Two values imply a starting
225 * and ending values. More than two values imply a starting value, values to animate through
226 * along the way, and an ending value (these values will be distributed evenly across
227 * the duration of the animation).
228 *
229 * @param target The object whose property is to be animated.
230 * @param property The property being animated.
231 * @param values A set of values that the animation will animate between over time.
232 * @return An ObjectAnimator object that is set up to animate between the given values.
233 */
234 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property,
235 float... values) {
236 ObjectAnimator anim = new ObjectAnimator(target, property);
237 anim.setFloatValues(values);
238 return anim;
239 }
240
241 /**
242 * Constructs and returns an ObjectAnimator that animates between Object values. A single
243 * value implies that that value is the one being animated to. Two values imply a starting
244 * and ending values. More than two values imply a starting value, values to animate through
245 * along the way, and an ending value (these values will be distributed evenly across
246 * the duration of the animation).
Chet Haasefe591562010-07-27 11:15:37 -0700247 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800248 * @param target The object whose property is to be animated. This object should
Chet Haaseb39f0512011-05-24 14:36:40 -0700249 * have a public method on it called <code>setName()</code>, where <code>name</code> is
250 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700251 * @param propertyName The name of the property being animated.
252 * @param evaluator A TypeEvaluator that will be called on each animation frame to
Chet Haaseb39f0512011-05-24 14:36:40 -0700253 * provide the necessary interpolation between the Object values to derive the animated
Chet Haase2794eb32010-10-12 16:29:28 -0700254 * value.
Chet Haaseb39f0512011-05-24 14:36:40 -0700255 * @param values A set of values that the animation will animate between over time.
256 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haasefe591562010-07-27 11:15:37 -0700257 */
Chet Haase2794eb32010-10-12 16:29:28 -0700258 public static ObjectAnimator ofObject(Object target, String propertyName,
259 TypeEvaluator evaluator, Object... values) {
260 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
261 anim.setObjectValues(values);
262 anim.setEvaluator(evaluator);
263 return anim;
264 }
265
266 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700267 * Constructs and returns an ObjectAnimator that animates between Object values. A single
268 * value implies that that value is the one being animated to. Two values imply a starting
269 * and ending values. More than two values imply a starting value, values to animate through
270 * along the way, and an ending value (these values will be distributed evenly across
271 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700272 *
Chet Haaseb39f0512011-05-24 14:36:40 -0700273 * @param target The object whose property is to be animated.
274 * @param property The property being animated.
275 * @param evaluator A TypeEvaluator that will be called on each animation frame to
276 * provide the necessary interpolation between the Object values to derive the animated
277 * value.
278 * @param values A set of values that the animation will animate between over time.
279 * @return An ObjectAnimator object that is set up to animate between the given values.
280 */
281 public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property,
282 TypeEvaluator<V> evaluator, V... values) {
283 ObjectAnimator anim = new ObjectAnimator(target, property);
284 anim.setObjectValues(values);
285 anim.setEvaluator(evaluator);
286 return anim;
287 }
288
289 /**
290 * Constructs and returns an ObjectAnimator that animates between the sets of values specified
291 * in <code>PropertyValueHolder</code> objects. This variant should be used when animating
292 * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows
293 * you to associate a set of animation values with a property name.
294 *
295 * @param target The object whose property is to be animated. Depending on how the
296 * PropertyValuesObjects were constructed, the target object should either have the {@link
297 * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the
298 * PropertyValuesHOlder objects were created with property names) the target object should have
299 * public methods on it called <code>setName()</code>, where <code>name</code> is the name of
300 * the property passed in as the <code>propertyName</code> parameter for each of the
301 * PropertyValuesHolder objects.
302 * @param values A set of PropertyValuesHolder objects whose values will be animated between
303 * over time.
304 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700305 */
306 public static ObjectAnimator ofPropertyValuesHolder(Object target,
307 PropertyValuesHolder... values) {
308 ObjectAnimator anim = new ObjectAnimator();
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800309 anim.mTarget = target;
Chet Haase2794eb32010-10-12 16:29:28 -0700310 anim.setValues(values);
311 return anim;
Chet Haase3dd207a2010-07-20 14:00:01 -0700312 }
313
Romain Guy83d6e822010-10-14 10:13:53 -0700314 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700315 public void setIntValues(int... values) {
Romain Guy83d6e822010-10-14 10:13:53 -0700316 if (mValues == null || mValues.length == 0) {
317 // No values yet - this animator is being constructed piecemeal. Init the values with
318 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700319 if (mProperty != null) {
320 setValues(PropertyValuesHolder.ofInt(mProperty, values));
321 } else {
322 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
323 }
Romain Guy83d6e822010-10-14 10:13:53 -0700324 } else {
Chet Haase2794eb32010-10-12 16:29:28 -0700325 super.setIntValues(values);
326 }
327 }
328
329 @Override
330 public void setFloatValues(float... values) {
331 if (mValues == null || mValues.length == 0) {
332 // No values yet - this animator is being constructed piecemeal. Init the values with
333 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700334 if (mProperty != null) {
335 setValues(PropertyValuesHolder.ofFloat(mProperty, values));
336 } else {
337 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
338 }
Chet Haase2794eb32010-10-12 16:29:28 -0700339 } else {
340 super.setFloatValues(values);
341 }
342 }
343
344 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700345 public void setObjectValues(Object... values) {
346 if (mValues == null || mValues.length == 0) {
347 // No values yet - this animator is being constructed piecemeal. Init the values with
348 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700349 if (mProperty != null) {
Chet Haasebe19e032013-03-15 17:08:55 -0700350 setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700351 } else {
Chet Haasebe19e032013-03-15 17:08:55 -0700352 setValues(PropertyValuesHolder.ofObject(mPropertyName,
353 (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700354 }
Chet Haase2794eb32010-10-12 16:29:28 -0700355 } else {
356 super.setObjectValues(values);
Romain Guy83d6e822010-10-14 10:13:53 -0700357 }
Chet Haase0e0590b2010-09-26 11:57:28 -0700358 }
Romain Guy83d6e822010-10-14 10:13:53 -0700359
Chet Haasebe19e032013-03-15 17:08:55 -0700360 /**
361 * autoCancel controls whether an ObjectAnimator will be canceled automatically
362 * when any other ObjectAnimator with the same target and properties is started.
363 * Setting this flag may make it easier to run different animators on the same target
364 * object without having to keep track of whether there are conflicting animators that
365 * need to be manually canceled. Canceling animators must have the same exact set of
366 * target properties, in the same order.
367 *
368 * @param cancel Whether future ObjectAnimators with the same target and properties
369 * as this ObjectAnimator will cause this ObjectAnimator to be canceled.
370 */
371 public void setAutoCancel(boolean cancel) {
372 mAutoCancel = cancel;
373 }
374
375 private boolean hasSameTargetAndProperties(Animator anim) {
376 if (anim instanceof ObjectAnimator) {
377 PropertyValuesHolder[] theirValues = ((ObjectAnimator) anim).getValues();
378 if (((ObjectAnimator) anim).getTarget() == mTarget &&
379 mValues.length == theirValues.length) {
380 for (int i = 0; i < mValues.length; ++i) {
381 PropertyValuesHolder pvhMine = mValues[i];
382 PropertyValuesHolder pvhTheirs = theirValues[i];
383 if (pvhMine.getPropertyName() == null ||
384 !pvhMine.getPropertyName().equals(pvhTheirs.getPropertyName())) {
385 return false;
386 }
387 }
388 return true;
389 }
390 }
391 return false;
392 }
393
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800394 @Override
395 public void start() {
Chet Haasebe19e032013-03-15 17:08:55 -0700396 // See if any of the current active/pending animators need to be canceled
397 AnimationHandler handler = sAnimationHandler.get();
398 if (handler != null) {
399 int numAnims = handler.mAnimations.size();
400 for (int i = numAnims - 1; i >= 0; i--) {
401 if (handler.mAnimations.get(i) instanceof ObjectAnimator) {
402 ObjectAnimator anim = (ObjectAnimator) handler.mAnimations.get(i);
403 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
404 anim.cancel();
405 }
406 }
407 }
408 numAnims = handler.mPendingAnimations.size();
409 for (int i = numAnims - 1; i >= 0; i--) {
410 if (handler.mPendingAnimations.get(i) instanceof ObjectAnimator) {
411 ObjectAnimator anim = (ObjectAnimator) handler.mPendingAnimations.get(i);
412 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
413 anim.cancel();
414 }
415 }
416 }
417 numAnims = handler.mDelayedAnims.size();
418 for (int i = numAnims - 1; i >= 0; i--) {
419 if (handler.mDelayedAnims.get(i) instanceof ObjectAnimator) {
420 ObjectAnimator anim = (ObjectAnimator) handler.mDelayedAnims.get(i);
421 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
422 anim.cancel();
423 }
424 }
425 }
426 }
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800427 if (DBG) {
Chet Haase3c4ce722011-09-02 15:37:25 -0700428 Log.d("ObjectAnimator", "Anim target, duration: " + mTarget + ", " + getDuration());
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800429 for (int i = 0; i < mValues.length; ++i) {
430 PropertyValuesHolder pvh = mValues[i];
431 ArrayList<Keyframe> keyframes = pvh.mKeyframeSet.mKeyframes;
432 Log.d("ObjectAnimator", " Values[" + i + "]: " +
433 pvh.getPropertyName() + ", " + keyframes.get(0).getValue() + ", " +
434 keyframes.get(pvh.mKeyframeSet.mNumKeyframes - 1).getValue());
435 }
436 }
437 super.start();
438 }
439
Chet Haase3dd207a2010-07-20 14:00:01 -0700440 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700441 * This function is called immediately before processing the first animation
442 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
443 * function is called after that delay ends.
444 * It takes care of the final initialization steps for the
445 * animation. This includes setting mEvaluator, if the user has not yet
446 * set it up, and the setter/getter methods, if the user did not supply
447 * them.
448 *
449 * <p>Overriders of this method should call the superclass method to cause
450 * internal mechanisms to be set up correctly.</p>
451 */
452 @Override
453 void initAnimation() {
Chet Haase21cd1382010-09-01 17:42:29 -0700454 if (!mInitialized) {
455 // mValueType may change due to setter/getter setup; do this before calling super.init(),
456 // which uses mValueType to set up the default type evaluator.
457 int numValues = mValues.length;
458 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800459 mValues[i].setupSetterAndGetter(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700460 }
461 super.initAnimation();
Chet Haase17fb4b02010-06-28 17:55:07 -0700462 }
463 }
464
Chet Haase2794eb32010-10-12 16:29:28 -0700465 /**
466 * Sets the length of the animation. The default duration is 300 milliseconds.
467 *
468 * @param duration The length of the animation, in milliseconds.
469 * @return ObjectAnimator The object called with setDuration(). This return
470 * value makes it easier to compose statements together that construct and then set the
471 * duration, as in
472 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
473 */
474 @Override
475 public ObjectAnimator setDuration(long duration) {
476 super.setDuration(duration);
477 return this;
478 }
479
Chet Haase17fb4b02010-06-28 17:55:07 -0700480
481 /**
482 * The target object whose property will be animated by this animation
483 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800484 * @return The object being animated
Chet Haase17fb4b02010-06-28 17:55:07 -0700485 */
486 public Object getTarget() {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800487 return mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -0700488 }
489
490 /**
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800491 * Sets the target object whose property will be animated by this animation
Chet Haasef54a8d72010-07-22 14:44:59 -0700492 *
493 * @param target The object being animated
494 */
Chet Haase21cd1382010-09-01 17:42:29 -0700495 @Override
Chet Haasef54a8d72010-07-22 14:44:59 -0700496 public void setTarget(Object target) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800497 if (mTarget != target) {
Patrick Dubroy7beecfa2011-01-16 14:42:39 -0800498 final Object oldTarget = mTarget;
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800499 mTarget = target;
Patrick Dubroy7beecfa2011-01-16 14:42:39 -0800500 if (oldTarget != null && target != null && oldTarget.getClass() == target.getClass()) {
Chet Haase70d4ba12010-10-06 09:46:45 -0700501 return;
502 }
503 // New target type should cause re-initialization prior to starting
504 mInitialized = false;
505 }
Chet Haasef54a8d72010-07-22 14:44:59 -0700506 }
507
Chet Haase21cd1382010-09-01 17:42:29 -0700508 @Override
509 public void setupStartValues() {
510 initAnimation();
511 int numValues = mValues.length;
512 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800513 mValues[i].setupStartValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700514 }
515 }
516
517 @Override
518 public void setupEndValues() {
519 initAnimation();
520 int numValues = mValues.length;
521 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800522 mValues[i].setupEndValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700523 }
524 }
525
Chet Haasef54a8d72010-07-22 14:44:59 -0700526 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700527 * This method is called with the elapsed fraction of the animation during every
528 * animation frame. This function turns the elapsed fraction into an interpolated fraction
529 * and then into an animated value (from the evaluator. The function is called mostly during
530 * animation updates, but it is also called when the <code>end()</code>
531 * function is called, to set the final value on the property.
532 *
533 * <p>Overrides of this method must call the superclass to perform the calculation
534 * of the animated value.</p>
535 *
536 * @param fraction The elapsed fraction of the animation.
537 */
538 @Override
539 void animateValue(float fraction) {
540 super.animateValue(fraction);
Chet Haase602e4d32010-08-16 08:57:23 -0700541 int numValues = mValues.length;
542 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800543 mValues[i].setAnimatedValue(mTarget);
Chet Haase17fb4b02010-06-28 17:55:07 -0700544 }
545 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700546
547 @Override
Chet Haasea18a86b2010-09-07 13:20:00 -0700548 public ObjectAnimator clone() {
549 final ObjectAnimator anim = (ObjectAnimator) super.clone();
Chet Haase49afa5b2010-08-23 11:39:53 -0700550 return anim;
551 }
Chet Haasee9140a72011-02-16 16:23:29 -0800552
553 @Override
554 public String toString() {
555 String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " +
556 mTarget;
557 if (mValues != null) {
558 for (int i = 0; i < mValues.length; ++i) {
559 returnVal += "\n " + mValues[i].toString();
560 }
561 }
562 return returnVal;
563 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700564}