blob: 500634ceab83747f283add0326815eec45fc3ba0 [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
Alan Viverette87ac5f62014-06-04 16:39:21 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
George Mountc96c7b22013-08-23 13:31:31 -070021import android.graphics.Path;
22import android.graphics.PointF;
Chet Haase17fb4b02010-06-28 17:55:07 -070023import android.util.Log;
Chet Haaseb39f0512011-05-24 14:36:40 -070024import android.util.Property;
Chet Haase17fb4b02010-06-28 17:55:07 -070025
Alan Viverette87ac5f62014-06-04 16:39:21 -070026import java.lang.ref.WeakReference;
Chet Haasee2ab7cc2010-12-06 16:10:07 -080027import java.util.ArrayList;
Chet Haase17fb4b02010-06-28 17:55:07 -070028
29/**
Chet Haasea18a86b2010-09-07 13:20:00 -070030 * This subclass of {@link ValueAnimator} provides support for animating properties on target objects.
Chet Haase17fb4b02010-06-28 17:55:07 -070031 * The constructors of this class take parameters to define the target object that will be animated
32 * as well as the name of the property that will be animated. Appropriate set/get functions
33 * are then determined internally and the animation will call these functions as necessary to
34 * animate the property.
Chet Haase6e0ecb42010-11-03 19:41:18 -070035 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080036 * <div class="special reference">
37 * <h3>Developer Guides</h3>
38 * <p>For more information about animating with {@code ObjectAnimator}, read the
39 * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#object-animator">Property
40 * Animation</a> developer guide.</p>
41 * </div>
42 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070043 * @see #setPropertyName(String)
44 *
Chet Haase17fb4b02010-06-28 17:55:07 -070045 */
Chet Haase2794eb32010-10-12 16:29:28 -070046public final class ObjectAnimator extends ValueAnimator {
Alan Viverette87ac5f62014-06-04 16:39:21 -070047 private static final String LOG_TAG = "ObjectAnimator";
48
Chet Haasee2ab7cc2010-12-06 16:10:07 -080049 private static final boolean DBG = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070050
Alan Viverette87ac5f62014-06-04 16:39:21 -070051 /**
52 * A weak reference to the target object on which the property exists, set
53 * in the constructor. We'll cancel the animation if this goes away.
54 */
55 private WeakReference<Object> mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -070056
57 private String mPropertyName;
58
Chet Haaseb39f0512011-05-24 14:36:40 -070059 private Property mProperty;
60
Chet Haasebe19e032013-03-15 17:08:55 -070061 private boolean mAutoCancel = false;
62
Chet Haase17fb4b02010-06-28 17:55:07 -070063 /**
64 * Sets the name of the property that will be animated. This name is used to derive
65 * a setter function that will be called to set animated values.
66 * For example, a property name of <code>foo</code> will result
67 * in a call to the function <code>setFoo()</code> on the target object. If either
68 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
69 * also be derived and called.
70 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070071 * <p>For best performance of the mechanism that calls the setter function determined by the
72 * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
73 * and make the setter function for those properties have a <code>void</code> return value. This
74 * will cause the code to take an optimized path for these constrained circumstances. Other
75 * property types and return types will work, but will have more overhead in processing
76 * the requests due to normal reflection mechanisms.</p>
77 *
Chet Haase17fb4b02010-06-28 17:55:07 -070078 * <p>Note that the setter function derived from this property name
79 * must take the same parameter type as the
80 * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
81 * the setter function will fail.</p>
82 *
Chet Haasea18a86b2010-09-07 13:20:00 -070083 * <p>If this ObjectAnimator has been set up to animate several properties together,
Chet Haased953d082010-08-16 17:44:28 -070084 * using more than one PropertyValuesHolder objects, then setting the propertyName simply
85 * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
86 *
Chet Haaseb39f0512011-05-24 14:36:40 -070087 * @param propertyName The name of the property being animated. Should not be null.
Chet Haase17fb4b02010-06-28 17:55:07 -070088 */
Alan Viverette87ac5f62014-06-04 16:39:21 -070089 public void setPropertyName(@NonNull String propertyName) {
Chet Haase0e0590b2010-09-26 11:57:28 -070090 // mValues could be null if this is being constructed piecemeal. Just record the
91 // propertyName to be used later when setValues() is called if so.
Chet Haased953d082010-08-16 17:44:28 -070092 if (mValues != null) {
Chet Haase602e4d32010-08-16 08:57:23 -070093 PropertyValuesHolder valuesHolder = mValues[0];
94 String oldName = valuesHolder.getPropertyName();
Chet Haased953d082010-08-16 17:44:28 -070095 valuesHolder.setPropertyName(propertyName);
Chet Haase602e4d32010-08-16 08:57:23 -070096 mValuesMap.remove(oldName);
97 mValuesMap.put(propertyName, valuesHolder);
Chet Haased953d082010-08-16 17:44:28 -070098 }
Chet Haase17fb4b02010-06-28 17:55:07 -070099 mPropertyName = propertyName;
Chet Haase0e0590b2010-09-26 11:57:28 -0700100 // New property/values/target should cause re-initialization prior to starting
101 mInitialized = false;
Chet Haase17fb4b02010-06-28 17:55:07 -0700102 }
103
104 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700105 * Sets the property that will be animated. Property objects will take precedence over
106 * properties specified by the {@link #setPropertyName(String)} method. Animations should
107 * be set up to use one or the other, not both.
108 *
109 * @param property The property being animated. Should not be null.
110 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700111 public void setProperty(@NonNull Property property) {
Chet Haaseb39f0512011-05-24 14:36:40 -0700112 // mValues could be null if this is being constructed piecemeal. Just record the
113 // propertyName to be used later when setValues() is called if so.
114 if (mValues != null) {
115 PropertyValuesHolder valuesHolder = mValues[0];
116 String oldName = valuesHolder.getPropertyName();
117 valuesHolder.setProperty(property);
118 mValuesMap.remove(oldName);
119 mValuesMap.put(mPropertyName, valuesHolder);
120 }
121 if (mProperty != null) {
122 mPropertyName = property.getName();
123 }
124 mProperty = property;
125 // New property/values/target should cause re-initialization prior to starting
126 mInitialized = false;
127 }
128
129 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700130 * Gets the name of the property that will be animated. This name will be used to derive
131 * a setter function that will be called to set animated values.
132 * For example, a property name of <code>foo</code> will result
133 * in a call to the function <code>setFoo()</code> on the target object. If either
134 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
135 * also be derived and called.
Chet Haasefdd3ad72013-04-24 16:38:20 -0700136 *
137 * <p>If this animator was created with a {@link Property} object instead of the
138 * string name of a property, then this method will return the {@link
139 * Property#getName() name} of that Property object instead. If this animator was
140 * created with one or more {@link PropertyValuesHolder} objects, then this method
141 * will return the {@link PropertyValuesHolder#getPropertyName() name} of that
142 * object (if there was just one) or a comma-separated list of all of the
143 * names (if there are more than one).</p>
Chet Haase17fb4b02010-06-28 17:55:07 -0700144 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700145 @Nullable
Chet Haase17fb4b02010-06-28 17:55:07 -0700146 public String getPropertyName() {
Chet Haasefdd3ad72013-04-24 16:38:20 -0700147 String propertyName = null;
148 if (mPropertyName != null) {
149 propertyName = mPropertyName;
150 } else if (mProperty != null) {
151 propertyName = mProperty.getName();
152 } else if (mValues != null && mValues.length > 0) {
153 for (int i = 0; i < mValues.length; ++i) {
154 if (i == 0) {
155 propertyName = "";
156 } else {
157 propertyName += ",";
158 }
159 propertyName += mValues[i].getPropertyName();
160 }
161 }
162 return propertyName;
163 }
164
165 @Override
166 String getNameForTrace() {
167 return "animator:" + getPropertyName();
Chet Haase17fb4b02010-06-28 17:55:07 -0700168 }
169
170 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700171 * Creates a new ObjectAnimator object. This default constructor is primarily for
Chet Haased51d3682010-08-11 19:46:48 -0700172 * use internally; the other constructors which take parameters are more generally
173 * useful.
174 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700175 public ObjectAnimator() {
Chet Haased51d3682010-08-11 19:46:48 -0700176 }
177
178 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700179 * Private utility constructor that initializes the target object and name of the
180 * property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700181 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800182 * @param target The object whose property is to be animated. This object should
183 * have a public method on it called <code>setName()</code>, where <code>name</code> is
184 * the value of the <code>propertyName</code> parameter.
Chet Haased953d082010-08-16 17:44:28 -0700185 * @param propertyName The name of the property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700186 */
Chet Haase2794eb32010-10-12 16:29:28 -0700187 private ObjectAnimator(Object target, String propertyName) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700188 setTarget(target);
Chet Haased953d082010-08-16 17:44:28 -0700189 setPropertyName(propertyName);
Chet Haase17fb4b02010-06-28 17:55:07 -0700190 }
191
192 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700193 * Private utility constructor that initializes the target object and property being animated.
194 *
195 * @param target The object whose property is to be animated.
196 * @param property The property being animated.
197 */
198 private <T> ObjectAnimator(T target, Property<T, ?> property) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700199 setTarget(target);
Chet Haaseb39f0512011-05-24 14:36:40 -0700200 setProperty(property);
201 }
202
203 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700204 * Constructs and returns an ObjectAnimator that animates between int values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700205 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700206 * and ending values. More than two values imply a starting value, values to animate through
207 * along the way, and an ending value (these values will be distributed evenly across
208 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700209 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800210 * @param target The object whose property is to be animated. This object should
211 * have a public method on it called <code>setName()</code>, where <code>name</code> is
212 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700213 * @param propertyName The name of the property being animated.
214 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700215 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700216 */
217 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
218 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
219 anim.setIntValues(values);
220 return anim;
221 }
222
223 /**
George Mountc96c7b22013-08-23 13:31:31 -0700224 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
225 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
226 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
227 * coordinates are integers that are set to separate properties designated by
228 * <code>xPropertyName</code> and <code>yPropertyName</code>.
229 *
230 * @param target The object whose properties are to be animated. This object should
231 * have public methods on it called <code>setNameX()</code> and
232 * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code>
233 * are the value of <code>xPropertyName</code> and <code>yPropertyName</code>
234 * parameters, respectively.
235 * @param xPropertyName The name of the property for the x coordinate being animated.
236 * @param yPropertyName The name of the property for the y coordinate being animated.
237 * @param path The <code>Path</code> to animate values along.
238 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
239 */
240 public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName,
241 Path path) {
George Mount984011f2014-08-21 14:28:01 -0700242 PathKeyframes keyframes = KeyframeSet.ofPath(path);
243 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xPropertyName,
244 keyframes.createXIntKeyframes());
245 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yPropertyName,
246 keyframes.createYIntKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700247 return ofPropertyValuesHolder(target, x, y);
248 }
249
250 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700251 * Constructs and returns an ObjectAnimator that animates between int values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700252 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700253 * 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 ofInt(T target, Property<T, Integer> property, int... values) {
263 ObjectAnimator anim = new ObjectAnimator(target, property);
264 anim.setIntValues(values);
265 return anim;
266 }
267
268 /**
George Mountc96c7b22013-08-23 13:31:31 -0700269 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
270 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
271 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
272 * coordinates are integers that are set to separate properties, <code>xProperty</code> and
273 * <code>yProperty</code>.
274 *
275 * @param target The object whose properties are to be animated.
276 * @param xProperty The property for the x coordinate being animated.
277 * @param yProperty The property for the y coordinate being animated.
278 * @param path The <code>Path</code> to animate values along.
279 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
280 */
281 public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> xProperty,
282 Property<T, Integer> yProperty, Path path) {
George Mount984011f2014-08-21 14:28:01 -0700283 PathKeyframes keyframes = KeyframeSet.ofPath(path);
284 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xProperty,
285 keyframes.createXIntKeyframes());
286 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yProperty,
287 keyframes.createYIntKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700288 return ofPropertyValuesHolder(target, x, y);
289 }
290
291 /**
George Mount4eed5292013-08-30 13:56:01 -0700292 * Constructs and returns an ObjectAnimator that animates over int values for a multiple
293 * parameters setter. Only public methods that take only int parameters are supported.
294 * Each <code>int[]</code> contains a complete set of parameters to the setter method.
295 * At least two <code>int[]</code> values must be provided, a start and end. More than two
296 * values imply a starting value, values to animate through along the way, and an ending
297 * value (these values will be distributed evenly across the duration of the animation).
298 *
299 * @param target The object whose property is to be animated. This object may
300 * have a public method on it called <code>setName()</code>, where <code>name</code> is
301 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
302 * be the case-sensitive complete name of the public setter method.
303 * @param propertyName The name of the property being animated or the name of the setter method.
304 * @param values A set of values that the animation will animate between over time.
305 * @return An ObjectAnimator object that is set up to animate between the given values.
306 */
307 public static ObjectAnimator ofMultiInt(Object target, String propertyName, int[][] values) {
308 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, values);
309 return ofPropertyValuesHolder(target, pvh);
310 }
311
312 /**
George Mountc96c7b22013-08-23 13:31:31 -0700313 * Constructs and returns an ObjectAnimator that animates the target using a multi-int setter
314 * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions,
315 * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the
316 * coordinates are integer x and y coordinates used in the first and second parameter of the
317 * setter, respectively.
318 *
319 * @param target The object whose property is to be animated. This object may
320 * have a public method on it called <code>setName()</code>, where <code>name</code> is
321 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
322 * be the case-sensitive complete name of the public setter method.
323 * @param propertyName The name of the property being animated or the name of the setter method.
324 * @param path The <code>Path</code> to animate values along.
325 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
326 */
327 public static ObjectAnimator ofMultiInt(Object target, String propertyName, Path path) {
328 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, path);
329 return ofPropertyValuesHolder(target, pvh);
330 }
331
332 /**
George Mount4eed5292013-08-30 13:56:01 -0700333 * Constructs and returns an ObjectAnimator that animates over values for a multiple int
334 * parameters setter. Only public methods that take only int parameters are supported.
335 * <p>At least two values must be provided, a start and end. More than two
336 * values imply a starting value, values to animate through along the way, and an ending
337 * value (these values will be distributed evenly across the duration of the animation).</p>
338 *
339 * @param target The object whose property is to be animated. This object may
340 * have a public method on it called <code>setName()</code>, where <code>name</code> is
341 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
George Mountc96c7b22013-08-23 13:31:31 -0700342 * be the case-sensitive complete name of the public setter method.
George Mount4eed5292013-08-30 13:56:01 -0700343 * @param propertyName The name of the property being animated or the name of the setter method.
344 * @param converter Converts T objects into int parameters for the multi-value setter.
345 * @param evaluator A TypeEvaluator that will be called on each animation frame to
346 * provide the necessary interpolation between the Object values to derive the animated
347 * value.
348 * @param values A set of values that the animation will animate between over time.
349 * @return An ObjectAnimator object that is set up to animate between the given values.
350 */
351 public static <T> ObjectAnimator ofMultiInt(Object target, String propertyName,
352 TypeConverter<T, int[]> converter, TypeEvaluator<T> evaluator, T... values) {
353 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, converter,
354 evaluator, values);
355 return ObjectAnimator.ofPropertyValuesHolder(target, pvh);
356 }
357
358 /**
George Mount1ffb2802013-10-09 16:13:54 -0700359 * Constructs and returns an ObjectAnimator that animates between color values. A single
360 * value implies that that value is the one being animated to. Two values imply starting
361 * and ending values. More than two values imply a starting value, values to animate through
362 * along the way, and an ending value (these values will be distributed evenly across
363 * the duration of the animation).
364 *
365 * @param target The object whose property is to be animated. This object should
366 * have a public method on it called <code>setName()</code>, where <code>name</code> is
367 * the value of the <code>propertyName</code> parameter.
368 * @param propertyName The name of the property being animated.
369 * @param values A set of values that the animation will animate between over time.
370 * @return An ObjectAnimator object that is set up to animate between the given values.
371 */
372 public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) {
373 ObjectAnimator animator = ofInt(target, propertyName, values);
374 animator.setEvaluator(ArgbEvaluator.getInstance());
375 return animator;
376 }
377
378 /**
379 * Constructs and returns an ObjectAnimator that animates between color values. A single
380 * value implies that that value is the one being animated to. Two values imply starting
381 * and ending values. More than two values imply a starting value, values to animate through
382 * along the way, and an ending value (these values will be distributed evenly across
383 * the duration of the animation).
384 *
385 * @param target The object whose property is to be animated.
386 * @param property The property being animated.
387 * @param values A set of values that the animation will animate between over time.
388 * @return An ObjectAnimator object that is set up to animate between the given values.
389 */
390 public static <T> ObjectAnimator ofArgb(T target, Property<T, Integer> property,
391 int... values) {
392 ObjectAnimator animator = ofInt(target, property, values);
393 animator.setEvaluator(ArgbEvaluator.getInstance());
394 return animator;
395 }
396
397 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700398 * Constructs and returns an ObjectAnimator that animates between float values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700399 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700400 * and ending values. More than two values imply a starting value, values to animate through
401 * along the way, and an ending value (these values will be distributed evenly across
402 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700403 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800404 * @param target The object whose property is to be animated. This object should
405 * have a public method on it called <code>setName()</code>, where <code>name</code> is
406 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700407 * @param propertyName The name of the property being animated.
408 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700409 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700410 */
411 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
412 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
413 anim.setFloatValues(values);
414 return anim;
415 }
416
417 /**
George Mountc96c7b22013-08-23 13:31:31 -0700418 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
419 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
420 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
421 * coordinates are floats that are set to separate properties designated by
422 * <code>xPropertyName</code> and <code>yPropertyName</code>.
423 *
424 * @param target The object whose properties are to be animated. This object should
425 * have public methods on it called <code>setNameX()</code> and
426 * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code>
427 * are the value of the <code>xPropertyName</code> and <code>yPropertyName</code>
428 * parameters, respectively.
429 * @param xPropertyName The name of the property for the x coordinate being animated.
430 * @param yPropertyName The name of the property for the y coordinate being animated.
431 * @param path The <code>Path</code> to animate values along.
432 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
433 */
434 public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName,
435 Path path) {
George Mount984011f2014-08-21 14:28:01 -0700436 PathKeyframes keyframes = KeyframeSet.ofPath(path);
437 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xPropertyName,
438 keyframes.createXFloatKeyframes());
439 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yPropertyName,
440 keyframes.createYFloatKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700441 return ofPropertyValuesHolder(target, x, y);
442 }
443
444 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700445 * Constructs and returns an ObjectAnimator that animates between float values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700446 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700447 * and ending values. More than two values imply a starting value, values to animate through
448 * along the way, and an ending value (these values will be distributed evenly across
449 * the duration of the animation).
450 *
451 * @param target The object whose property is to be animated.
452 * @param property The property being animated.
453 * @param values A set of values that the animation will animate between over time.
454 * @return An ObjectAnimator object that is set up to animate between the given values.
455 */
456 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property,
457 float... values) {
458 ObjectAnimator anim = new ObjectAnimator(target, property);
459 anim.setFloatValues(values);
460 return anim;
461 }
462
463 /**
George Mountc96c7b22013-08-23 13:31:31 -0700464 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
465 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
466 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
467 * coordinates are floats that are set to separate properties, <code>xProperty</code> and
468 * <code>yProperty</code>.
469 *
470 * @param target The object whose properties are to be animated.
471 * @param xProperty The property for the x coordinate being animated.
472 * @param yProperty The property for the y coordinate being animated.
473 * @param path The <code>Path</code> to animate values along.
474 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
475 */
476 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> xProperty,
477 Property<T, Float> yProperty, Path path) {
George Mount984011f2014-08-21 14:28:01 -0700478 PathKeyframes keyframes = KeyframeSet.ofPath(path);
479 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xProperty,
480 keyframes.createXFloatKeyframes());
481 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yProperty,
482 keyframes.createYFloatKeyframes());
George Mountf505b1f2014-06-23 10:17:34 -0700483 return ofPropertyValuesHolder(target, x, y);
George Mountc96c7b22013-08-23 13:31:31 -0700484 }
485
486 /**
George Mount4eed5292013-08-30 13:56:01 -0700487 * Constructs and returns an ObjectAnimator that animates over float values for a multiple
488 * parameters setter. Only public methods that take only float parameters are supported.
489 * Each <code>float[]</code> contains a complete set of parameters to the setter method.
490 * At least two <code>float[]</code> values must be provided, a start and end. More than two
491 * values imply a starting value, values to animate through along the way, and an ending
492 * value (these values will be distributed evenly across the duration of the animation).
493 *
494 * @param target The object whose property is to be animated. This object may
495 * have a public method on it called <code>setName()</code>, where <code>name</code> is
496 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
George Mountc96c7b22013-08-23 13:31:31 -0700497 * be the case-sensitive complete name of the public setter method.
George Mount4eed5292013-08-30 13:56:01 -0700498 * @param propertyName The name of the property being animated or the name of the setter method.
499 * @param values A set of values that the animation will animate between over time.
500 * @return An ObjectAnimator object that is set up to animate between the given values.
501 */
502 public static ObjectAnimator ofMultiFloat(Object target, String propertyName,
503 float[][] values) {
504 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, values);
505 return ofPropertyValuesHolder(target, pvh);
506 }
507
508 /**
George Mountc96c7b22013-08-23 13:31:31 -0700509 * Constructs and returns an ObjectAnimator that animates the target using a multi-float setter
510 * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions,
511 * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the
512 * coordinates are float x and y coordinates used in the first and second parameter of the
513 * setter, respectively.
514 *
515 * @param target The object whose property is to be animated. This object may
516 * have a public method on it called <code>setName()</code>, where <code>name</code> is
517 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
518 * be the case-sensitive complete name of the public setter method.
519 * @param propertyName The name of the property being animated or the name of the setter method.
520 * @param path The <code>Path</code> to animate values along.
521 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
522 */
523 public static ObjectAnimator ofMultiFloat(Object target, String propertyName, Path path) {
524 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, path);
525 return ofPropertyValuesHolder(target, pvh);
526 }
527
528 /**
George Mount4eed5292013-08-30 13:56:01 -0700529 * Constructs and returns an ObjectAnimator that animates over values for a multiple float
530 * parameters setter. Only public methods that take only float parameters are supported.
531 * <p>At least two values must be provided, a start and end. More than two
532 * values imply a starting value, values to animate through along the way, and an ending
533 * value (these values will be distributed evenly across the duration of the animation).</p>
534 *
535 * @param target The object whose property is to be animated. This object may
536 * have a public method on it called <code>setName()</code>, where <code>name</code> is
537 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
538 * be the case-sensitive complete name of the public setter method.
539 * @param propertyName The name of the property being animated or the name of the setter method.
540 * @param converter Converts T objects into float parameters for the multi-value setter.
541 * @param evaluator A TypeEvaluator that will be called on each animation frame to
542 * provide the necessary interpolation between the Object values to derive the animated
543 * value.
544 * @param values A set of values that the animation will animate between over time.
545 * @return An ObjectAnimator object that is set up to animate between the given values.
546 */
547 public static <T> ObjectAnimator ofMultiFloat(Object target, String propertyName,
548 TypeConverter<T, float[]> converter, TypeEvaluator<T> evaluator, T... values) {
549 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, converter,
550 evaluator, values);
551 return ObjectAnimator.ofPropertyValuesHolder(target, pvh);
552 }
553
554 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700555 * Constructs and returns an ObjectAnimator that animates between Object values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700556 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700557 * and ending values. More than two values imply a starting value, values to animate through
558 * along the way, and an ending value (these values will be distributed evenly across
559 * the duration of the animation).
Chet Haasefe591562010-07-27 11:15:37 -0700560 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800561 * @param target The object whose property is to be animated. This object should
Chet Haaseb39f0512011-05-24 14:36:40 -0700562 * have a public method on it called <code>setName()</code>, where <code>name</code> is
563 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700564 * @param propertyName The name of the property being animated.
565 * @param evaluator A TypeEvaluator that will be called on each animation frame to
Chet Haaseb39f0512011-05-24 14:36:40 -0700566 * provide the necessary interpolation between the Object values to derive the animated
Chet Haase2794eb32010-10-12 16:29:28 -0700567 * value.
Chet Haaseb39f0512011-05-24 14:36:40 -0700568 * @param values A set of values that the animation will animate between over time.
569 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haasefe591562010-07-27 11:15:37 -0700570 */
Chet Haase2794eb32010-10-12 16:29:28 -0700571 public static ObjectAnimator ofObject(Object target, String propertyName,
572 TypeEvaluator evaluator, Object... values) {
573 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
574 anim.setObjectValues(values);
575 anim.setEvaluator(evaluator);
576 return anim;
577 }
578
579 /**
George Mountc96c7b22013-08-23 13:31:31 -0700580 * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>.
581 * A <code>Path</code></> animation moves in two dimensions, animating coordinates
582 * <code>(x, y)</code> together to follow the line. This variant animates the coordinates
583 * in a <code>PointF</code> to follow the <code>Path</code>. If the <code>Property</code>
584 * associated with <code>propertyName</code> uses a type other than <code>PointF</code>,
585 * <code>converter</code> can be used to change from <code>PointF</code> to the type
586 * associated with the <code>Property</code>.
587 *
588 * @param target The object whose property is to be animated. This object should
589 * have a public method on it called <code>setName()</code>, where <code>name</code> is
590 * the value of the <code>propertyName</code> parameter.
591 * @param propertyName The name of the property being animated.
592 * @param converter Converts a PointF to the type associated with the setter. May be
593 * null if conversion is unnecessary.
594 * @param path The <code>Path</code> to animate values along.
595 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
596 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700597 @NonNull
George Mountc96c7b22013-08-23 13:31:31 -0700598 public static ObjectAnimator ofObject(Object target, String propertyName,
Alan Viverette87ac5f62014-06-04 16:39:21 -0700599 @Nullable TypeConverter<PointF, ?> converter, Path path) {
George Mountc96c7b22013-08-23 13:31:31 -0700600 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(propertyName, converter, path);
601 return ofPropertyValuesHolder(target, pvh);
602 }
603
604 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700605 * Constructs and returns an ObjectAnimator that animates between Object values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700606 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700607 * and ending values. More than two values imply a starting value, values to animate through
608 * along the way, and an ending value (these values will be distributed evenly across
609 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700610 *
Chet Haaseb39f0512011-05-24 14:36:40 -0700611 * @param target The object whose property is to be animated.
612 * @param property The property being animated.
613 * @param evaluator A TypeEvaluator that will be called on each animation frame to
614 * provide the necessary interpolation between the Object values to derive the animated
615 * value.
616 * @param values A set of values that the animation will animate between over time.
617 * @return An ObjectAnimator object that is set up to animate between the given values.
618 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700619 @NonNull
Chet Haaseb39f0512011-05-24 14:36:40 -0700620 public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property,
621 TypeEvaluator<V> evaluator, V... values) {
622 ObjectAnimator anim = new ObjectAnimator(target, property);
623 anim.setObjectValues(values);
624 anim.setEvaluator(evaluator);
625 return anim;
626 }
627
628 /**
George Mount16d2c9c2013-09-17 09:07:48 -0700629 * Constructs and returns an ObjectAnimator that animates between Object values. A single
630 * value implies that that value is the one being animated to. Two values imply starting
631 * and ending values. More than two values imply a starting value, values to animate through
632 * along the way, and an ending value (these values will be distributed evenly across
633 * the duration of the animation). This variant supplies a <code>TypeConverter</code> to
634 * convert from the animated values to the type of the property. If only one value is
George Mount42516d12014-05-19 15:49:29 -0700635 * supplied, the <code>TypeConverter</code> must be a
636 * {@link android.animation.BidirectionalTypeConverter} to retrieve the current value.
George Mount16d2c9c2013-09-17 09:07:48 -0700637 *
638 * @param target The object whose property is to be animated.
639 * @param property The property being animated.
640 * @param converter Converts the animated object to the Property type.
641 * @param evaluator A TypeEvaluator that will be called on each animation frame to
642 * provide the necessary interpolation between the Object values to derive the animated
643 * value.
644 * @param values A set of values that the animation will animate between over time.
645 * @return An ObjectAnimator object that is set up to animate between the given values.
646 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700647 @NonNull
George Mount16d2c9c2013-09-17 09:07:48 -0700648 public static <T, V, P> ObjectAnimator ofObject(T target, Property<T, P> property,
649 TypeConverter<V, P> converter, TypeEvaluator<V> evaluator, V... values) {
650 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, evaluator,
651 values);
652 return ofPropertyValuesHolder(target, pvh);
653 }
654
655 /**
George Mountc96c7b22013-08-23 13:31:31 -0700656 * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>.
657 * A <code>Path</code></> animation moves in two dimensions, animating coordinates
658 * <code>(x, y)</code> together to follow the line. This variant animates the coordinates
659 * in a <code>PointF</code> to follow the <code>Path</code>. If <code>property</code>
660 * uses a type other than <code>PointF</code>, <code>converter</code> can be used to change
661 * from <code>PointF</code> to the type associated with the <code>Property</code>.
662 *
George Mount984011f2014-08-21 14:28:01 -0700663 * <p>The PointF passed to <code>converter</code> or <code>property</code>, if
664 * <code>converter</code> is <code>null</code>, is reused on each animation frame and should
665 * not be stored by the setter or TypeConverter.</p>
666 *
George Mountc96c7b22013-08-23 13:31:31 -0700667 * @param target The object whose property is to be animated.
668 * @param property The property being animated. Should not be null.
669 * @param converter Converts a PointF to the type associated with the setter. May be
670 * null if conversion is unnecessary.
671 * @param path The <code>Path</code> to animate values along.
672 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
673 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700674 @NonNull
675 public static <T, V> ObjectAnimator ofObject(T target, @NonNull Property<T, V> property,
676 @Nullable TypeConverter<PointF, V> converter, Path path) {
George Mountc96c7b22013-08-23 13:31:31 -0700677 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, path);
678 return ofPropertyValuesHolder(target, pvh);
679 }
680
681 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700682 * Constructs and returns an ObjectAnimator that animates between the sets of values specified
683 * in <code>PropertyValueHolder</code> objects. This variant should be used when animating
684 * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows
685 * you to associate a set of animation values with a property name.
686 *
687 * @param target The object whose property is to be animated. Depending on how the
688 * PropertyValuesObjects were constructed, the target object should either have the {@link
689 * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the
690 * PropertyValuesHOlder objects were created with property names) the target object should have
691 * public methods on it called <code>setName()</code>, where <code>name</code> is the name of
692 * the property passed in as the <code>propertyName</code> parameter for each of the
693 * PropertyValuesHolder objects.
694 * @param values A set of PropertyValuesHolder objects whose values will be animated between
695 * over time.
696 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700697 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700698 @NonNull
Chet Haase2794eb32010-10-12 16:29:28 -0700699 public static ObjectAnimator ofPropertyValuesHolder(Object target,
700 PropertyValuesHolder... values) {
701 ObjectAnimator anim = new ObjectAnimator();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700702 anim.setTarget(target);
Chet Haase2794eb32010-10-12 16:29:28 -0700703 anim.setValues(values);
704 return anim;
Chet Haase3dd207a2010-07-20 14:00:01 -0700705 }
706
Romain Guy83d6e822010-10-14 10:13:53 -0700707 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700708 public void setIntValues(int... values) {
Romain Guy83d6e822010-10-14 10:13:53 -0700709 if (mValues == null || mValues.length == 0) {
710 // No values yet - this animator is being constructed piecemeal. Init the values with
711 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700712 if (mProperty != null) {
713 setValues(PropertyValuesHolder.ofInt(mProperty, values));
714 } else {
715 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
716 }
Romain Guy83d6e822010-10-14 10:13:53 -0700717 } else {
Chet Haase2794eb32010-10-12 16:29:28 -0700718 super.setIntValues(values);
719 }
720 }
721
722 @Override
723 public void setFloatValues(float... values) {
724 if (mValues == null || mValues.length == 0) {
725 // No values yet - this animator is being constructed piecemeal. Init the values with
726 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700727 if (mProperty != null) {
728 setValues(PropertyValuesHolder.ofFloat(mProperty, values));
729 } else {
730 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
731 }
Chet Haase2794eb32010-10-12 16:29:28 -0700732 } else {
733 super.setFloatValues(values);
734 }
735 }
736
737 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700738 public void setObjectValues(Object... values) {
739 if (mValues == null || mValues.length == 0) {
740 // No values yet - this animator is being constructed piecemeal. Init the values with
741 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700742 if (mProperty != null) {
Chet Haasebe19e032013-03-15 17:08:55 -0700743 setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700744 } else {
Chet Haasebe19e032013-03-15 17:08:55 -0700745 setValues(PropertyValuesHolder.ofObject(mPropertyName,
746 (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700747 }
Chet Haase2794eb32010-10-12 16:29:28 -0700748 } else {
749 super.setObjectValues(values);
Romain Guy83d6e822010-10-14 10:13:53 -0700750 }
Chet Haase0e0590b2010-09-26 11:57:28 -0700751 }
Romain Guy83d6e822010-10-14 10:13:53 -0700752
Chet Haasebe19e032013-03-15 17:08:55 -0700753 /**
754 * autoCancel controls whether an ObjectAnimator will be canceled automatically
755 * when any other ObjectAnimator with the same target and properties is started.
756 * Setting this flag may make it easier to run different animators on the same target
757 * object without having to keep track of whether there are conflicting animators that
758 * need to be manually canceled. Canceling animators must have the same exact set of
759 * target properties, in the same order.
760 *
761 * @param cancel Whether future ObjectAnimators with the same target and properties
762 * as this ObjectAnimator will cause this ObjectAnimator to be canceled.
763 */
764 public void setAutoCancel(boolean cancel) {
765 mAutoCancel = cancel;
766 }
767
Alan Viverette87ac5f62014-06-04 16:39:21 -0700768 private boolean hasSameTargetAndProperties(@Nullable Animator anim) {
Chet Haasebe19e032013-03-15 17:08:55 -0700769 if (anim instanceof ObjectAnimator) {
770 PropertyValuesHolder[] theirValues = ((ObjectAnimator) anim).getValues();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700771 if (((ObjectAnimator) anim).getTarget() == getTarget() &&
Chet Haasebe19e032013-03-15 17:08:55 -0700772 mValues.length == theirValues.length) {
773 for (int i = 0; i < mValues.length; ++i) {
774 PropertyValuesHolder pvhMine = mValues[i];
775 PropertyValuesHolder pvhTheirs = theirValues[i];
776 if (pvhMine.getPropertyName() == null ||
777 !pvhMine.getPropertyName().equals(pvhTheirs.getPropertyName())) {
778 return false;
779 }
780 }
781 return true;
782 }
783 }
784 return false;
785 }
786
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800787 @Override
788 public void start() {
Chet Haasebe19e032013-03-15 17:08:55 -0700789 // See if any of the current active/pending animators need to be canceled
790 AnimationHandler handler = sAnimationHandler.get();
791 if (handler != null) {
792 int numAnims = handler.mAnimations.size();
793 for (int i = numAnims - 1; i >= 0; i--) {
794 if (handler.mAnimations.get(i) instanceof ObjectAnimator) {
795 ObjectAnimator anim = (ObjectAnimator) handler.mAnimations.get(i);
796 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
797 anim.cancel();
798 }
799 }
800 }
801 numAnims = handler.mPendingAnimations.size();
802 for (int i = numAnims - 1; i >= 0; i--) {
803 if (handler.mPendingAnimations.get(i) instanceof ObjectAnimator) {
804 ObjectAnimator anim = (ObjectAnimator) handler.mPendingAnimations.get(i);
805 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
806 anim.cancel();
807 }
808 }
809 }
810 numAnims = handler.mDelayedAnims.size();
811 for (int i = numAnims - 1; i >= 0; i--) {
812 if (handler.mDelayedAnims.get(i) instanceof ObjectAnimator) {
813 ObjectAnimator anim = (ObjectAnimator) handler.mDelayedAnims.get(i);
814 if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
815 anim.cancel();
816 }
817 }
818 }
819 }
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800820 if (DBG) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700821 Log.d(LOG_TAG, "Anim target, duration: " + getTarget() + ", " + getDuration());
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800822 for (int i = 0; i < mValues.length; ++i) {
823 PropertyValuesHolder pvh = mValues[i];
Alan Viverette87ac5f62014-06-04 16:39:21 -0700824 Log.d(LOG_TAG, " Values[" + i + "]: " +
George Mount984011f2014-08-21 14:28:01 -0700825 pvh.getPropertyName() + ", " + pvh.mKeyframes.getValue(0) + ", " +
826 pvh.mKeyframes.getValue(1));
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800827 }
828 }
829 super.start();
830 }
831
Chet Haase3dd207a2010-07-20 14:00:01 -0700832 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700833 * This function is called immediately before processing the first animation
834 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
835 * function is called after that delay ends.
836 * It takes care of the final initialization steps for the
837 * animation. This includes setting mEvaluator, if the user has not yet
838 * set it up, and the setter/getter methods, if the user did not supply
839 * them.
840 *
841 * <p>Overriders of this method should call the superclass method to cause
842 * internal mechanisms to be set up correctly.</p>
843 */
844 @Override
845 void initAnimation() {
Chet Haase21cd1382010-09-01 17:42:29 -0700846 if (!mInitialized) {
847 // mValueType may change due to setter/getter setup; do this before calling super.init(),
848 // which uses mValueType to set up the default type evaluator.
Alan Viverette87ac5f62014-06-04 16:39:21 -0700849 final Object target = getTarget();
850 if (target != null) {
851 final int numValues = mValues.length;
852 for (int i = 0; i < numValues; ++i) {
853 mValues[i].setupSetterAndGetter(target);
854 }
Chet Haase21cd1382010-09-01 17:42:29 -0700855 }
856 super.initAnimation();
Chet Haase17fb4b02010-06-28 17:55:07 -0700857 }
858 }
859
Chet Haase2794eb32010-10-12 16:29:28 -0700860 /**
861 * Sets the length of the animation. The default duration is 300 milliseconds.
862 *
863 * @param duration The length of the animation, in milliseconds.
864 * @return ObjectAnimator The object called with setDuration(). This return
865 * value makes it easier to compose statements together that construct and then set the
866 * duration, as in
867 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
868 */
869 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700870 @NonNull
Chet Haase2794eb32010-10-12 16:29:28 -0700871 public ObjectAnimator setDuration(long duration) {
872 super.setDuration(duration);
873 return this;
874 }
875
Chet Haase17fb4b02010-06-28 17:55:07 -0700876
877 /**
878 * The target object whose property will be animated by this animation
879 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800880 * @return The object being animated
Chet Haase17fb4b02010-06-28 17:55:07 -0700881 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700882 @Nullable
Chet Haase17fb4b02010-06-28 17:55:07 -0700883 public Object getTarget() {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700884 return mTarget == null ? null : mTarget.get();
Chet Haase17fb4b02010-06-28 17:55:07 -0700885 }
886
887 /**
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800888 * Sets the target object whose property will be animated by this animation
Chet Haasef54a8d72010-07-22 14:44:59 -0700889 *
890 * @param target The object being animated
891 */
Chet Haase21cd1382010-09-01 17:42:29 -0700892 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700893 public void setTarget(@Nullable Object target) {
894 final Object oldTarget = getTarget();
895 if (oldTarget != target) {
896 mTarget = target == null ? null : new WeakReference<Object>(target);
Yigit Boyar8619f482014-07-15 17:28:07 -0700897 // New target should cause re-initialization prior to starting
Chet Haase70d4ba12010-10-06 09:46:45 -0700898 mInitialized = false;
899 }
Chet Haasef54a8d72010-07-22 14:44:59 -0700900 }
901
Chet Haase21cd1382010-09-01 17:42:29 -0700902 @Override
903 public void setupStartValues() {
904 initAnimation();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700905
906 final Object target = getTarget();
907 if (target != null) {
908 final int numValues = mValues.length;
909 for (int i = 0; i < numValues; ++i) {
910 mValues[i].setupStartValue(target);
911 }
Chet Haase21cd1382010-09-01 17:42:29 -0700912 }
913 }
914
915 @Override
916 public void setupEndValues() {
917 initAnimation();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700918
919 final Object target = getTarget();
920 if (target != null) {
921 final int numValues = mValues.length;
922 for (int i = 0; i < numValues; ++i) {
923 mValues[i].setupEndValue(target);
924 }
Chet Haase21cd1382010-09-01 17:42:29 -0700925 }
926 }
927
Chet Haasef54a8d72010-07-22 14:44:59 -0700928 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700929 * This method is called with the elapsed fraction of the animation during every
930 * animation frame. This function turns the elapsed fraction into an interpolated fraction
931 * and then into an animated value (from the evaluator. The function is called mostly during
932 * animation updates, but it is also called when the <code>end()</code>
933 * function is called, to set the final value on the property.
934 *
935 * <p>Overrides of this method must call the superclass to perform the calculation
936 * of the animated value.</p>
937 *
938 * @param fraction The elapsed fraction of the animation.
939 */
940 @Override
941 void animateValue(float fraction) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700942 final Object target = getTarget();
943 if (mTarget != null && target == null) {
944 // We lost the target reference, cancel and clean up.
945 cancel();
946 return;
947 }
948
Chet Haase17fb4b02010-06-28 17:55:07 -0700949 super.animateValue(fraction);
Chet Haase602e4d32010-08-16 08:57:23 -0700950 int numValues = mValues.length;
951 for (int i = 0; i < numValues; ++i) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700952 mValues[i].setAnimatedValue(target);
Chet Haase17fb4b02010-06-28 17:55:07 -0700953 }
954 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700955
956 @Override
Chet Haasea18a86b2010-09-07 13:20:00 -0700957 public ObjectAnimator clone() {
958 final ObjectAnimator anim = (ObjectAnimator) super.clone();
Chet Haase49afa5b2010-08-23 11:39:53 -0700959 return anim;
960 }
Chet Haasee9140a72011-02-16 16:23:29 -0800961
962 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700963 @NonNull
Chet Haasee9140a72011-02-16 16:23:29 -0800964 public String toString() {
965 String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " +
Alan Viverette87ac5f62014-06-04 16:39:21 -0700966 getTarget();
Chet Haasee9140a72011-02-16 16:23:29 -0800967 if (mValues != null) {
968 for (int i = 0; i < mValues.length; ++i) {
969 returnVal += "\n " + mValues[i].toString();
970 }
971 }
972 return returnVal;
973 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700974}