blob: 9c88ccf32d412ef89ac0296ea27bd39bfa69e83e [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.
Chet Haasefdd3ad72013-04-24 16:38:20 -0700126 *
127 * <p>If this animator was created with a {@link Property} object instead of the
128 * string name of a property, then this method will return the {@link
129 * Property#getName() name} of that Property object instead. If this animator was
130 * created with one or more {@link PropertyValuesHolder} objects, then this method
131 * will return the {@link PropertyValuesHolder#getPropertyName() name} of that
132 * object (if there was just one) or a comma-separated list of all of the
133 * names (if there are more than one).</p>
Chet Haase17fb4b02010-06-28 17:55:07 -0700134 */
135 public String getPropertyName() {
Chet Haasefdd3ad72013-04-24 16:38:20 -0700136 String propertyName = null;
137 if (mPropertyName != null) {
138 propertyName = mPropertyName;
139 } else if (mProperty != null) {
140 propertyName = mProperty.getName();
141 } else if (mValues != null && mValues.length > 0) {
142 for (int i = 0; i < mValues.length; ++i) {
143 if (i == 0) {
144 propertyName = "";
145 } else {
146 propertyName += ",";
147 }
148 propertyName += mValues[i].getPropertyName();
149 }
150 }
151 return propertyName;
152 }
153
154 @Override
155 String getNameForTrace() {
156 return "animator:" + getPropertyName();
Chet Haase17fb4b02010-06-28 17:55:07 -0700157 }
158
159 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700160 * Creates a new ObjectAnimator object. This default constructor is primarily for
Chet Haased51d3682010-08-11 19:46:48 -0700161 * use internally; the other constructors which take parameters are more generally
162 * useful.
163 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700164 public ObjectAnimator() {
Chet Haased51d3682010-08-11 19:46:48 -0700165 }
166
167 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700168 * Private utility constructor that initializes the target object and name of the
169 * property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -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 Haased953d082010-08-16 17:44:28 -0700174 * @param propertyName The name of the property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700175 */
Chet Haase2794eb32010-10-12 16:29:28 -0700176 private ObjectAnimator(Object target, String propertyName) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800177 mTarget = target;
Chet Haased953d082010-08-16 17:44:28 -0700178 setPropertyName(propertyName);
Chet Haase17fb4b02010-06-28 17:55:07 -0700179 }
180
181 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700182 * Private utility constructor that initializes the target object and property being animated.
183 *
184 * @param target The object whose property is to be animated.
185 * @param property The property being animated.
186 */
187 private <T> ObjectAnimator(T target, Property<T, ?> property) {
188 mTarget = target;
189 setProperty(property);
190 }
191
192 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700193 * Constructs and returns an ObjectAnimator that animates between int values. A single
Chet Haaseb39f0512011-05-24 14:36:40 -0700194 * value implies that that value is the one being animated to. Two values imply a starting
195 * and ending values. More than two values imply a starting value, values to animate through
196 * along the way, and an ending value (these values will be distributed evenly across
197 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700198 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800199 * @param target The object whose property is to be animated. This object should
200 * have a public method on it called <code>setName()</code>, where <code>name</code> is
201 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700202 * @param propertyName The name of the property being animated.
203 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700204 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700205 */
206 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
207 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
208 anim.setIntValues(values);
209 return anim;
210 }
211
212 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700213 * Constructs and returns an ObjectAnimator that animates between int values. A single
214 * value implies that that value is the one being animated to. Two values imply a starting
215 * and ending values. More than two values imply a starting value, values to animate through
216 * along the way, and an ending value (these values will be distributed evenly across
217 * the duration of the animation).
218 *
219 * @param target The object whose property is to be animated.
220 * @param property The property being animated.
221 * @param values A set of values that the animation will animate between over time.
222 * @return An ObjectAnimator object that is set up to animate between the given values.
223 */
224 public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values) {
225 ObjectAnimator anim = new ObjectAnimator(target, property);
226 anim.setIntValues(values);
227 return anim;
228 }
229
230 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700231 * Constructs and returns an ObjectAnimator that animates between float values. A single
Chet Haaseb39f0512011-05-24 14:36:40 -0700232 * value implies that that value is the one being animated to. Two values imply a starting
233 * and ending values. More than two values imply a starting value, values to animate through
234 * along the way, and an ending value (these values will be distributed evenly across
235 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700236 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800237 * @param target The object whose property is to be animated. This object should
238 * have a public method on it called <code>setName()</code>, where <code>name</code> is
239 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700240 * @param propertyName The name of the property being animated.
241 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700242 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700243 */
244 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
245 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
246 anim.setFloatValues(values);
247 return anim;
248 }
249
250 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700251 * Constructs and returns an ObjectAnimator that animates between float values. A single
252 * value implies that that value is the one being animated to. Two values imply a starting
253 * and ending values. More than two values imply a starting value, values to animate through
254 * along the way, and an ending value (these values will be distributed evenly across
255 * the duration of the animation).
256 *
257 * @param target The object whose property is to be animated.
258 * @param property The property being animated.
259 * @param values A set of values that the animation will animate between over time.
260 * @return An ObjectAnimator object that is set up to animate between the given values.
261 */
262 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property,
263 float... values) {
264 ObjectAnimator anim = new ObjectAnimator(target, property);
265 anim.setFloatValues(values);
266 return anim;
267 }
268
269 /**
270 * Constructs and returns an ObjectAnimator that animates between Object values. A single
271 * value implies that that value is the one being animated to. Two values imply a starting
272 * and ending values. More than two values imply a starting value, values to animate through
273 * along the way, and an ending value (these values will be distributed evenly across
274 * the duration of the animation).
Chet Haasefe591562010-07-27 11:15:37 -0700275 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800276 * @param target The object whose property is to be animated. This object should
Chet Haaseb39f0512011-05-24 14:36:40 -0700277 * have a public method on it called <code>setName()</code>, where <code>name</code> is
278 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700279 * @param propertyName The name of the property being animated.
280 * @param evaluator A TypeEvaluator that will be called on each animation frame to
Chet Haaseb39f0512011-05-24 14:36:40 -0700281 * provide the necessary interpolation between the Object values to derive the animated
Chet Haase2794eb32010-10-12 16:29:28 -0700282 * value.
Chet Haaseb39f0512011-05-24 14:36:40 -0700283 * @param values A set of values that the animation will animate between over time.
284 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haasefe591562010-07-27 11:15:37 -0700285 */
Chet Haase2794eb32010-10-12 16:29:28 -0700286 public static ObjectAnimator ofObject(Object target, String propertyName,
287 TypeEvaluator evaluator, Object... values) {
288 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
289 anim.setObjectValues(values);
290 anim.setEvaluator(evaluator);
291 return anim;
292 }
293
294 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700295 * Constructs and returns an ObjectAnimator that animates between Object values. A single
296 * value implies that that value is the one being animated to. Two values imply a starting
297 * and ending values. More than two values imply a starting value, values to animate through
298 * along the way, and an ending value (these values will be distributed evenly across
299 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700300 *
Chet Haaseb39f0512011-05-24 14:36:40 -0700301 * @param target The object whose property is to be animated.
302 * @param property The property being animated.
303 * @param evaluator A TypeEvaluator that will be called on each animation frame to
304 * provide the necessary interpolation between the Object values to derive the animated
305 * value.
306 * @param values A set of values that the animation will animate between over time.
307 * @return An ObjectAnimator object that is set up to animate between the given values.
308 */
309 public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property,
310 TypeEvaluator<V> evaluator, V... values) {
311 ObjectAnimator anim = new ObjectAnimator(target, property);
312 anim.setObjectValues(values);
313 anim.setEvaluator(evaluator);
314 return anim;
315 }
316
317 /**
318 * Constructs and returns an ObjectAnimator that animates between the sets of values specified
319 * in <code>PropertyValueHolder</code> objects. This variant should be used when animating
320 * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows
321 * you to associate a set of animation values with a property name.
322 *
323 * @param target The object whose property is to be animated. Depending on how the
324 * PropertyValuesObjects were constructed, the target object should either have the {@link
325 * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the
326 * PropertyValuesHOlder objects were created with property names) the target object should have
327 * public methods on it called <code>setName()</code>, where <code>name</code> is the name of
328 * the property passed in as the <code>propertyName</code> parameter for each of the
329 * PropertyValuesHolder objects.
330 * @param values A set of PropertyValuesHolder objects whose values will be animated between
331 * over time.
332 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700333 */
334 public static ObjectAnimator ofPropertyValuesHolder(Object target,
335 PropertyValuesHolder... values) {
336 ObjectAnimator anim = new ObjectAnimator();
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800337 anim.mTarget = target;
Chet Haase2794eb32010-10-12 16:29:28 -0700338 anim.setValues(values);
339 return anim;
Chet Haase3dd207a2010-07-20 14:00:01 -0700340 }
341
Romain Guy83d6e822010-10-14 10:13:53 -0700342 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700343 public void setIntValues(int... values) {
Romain Guy83d6e822010-10-14 10:13:53 -0700344 if (mValues == null || mValues.length == 0) {
345 // No values yet - this animator is being constructed piecemeal. Init the values with
346 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700347 if (mProperty != null) {
348 setValues(PropertyValuesHolder.ofInt(mProperty, values));
349 } else {
350 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
351 }
Romain Guy83d6e822010-10-14 10:13:53 -0700352 } else {
Chet Haase2794eb32010-10-12 16:29:28 -0700353 super.setIntValues(values);
354 }
355 }
356
357 @Override
358 public void setFloatValues(float... values) {
359 if (mValues == null || mValues.length == 0) {
360 // No values yet - this animator is being constructed piecemeal. Init the values with
361 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700362 if (mProperty != null) {
363 setValues(PropertyValuesHolder.ofFloat(mProperty, values));
364 } else {
365 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
366 }
Chet Haase2794eb32010-10-12 16:29:28 -0700367 } else {
368 super.setFloatValues(values);
369 }
370 }
371
372 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700373 public void setObjectValues(Object... values) {
374 if (mValues == null || mValues.length == 0) {
375 // No values yet - this animator is being constructed piecemeal. Init the values with
376 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700377 if (mProperty != null) {
Chet Haasebe19e032013-03-15 17:08:55 -0700378 setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700379 } else {
Chet Haasebe19e032013-03-15 17:08:55 -0700380 setValues(PropertyValuesHolder.ofObject(mPropertyName,
381 (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700382 }
Chet Haase2794eb32010-10-12 16:29:28 -0700383 } else {
384 super.setObjectValues(values);
Romain Guy83d6e822010-10-14 10:13:53 -0700385 }
Chet Haase0e0590b2010-09-26 11:57:28 -0700386 }
Romain Guy83d6e822010-10-14 10:13:53 -0700387
Chet Haasebe19e032013-03-15 17:08:55 -0700388 /**
389 * autoCancel controls whether an ObjectAnimator will be canceled automatically
390 * when any other ObjectAnimator with the same target and properties is started.
391 * Setting this flag may make it easier to run different animators on the same target
392 * object without having to keep track of whether there are conflicting animators that
393 * need to be manually canceled. Canceling animators must have the same exact set of
394 * target properties, in the same order.
395 *
396 * @param cancel Whether future ObjectAnimators with the same target and properties
397 * as this ObjectAnimator will cause this ObjectAnimator to be canceled.
398 */
399 public void setAutoCancel(boolean cancel) {
400 mAutoCancel = cancel;
401 }
402
403 private boolean hasSameTargetAndProperties(Animator anim) {
404 if (anim instanceof ObjectAnimator) {
405 PropertyValuesHolder[] theirValues = ((ObjectAnimator) anim).getValues();
406 if (((ObjectAnimator) anim).getTarget() == mTarget &&
407 mValues.length == theirValues.length) {
408 for (int i = 0; i < mValues.length; ++i) {
409 PropertyValuesHolder pvhMine = mValues[i];
410 PropertyValuesHolder pvhTheirs = theirValues[i];
411 if (pvhMine.getPropertyName() == null ||
412 !pvhMine.getPropertyName().equals(pvhTheirs.getPropertyName())) {
413 return false;
414 }
415 }
416 return true;
417 }
418 }
419 return false;
420 }
421
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800422 @Override
423 public void start() {
Chet Haasebe19e032013-03-15 17:08:55 -0700424 // See if any of the current active/pending animators need to be canceled
425 AnimationHandler handler = sAnimationHandler.get();
426 if (handler != null) {
427 int numAnims = handler.mAnimations.size();
428 for (int i = numAnims - 1; i >= 0; i--) {
429 if (handler.mAnimations.get(i) instanceof ObjectAnimator) {
430 ObjectAnimator anim = (ObjectAnimator) handler.mAnimations.get(i);
431 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
432 anim.cancel();
433 }
434 }
435 }
436 numAnims = handler.mPendingAnimations.size();
437 for (int i = numAnims - 1; i >= 0; i--) {
438 if (handler.mPendingAnimations.get(i) instanceof ObjectAnimator) {
439 ObjectAnimator anim = (ObjectAnimator) handler.mPendingAnimations.get(i);
440 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
441 anim.cancel();
442 }
443 }
444 }
445 numAnims = handler.mDelayedAnims.size();
446 for (int i = numAnims - 1; i >= 0; i--) {
447 if (handler.mDelayedAnims.get(i) instanceof ObjectAnimator) {
448 ObjectAnimator anim = (ObjectAnimator) handler.mDelayedAnims.get(i);
449 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
450 anim.cancel();
451 }
452 }
453 }
454 }
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800455 if (DBG) {
Chet Haase3c4ce722011-09-02 15:37:25 -0700456 Log.d("ObjectAnimator", "Anim target, duration: " + mTarget + ", " + getDuration());
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800457 for (int i = 0; i < mValues.length; ++i) {
458 PropertyValuesHolder pvh = mValues[i];
459 ArrayList<Keyframe> keyframes = pvh.mKeyframeSet.mKeyframes;
460 Log.d("ObjectAnimator", " Values[" + i + "]: " +
461 pvh.getPropertyName() + ", " + keyframes.get(0).getValue() + ", " +
462 keyframes.get(pvh.mKeyframeSet.mNumKeyframes - 1).getValue());
463 }
464 }
465 super.start();
466 }
467
Chet Haase3dd207a2010-07-20 14:00:01 -0700468 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700469 * This function is called immediately before processing the first animation
470 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
471 * function is called after that delay ends.
472 * It takes care of the final initialization steps for the
473 * animation. This includes setting mEvaluator, if the user has not yet
474 * set it up, and the setter/getter methods, if the user did not supply
475 * them.
476 *
477 * <p>Overriders of this method should call the superclass method to cause
478 * internal mechanisms to be set up correctly.</p>
479 */
480 @Override
481 void initAnimation() {
Chet Haase21cd1382010-09-01 17:42:29 -0700482 if (!mInitialized) {
483 // mValueType may change due to setter/getter setup; do this before calling super.init(),
484 // which uses mValueType to set up the default type evaluator.
485 int numValues = mValues.length;
486 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800487 mValues[i].setupSetterAndGetter(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700488 }
489 super.initAnimation();
Chet Haase17fb4b02010-06-28 17:55:07 -0700490 }
491 }
492
Chet Haase2794eb32010-10-12 16:29:28 -0700493 /**
494 * Sets the length of the animation. The default duration is 300 milliseconds.
495 *
496 * @param duration The length of the animation, in milliseconds.
497 * @return ObjectAnimator The object called with setDuration(). This return
498 * value makes it easier to compose statements together that construct and then set the
499 * duration, as in
500 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
501 */
502 @Override
503 public ObjectAnimator setDuration(long duration) {
504 super.setDuration(duration);
505 return this;
506 }
507
Chet Haase17fb4b02010-06-28 17:55:07 -0700508
509 /**
510 * The target object whose property will be animated by this animation
511 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800512 * @return The object being animated
Chet Haase17fb4b02010-06-28 17:55:07 -0700513 */
514 public Object getTarget() {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800515 return mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -0700516 }
517
518 /**
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800519 * Sets the target object whose property will be animated by this animation
Chet Haasef54a8d72010-07-22 14:44:59 -0700520 *
521 * @param target The object being animated
522 */
Chet Haase21cd1382010-09-01 17:42:29 -0700523 @Override
Chet Haasef54a8d72010-07-22 14:44:59 -0700524 public void setTarget(Object target) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800525 if (mTarget != target) {
Patrick Dubroy7beecfa2011-01-16 14:42:39 -0800526 final Object oldTarget = mTarget;
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800527 mTarget = target;
Patrick Dubroy7beecfa2011-01-16 14:42:39 -0800528 if (oldTarget != null && target != null && oldTarget.getClass() == target.getClass()) {
Chet Haase70d4ba12010-10-06 09:46:45 -0700529 return;
530 }
531 // New target type should cause re-initialization prior to starting
532 mInitialized = false;
533 }
Chet Haasef54a8d72010-07-22 14:44:59 -0700534 }
535
Chet Haase21cd1382010-09-01 17:42:29 -0700536 @Override
537 public void setupStartValues() {
538 initAnimation();
539 int numValues = mValues.length;
540 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800541 mValues[i].setupStartValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700542 }
543 }
544
545 @Override
546 public void setupEndValues() {
547 initAnimation();
548 int numValues = mValues.length;
549 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800550 mValues[i].setupEndValue(mTarget);
Chet Haase21cd1382010-09-01 17:42:29 -0700551 }
552 }
553
Chet Haasef54a8d72010-07-22 14:44:59 -0700554 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700555 * This method is called with the elapsed fraction of the animation during every
556 * animation frame. This function turns the elapsed fraction into an interpolated fraction
557 * and then into an animated value (from the evaluator. The function is called mostly during
558 * animation updates, but it is also called when the <code>end()</code>
559 * function is called, to set the final value on the property.
560 *
561 * <p>Overrides of this method must call the superclass to perform the calculation
562 * of the animated value.</p>
563 *
564 * @param fraction The elapsed fraction of the animation.
565 */
566 @Override
567 void animateValue(float fraction) {
568 super.animateValue(fraction);
Chet Haase602e4d32010-08-16 08:57:23 -0700569 int numValues = mValues.length;
570 for (int i = 0; i < numValues; ++i) {
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800571 mValues[i].setAnimatedValue(mTarget);
Chet Haase17fb4b02010-06-28 17:55:07 -0700572 }
573 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700574
575 @Override
Chet Haasea18a86b2010-09-07 13:20:00 -0700576 public ObjectAnimator clone() {
577 final ObjectAnimator anim = (ObjectAnimator) super.clone();
Chet Haase49afa5b2010-08-23 11:39:53 -0700578 return anim;
579 }
Chet Haasee9140a72011-02-16 16:23:29 -0800580
581 @Override
582 public String toString() {
583 String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " +
584 mTarget;
585 if (mValues != null) {
586 for (int i = 0; i < mValues.length; ++i) {
587 returnVal += "\n " + mValues[i].toString();
588 }
589 }
590 return returnVal;
591 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700592}