blob: 0b751b27afc94c7e0489079665d94bffbe04d653 [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
Tor Norbyec615c6f2015-03-02 10:11:44 -080019import android.annotation.CallSuper;
Alan Viverette87ac5f62014-06-04 16:39:21 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
George Mountc96c7b22013-08-23 13:31:31 -070022import android.graphics.Path;
23import android.graphics.PointF;
Chet Haase17fb4b02010-06-28 17:55:07 -070024import android.util.Log;
Chet Haaseb39f0512011-05-24 14:36:40 -070025import android.util.Property;
George Mountf2aeca32015-10-16 10:47:57 -070026import android.view.animation.AccelerateDecelerateInterpolator;
Chet Haase17fb4b02010-06-28 17:55:07 -070027
Alan Viverette87ac5f62014-06-04 16:39:21 -070028import java.lang.ref.WeakReference;
Chet Haase17fb4b02010-06-28 17:55:07 -070029
30/**
Chet Haasea18a86b2010-09-07 13:20:00 -070031 * This subclass of {@link ValueAnimator} provides support for animating properties on target objects.
Chet Haase17fb4b02010-06-28 17:55:07 -070032 * The constructors of this class take parameters to define the target object that will be animated
33 * as well as the name of the property that will be animated. Appropriate set/get functions
34 * are then determined internally and the animation will call these functions as necessary to
35 * animate the property.
Chet Haase6e0ecb42010-11-03 19:41:18 -070036 *
Chet Haased4307532014-12-01 06:32:38 -080037 * <p>Animators can be created from either code or resource files, as shown here:</p>
38 *
39 * {@sample development/samples/ApiDemos/res/anim/object_animator.xml ObjectAnimatorResources}
40 *
41 * <p>When using resource files, it is possible to use {@link PropertyValuesHolder} and
42 * {@link Keyframe} to create more complex animations. Using PropertyValuesHolders
43 * allows animators to animate several properties in parallel, as shown in this sample:</p>
44 *
45 * {@sample development/samples/ApiDemos/res/anim/object_animator_pvh.xml
46 * PropertyValuesHolderResources}
47 *
48 * <p>Using Keyframes allows animations to follow more complex paths from the start
49 * to the end values. Note that you can specify explicit fractional values (from 0 to 1) for
50 * each keyframe to determine when, in the overall duration, the animation should arrive at that
51 * value. Alternatively, you can leave the fractions off and the keyframes will be equally
52 * distributed within the total duration. Also, a keyframe with no value will derive its value
53 * from the target object when the animator starts, just like animators with only one
Doris Liu6aac06a2015-04-01 10:27:40 -070054 * value specified. In addition, an optional interpolator can be specified. The interpolator will
55 * be applied on the interval between the keyframe that the interpolator is set on and the previous
George Mountf2aeca32015-10-16 10:47:57 -070056 * keyframe. When no interpolator is supplied, the default {@link AccelerateDecelerateInterpolator}
57 * will be used. </p>
Chet Haased4307532014-12-01 06:32:38 -080058 *
Doris Liu6aac06a2015-04-01 10:27:40 -070059 * {@sample development/samples/ApiDemos/res/anim/object_animator_pvh_kf_interpolated.xml KeyframeResources}
Chet Haased4307532014-12-01 06:32:38 -080060 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080061 * <div class="special reference">
62 * <h3>Developer Guides</h3>
63 * <p>For more information about animating with {@code ObjectAnimator}, read the
64 * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#object-animator">Property
65 * Animation</a> developer guide.</p>
66 * </div>
67 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070068 * @see #setPropertyName(String)
69 *
Chet Haase17fb4b02010-06-28 17:55:07 -070070 */
Chet Haase2794eb32010-10-12 16:29:28 -070071public final class ObjectAnimator extends ValueAnimator {
Alan Viverette87ac5f62014-06-04 16:39:21 -070072 private static final String LOG_TAG = "ObjectAnimator";
73
Chet Haasee2ab7cc2010-12-06 16:10:07 -080074 private static final boolean DBG = false;
Chet Haase17fb4b02010-06-28 17:55:07 -070075
Alan Viverette87ac5f62014-06-04 16:39:21 -070076 /**
77 * A weak reference to the target object on which the property exists, set
78 * in the constructor. We'll cancel the animation if this goes away.
79 */
80 private WeakReference<Object> mTarget;
Chet Haase17fb4b02010-06-28 17:55:07 -070081
82 private String mPropertyName;
83
Chet Haaseb39f0512011-05-24 14:36:40 -070084 private Property mProperty;
85
Chet Haasebe19e032013-03-15 17:08:55 -070086 private boolean mAutoCancel = false;
87
Chet Haase17fb4b02010-06-28 17:55:07 -070088 /**
89 * Sets the name of the property that will be animated. This name is used to derive
90 * a setter function that will be called to set animated values.
91 * For example, a property name of <code>foo</code> will result
92 * in a call to the function <code>setFoo()</code> on the target object. If either
93 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
94 * also be derived and called.
95 *
Chet Haase6e0ecb42010-11-03 19:41:18 -070096 * <p>For best performance of the mechanism that calls the setter function determined by the
97 * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
98 * and make the setter function for those properties have a <code>void</code> return value. This
99 * will cause the code to take an optimized path for these constrained circumstances. Other
100 * property types and return types will work, but will have more overhead in processing
101 * the requests due to normal reflection mechanisms.</p>
102 *
Chet Haase17fb4b02010-06-28 17:55:07 -0700103 * <p>Note that the setter function derived from this property name
104 * must take the same parameter type as the
105 * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
106 * the setter function will fail.</p>
107 *
Chet Haasea18a86b2010-09-07 13:20:00 -0700108 * <p>If this ObjectAnimator has been set up to animate several properties together,
Chet Haased953d082010-08-16 17:44:28 -0700109 * using more than one PropertyValuesHolder objects, then setting the propertyName simply
110 * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
111 *
Chet Haaseb39f0512011-05-24 14:36:40 -0700112 * @param propertyName The name of the property being animated. Should not be null.
Chet Haase17fb4b02010-06-28 17:55:07 -0700113 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700114 public void setPropertyName(@NonNull String propertyName) {
Chet Haase0e0590b2010-09-26 11:57:28 -0700115 // mValues could be null if this is being constructed piecemeal. Just record the
116 // propertyName to be used later when setValues() is called if so.
Chet Haased953d082010-08-16 17:44:28 -0700117 if (mValues != null) {
Chet Haase602e4d32010-08-16 08:57:23 -0700118 PropertyValuesHolder valuesHolder = mValues[0];
119 String oldName = valuesHolder.getPropertyName();
Chet Haased953d082010-08-16 17:44:28 -0700120 valuesHolder.setPropertyName(propertyName);
Chet Haase602e4d32010-08-16 08:57:23 -0700121 mValuesMap.remove(oldName);
122 mValuesMap.put(propertyName, valuesHolder);
Chet Haased953d082010-08-16 17:44:28 -0700123 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700124 mPropertyName = propertyName;
Chet Haase0e0590b2010-09-26 11:57:28 -0700125 // New property/values/target should cause re-initialization prior to starting
126 mInitialized = false;
Chet Haase17fb4b02010-06-28 17:55:07 -0700127 }
128
129 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700130 * Sets the property that will be animated. Property objects will take precedence over
131 * properties specified by the {@link #setPropertyName(String)} method. Animations should
132 * be set up to use one or the other, not both.
133 *
134 * @param property The property being animated. Should not be null.
135 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700136 public void setProperty(@NonNull Property property) {
Chet Haaseb39f0512011-05-24 14:36:40 -0700137 // mValues could be null if this is being constructed piecemeal. Just record the
138 // propertyName to be used later when setValues() is called if so.
139 if (mValues != null) {
140 PropertyValuesHolder valuesHolder = mValues[0];
141 String oldName = valuesHolder.getPropertyName();
142 valuesHolder.setProperty(property);
143 mValuesMap.remove(oldName);
144 mValuesMap.put(mPropertyName, valuesHolder);
145 }
146 if (mProperty != null) {
147 mPropertyName = property.getName();
148 }
149 mProperty = property;
150 // New property/values/target should cause re-initialization prior to starting
151 mInitialized = false;
152 }
153
154 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700155 * Gets the name of the property that will be animated. This name will be used to derive
156 * a setter function that will be called to set animated values.
157 * For example, a property name of <code>foo</code> will result
158 * in a call to the function <code>setFoo()</code> on the target object. If either
159 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
160 * also be derived and called.
Chet Haasefdd3ad72013-04-24 16:38:20 -0700161 *
162 * <p>If this animator was created with a {@link Property} object instead of the
163 * string name of a property, then this method will return the {@link
164 * Property#getName() name} of that Property object instead. If this animator was
165 * created with one or more {@link PropertyValuesHolder} objects, then this method
166 * will return the {@link PropertyValuesHolder#getPropertyName() name} of that
167 * object (if there was just one) or a comma-separated list of all of the
168 * names (if there are more than one).</p>
Chet Haase17fb4b02010-06-28 17:55:07 -0700169 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700170 @Nullable
Chet Haase17fb4b02010-06-28 17:55:07 -0700171 public String getPropertyName() {
Chet Haasefdd3ad72013-04-24 16:38:20 -0700172 String propertyName = null;
173 if (mPropertyName != null) {
174 propertyName = mPropertyName;
175 } else if (mProperty != null) {
176 propertyName = mProperty.getName();
177 } else if (mValues != null && mValues.length > 0) {
178 for (int i = 0; i < mValues.length; ++i) {
179 if (i == 0) {
180 propertyName = "";
181 } else {
182 propertyName += ",";
183 }
184 propertyName += mValues[i].getPropertyName();
185 }
186 }
187 return propertyName;
188 }
189
190 @Override
191 String getNameForTrace() {
192 return "animator:" + getPropertyName();
Chet Haase17fb4b02010-06-28 17:55:07 -0700193 }
194
195 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700196 * Creates a new ObjectAnimator object. This default constructor is primarily for
Chet Haased51d3682010-08-11 19:46:48 -0700197 * use internally; the other constructors which take parameters are more generally
198 * useful.
199 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700200 public ObjectAnimator() {
Chet Haased51d3682010-08-11 19:46:48 -0700201 }
202
203 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700204 * Private utility constructor that initializes the target object and name of the
205 * property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700206 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800207 * @param target The object whose property is to be animated. This object should
208 * have a public method on it called <code>setName()</code>, where <code>name</code> is
209 * the value of the <code>propertyName</code> parameter.
Chet Haased953d082010-08-16 17:44:28 -0700210 * @param propertyName The name of the property being animated.
Chet Haase17fb4b02010-06-28 17:55:07 -0700211 */
Chet Haase2794eb32010-10-12 16:29:28 -0700212 private ObjectAnimator(Object target, String propertyName) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700213 setTarget(target);
Chet Haased953d082010-08-16 17:44:28 -0700214 setPropertyName(propertyName);
Chet Haase17fb4b02010-06-28 17:55:07 -0700215 }
216
217 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700218 * Private utility constructor that initializes the target object and property being animated.
219 *
220 * @param target The object whose property is to be animated.
221 * @param property The property being animated.
222 */
223 private <T> ObjectAnimator(T target, Property<T, ?> property) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700224 setTarget(target);
Chet Haaseb39f0512011-05-24 14:36:40 -0700225 setProperty(property);
226 }
227
228 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700229 * Constructs and returns an ObjectAnimator that animates between int values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700230 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700231 * and ending values. More than two values imply a starting value, values to animate through
232 * along the way, and an ending value (these values will be distributed evenly across
233 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700234 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800235 * @param target The object whose property is to be animated. This object should
236 * have a public method on it called <code>setName()</code>, where <code>name</code> is
237 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700238 * @param propertyName The name of the property being animated.
239 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700240 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700241 */
242 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
243 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
244 anim.setIntValues(values);
245 return anim;
246 }
247
248 /**
George Mountc96c7b22013-08-23 13:31:31 -0700249 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
250 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
251 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
252 * coordinates are integers that are set to separate properties designated by
253 * <code>xPropertyName</code> and <code>yPropertyName</code>.
254 *
255 * @param target The object whose properties are to be animated. This object should
256 * have public methods on it called <code>setNameX()</code> and
257 * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code>
258 * are the value of <code>xPropertyName</code> and <code>yPropertyName</code>
259 * parameters, respectively.
260 * @param xPropertyName The name of the property for the x coordinate being animated.
261 * @param yPropertyName The name of the property for the y coordinate being animated.
262 * @param path The <code>Path</code> to animate values along.
263 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
264 */
265 public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName,
266 Path path) {
George Mount984011f2014-08-21 14:28:01 -0700267 PathKeyframes keyframes = KeyframeSet.ofPath(path);
268 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xPropertyName,
269 keyframes.createXIntKeyframes());
270 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yPropertyName,
271 keyframes.createYIntKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700272 return ofPropertyValuesHolder(target, x, y);
273 }
274
275 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700276 * Constructs and returns an ObjectAnimator that animates between int values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700277 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700278 * and ending values. More than two values imply a starting value, values to animate through
279 * along the way, and an ending value (these values will be distributed evenly across
280 * the duration of the animation).
281 *
282 * @param target The object whose property is to be animated.
283 * @param property The property being animated.
284 * @param values A set of values that the animation will animate between over time.
285 * @return An ObjectAnimator object that is set up to animate between the given values.
286 */
287 public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values) {
288 ObjectAnimator anim = new ObjectAnimator(target, property);
289 anim.setIntValues(values);
290 return anim;
291 }
292
293 /**
George Mountc96c7b22013-08-23 13:31:31 -0700294 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
295 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
296 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
297 * coordinates are integers that are set to separate properties, <code>xProperty</code> and
298 * <code>yProperty</code>.
299 *
300 * @param target The object whose properties are to be animated.
301 * @param xProperty The property for the x coordinate being animated.
302 * @param yProperty The property for the y coordinate being animated.
303 * @param path The <code>Path</code> to animate values along.
304 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
305 */
306 public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> xProperty,
307 Property<T, Integer> yProperty, Path path) {
George Mount984011f2014-08-21 14:28:01 -0700308 PathKeyframes keyframes = KeyframeSet.ofPath(path);
309 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xProperty,
310 keyframes.createXIntKeyframes());
311 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yProperty,
312 keyframes.createYIntKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700313 return ofPropertyValuesHolder(target, x, y);
314 }
315
316 /**
George Mount4eed5292013-08-30 13:56:01 -0700317 * Constructs and returns an ObjectAnimator that animates over int values for a multiple
318 * parameters setter. Only public methods that take only int parameters are supported.
319 * Each <code>int[]</code> contains a complete set of parameters to the setter method.
320 * At least two <code>int[]</code> values must be provided, a start and end. More than two
321 * values imply a starting value, values to animate through along the way, and an ending
322 * value (these values will be distributed evenly across the duration of the animation).
323 *
324 * @param target The object whose property is to be animated. This object may
325 * have a public method on it called <code>setName()</code>, where <code>name</code> is
326 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
327 * be the case-sensitive complete name of the public setter method.
328 * @param propertyName The name of the property being animated or the name of the setter method.
329 * @param values A set of values that the animation will animate between over time.
330 * @return An ObjectAnimator object that is set up to animate between the given values.
331 */
332 public static ObjectAnimator ofMultiInt(Object target, String propertyName, int[][] values) {
333 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, values);
334 return ofPropertyValuesHolder(target, pvh);
335 }
336
337 /**
George Mountc96c7b22013-08-23 13:31:31 -0700338 * Constructs and returns an ObjectAnimator that animates the target using a multi-int setter
339 * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions,
340 * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the
341 * coordinates are integer x and y coordinates used in the first and second parameter of the
342 * setter, respectively.
343 *
344 * @param target The object whose property is to be animated. This object may
345 * have a public method on it called <code>setName()</code>, where <code>name</code> is
346 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
347 * be the case-sensitive complete name of the public setter method.
348 * @param propertyName The name of the property being animated or the name of the setter method.
349 * @param path The <code>Path</code> to animate values along.
350 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
351 */
352 public static ObjectAnimator ofMultiInt(Object target, String propertyName, Path path) {
353 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, path);
354 return ofPropertyValuesHolder(target, pvh);
355 }
356
357 /**
George Mount4eed5292013-08-30 13:56:01 -0700358 * Constructs and returns an ObjectAnimator that animates over values for a multiple int
359 * parameters setter. Only public methods that take only int parameters are supported.
360 * <p>At least two values must be provided, a start and end. More than two
361 * values imply a starting value, values to animate through along the way, and an ending
362 * value (these values will be distributed evenly across the duration of the animation).</p>
363 *
364 * @param target The object whose property is to be animated. This object may
365 * have a public method on it called <code>setName()</code>, where <code>name</code> is
366 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
George Mountc96c7b22013-08-23 13:31:31 -0700367 * be the case-sensitive complete name of the public setter method.
George Mount4eed5292013-08-30 13:56:01 -0700368 * @param propertyName The name of the property being animated or the name of the setter method.
369 * @param converter Converts T objects into int parameters for the multi-value setter.
370 * @param evaluator A TypeEvaluator that will be called on each animation frame to
371 * provide the necessary interpolation between the Object values to derive the animated
372 * value.
373 * @param values A set of values that the animation will animate between over time.
374 * @return An ObjectAnimator object that is set up to animate between the given values.
375 */
376 public static <T> ObjectAnimator ofMultiInt(Object target, String propertyName,
377 TypeConverter<T, int[]> converter, TypeEvaluator<T> evaluator, T... values) {
378 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, converter,
379 evaluator, values);
380 return ObjectAnimator.ofPropertyValuesHolder(target, pvh);
381 }
382
383 /**
George Mount1ffb2802013-10-09 16:13:54 -0700384 * Constructs and returns an ObjectAnimator that animates between color values. A single
385 * value implies that that value is the one being animated to. Two values imply starting
386 * and ending values. More than two values imply a starting value, values to animate through
387 * along the way, and an ending value (these values will be distributed evenly across
388 * the duration of the animation).
389 *
390 * @param target The object whose property is to be animated. This object should
391 * have a public method on it called <code>setName()</code>, where <code>name</code> is
392 * the value of the <code>propertyName</code> parameter.
393 * @param propertyName The name of the property being animated.
394 * @param values A set of values that the animation will animate between over time.
395 * @return An ObjectAnimator object that is set up to animate between the given values.
396 */
397 public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) {
398 ObjectAnimator animator = ofInt(target, propertyName, values);
399 animator.setEvaluator(ArgbEvaluator.getInstance());
400 return animator;
401 }
402
403 /**
404 * Constructs and returns an ObjectAnimator that animates between color values. A single
405 * value implies that that value is the one being animated to. Two values imply starting
406 * and ending values. More than two values imply a starting value, values to animate through
407 * along the way, and an ending value (these values will be distributed evenly across
408 * the duration of the animation).
409 *
410 * @param target The object whose property is to be animated.
411 * @param property The property being animated.
412 * @param values A set of values that the animation will animate between over time.
413 * @return An ObjectAnimator object that is set up to animate between the given values.
414 */
415 public static <T> ObjectAnimator ofArgb(T target, Property<T, Integer> property,
416 int... values) {
417 ObjectAnimator animator = ofInt(target, property, values);
418 animator.setEvaluator(ArgbEvaluator.getInstance());
419 return animator;
420 }
421
422 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700423 * Constructs and returns an ObjectAnimator that animates between float values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700424 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700425 * and ending values. More than two values imply a starting value, values to animate through
426 * along the way, and an ending value (these values will be distributed evenly across
427 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700428 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800429 * @param target The object whose property is to be animated. This object should
430 * have a public method on it called <code>setName()</code>, where <code>name</code> is
431 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700432 * @param propertyName The name of the property being animated.
433 * @param values A set of values that the animation will animate between over time.
Chet Haaseb39f0512011-05-24 14:36:40 -0700434 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700435 */
436 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
437 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
438 anim.setFloatValues(values);
439 return anim;
440 }
441
442 /**
George Mountc96c7b22013-08-23 13:31:31 -0700443 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
444 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
445 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
446 * coordinates are floats that are set to separate properties designated by
447 * <code>xPropertyName</code> and <code>yPropertyName</code>.
448 *
449 * @param target The object whose properties are to be animated. This object should
450 * have public methods on it called <code>setNameX()</code> and
451 * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code>
452 * are the value of the <code>xPropertyName</code> and <code>yPropertyName</code>
453 * parameters, respectively.
454 * @param xPropertyName The name of the property for the x coordinate being animated.
455 * @param yPropertyName The name of the property for the y coordinate being animated.
456 * @param path The <code>Path</code> to animate values along.
457 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
458 */
459 public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName,
460 Path path) {
George Mount984011f2014-08-21 14:28:01 -0700461 PathKeyframes keyframes = KeyframeSet.ofPath(path);
462 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xPropertyName,
463 keyframes.createXFloatKeyframes());
464 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yPropertyName,
465 keyframes.createYFloatKeyframes());
George Mountc96c7b22013-08-23 13:31:31 -0700466 return ofPropertyValuesHolder(target, x, y);
467 }
468
469 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700470 * Constructs and returns an ObjectAnimator that animates between float values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700471 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700472 * and ending values. More than two values imply a starting value, values to animate through
473 * along the way, and an ending value (these values will be distributed evenly across
474 * the duration of the animation).
475 *
476 * @param target The object whose property is to be animated.
477 * @param property The property being animated.
478 * @param values A set of values that the animation will animate between over time.
479 * @return An ObjectAnimator object that is set up to animate between the given values.
480 */
481 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property,
482 float... values) {
483 ObjectAnimator anim = new ObjectAnimator(target, property);
484 anim.setFloatValues(values);
485 return anim;
486 }
487
488 /**
George Mountc96c7b22013-08-23 13:31:31 -0700489 * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code>
490 * using two properties. A <code>Path</code></> animation moves in two dimensions, animating
491 * coordinates <code>(x, y)</code> together to follow the line. In this variation, the
492 * coordinates are floats that are set to separate properties, <code>xProperty</code> and
493 * <code>yProperty</code>.
494 *
495 * @param target The object whose properties are to be animated.
496 * @param xProperty The property for the x coordinate being animated.
497 * @param yProperty The property for the y coordinate being animated.
498 * @param path The <code>Path</code> to animate values along.
499 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
500 */
501 public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> xProperty,
502 Property<T, Float> yProperty, Path path) {
George Mount984011f2014-08-21 14:28:01 -0700503 PathKeyframes keyframes = KeyframeSet.ofPath(path);
504 PropertyValuesHolder x = PropertyValuesHolder.ofKeyframes(xProperty,
505 keyframes.createXFloatKeyframes());
506 PropertyValuesHolder y = PropertyValuesHolder.ofKeyframes(yProperty,
507 keyframes.createYFloatKeyframes());
George Mountf505b1f2014-06-23 10:17:34 -0700508 return ofPropertyValuesHolder(target, x, y);
George Mountc96c7b22013-08-23 13:31:31 -0700509 }
510
511 /**
George Mount4eed5292013-08-30 13:56:01 -0700512 * Constructs and returns an ObjectAnimator that animates over float values for a multiple
513 * parameters setter. Only public methods that take only float parameters are supported.
514 * Each <code>float[]</code> contains a complete set of parameters to the setter method.
515 * At least two <code>float[]</code> values must be provided, a start and end. More than two
516 * values imply a starting value, values to animate through along the way, and an ending
517 * value (these values will be distributed evenly across the duration of the animation).
518 *
519 * @param target The object whose property is to be animated. This object may
520 * have a public method on it called <code>setName()</code>, where <code>name</code> is
521 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
George Mountc96c7b22013-08-23 13:31:31 -0700522 * be the case-sensitive complete name of the public setter method.
George Mount4eed5292013-08-30 13:56:01 -0700523 * @param propertyName The name of the property being animated or the name of the setter method.
524 * @param values A set of values that the animation will animate between over time.
525 * @return An ObjectAnimator object that is set up to animate between the given values.
526 */
527 public static ObjectAnimator ofMultiFloat(Object target, String propertyName,
528 float[][] values) {
529 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, values);
530 return ofPropertyValuesHolder(target, pvh);
531 }
532
533 /**
George Mountc96c7b22013-08-23 13:31:31 -0700534 * Constructs and returns an ObjectAnimator that animates the target using a multi-float setter
535 * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions,
536 * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the
537 * coordinates are float x and y coordinates used in the first and second parameter of the
538 * setter, respectively.
539 *
540 * @param target The object whose property is to be animated. This object may
541 * have a public method on it called <code>setName()</code>, where <code>name</code> is
542 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
543 * be the case-sensitive complete name of the public setter method.
544 * @param propertyName The name of the property being animated or the name of the setter method.
545 * @param path The <code>Path</code> to animate values along.
546 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
547 */
548 public static ObjectAnimator ofMultiFloat(Object target, String propertyName, Path path) {
549 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, path);
550 return ofPropertyValuesHolder(target, pvh);
551 }
552
553 /**
George Mount4eed5292013-08-30 13:56:01 -0700554 * Constructs and returns an ObjectAnimator that animates over values for a multiple float
555 * parameters setter. Only public methods that take only float parameters are supported.
556 * <p>At least two values must be provided, a start and end. More than two
557 * values imply a starting value, values to animate through along the way, and an ending
558 * value (these values will be distributed evenly across the duration of the animation).</p>
559 *
560 * @param target The object whose property is to be animated. This object may
561 * have a public method on it called <code>setName()</code>, where <code>name</code> is
562 * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also
563 * be the case-sensitive complete name of the public setter method.
564 * @param propertyName The name of the property being animated or the name of the setter method.
565 * @param converter Converts T objects into float parameters for the multi-value setter.
566 * @param evaluator A TypeEvaluator that will be called on each animation frame to
567 * provide the necessary interpolation between the Object values to derive the animated
568 * value.
569 * @param values A set of values that the animation will animate between over time.
570 * @return An ObjectAnimator object that is set up to animate between the given values.
571 */
572 public static <T> ObjectAnimator ofMultiFloat(Object target, String propertyName,
573 TypeConverter<T, float[]> converter, TypeEvaluator<T> evaluator, T... values) {
574 PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, converter,
575 evaluator, values);
576 return ObjectAnimator.ofPropertyValuesHolder(target, pvh);
577 }
578
579 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700580 * Constructs and returns an ObjectAnimator that animates between Object values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700581 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700582 * and ending values. More than two values imply a starting value, values to animate through
583 * along the way, and an ending value (these values will be distributed evenly across
584 * the duration of the animation).
Chet Haasefe591562010-07-27 11:15:37 -0700585 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800586 * @param target The object whose property is to be animated. This object should
Chet Haaseb39f0512011-05-24 14:36:40 -0700587 * have a public method on it called <code>setName()</code>, where <code>name</code> is
588 * the value of the <code>propertyName</code> parameter.
Chet Haase2794eb32010-10-12 16:29:28 -0700589 * @param propertyName The name of the property being animated.
590 * @param evaluator A TypeEvaluator that will be called on each animation frame to
Chet Haaseb39f0512011-05-24 14:36:40 -0700591 * provide the necessary interpolation between the Object values to derive the animated
Chet Haase2794eb32010-10-12 16:29:28 -0700592 * value.
Chet Haaseb39f0512011-05-24 14:36:40 -0700593 * @param values A set of values that the animation will animate between over time.
594 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haasefe591562010-07-27 11:15:37 -0700595 */
Chet Haase2794eb32010-10-12 16:29:28 -0700596 public static ObjectAnimator ofObject(Object target, String propertyName,
597 TypeEvaluator evaluator, Object... values) {
598 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
599 anim.setObjectValues(values);
600 anim.setEvaluator(evaluator);
601 return anim;
602 }
603
604 /**
George Mountc96c7b22013-08-23 13:31:31 -0700605 * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>.
606 * A <code>Path</code></> animation moves in two dimensions, animating coordinates
607 * <code>(x, y)</code> together to follow the line. This variant animates the coordinates
608 * in a <code>PointF</code> to follow the <code>Path</code>. If the <code>Property</code>
609 * associated with <code>propertyName</code> uses a type other than <code>PointF</code>,
610 * <code>converter</code> can be used to change from <code>PointF</code> to the type
611 * associated with the <code>Property</code>.
612 *
613 * @param target The object whose property is to be animated. This object should
614 * have a public method on it called <code>setName()</code>, where <code>name</code> is
615 * the value of the <code>propertyName</code> parameter.
616 * @param propertyName The name of the property being animated.
617 * @param converter Converts a PointF to the type associated with the setter. May be
618 * null if conversion is unnecessary.
619 * @param path The <code>Path</code> to animate values along.
620 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
621 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700622 @NonNull
George Mountc96c7b22013-08-23 13:31:31 -0700623 public static ObjectAnimator ofObject(Object target, String propertyName,
Alan Viverette87ac5f62014-06-04 16:39:21 -0700624 @Nullable TypeConverter<PointF, ?> converter, Path path) {
George Mountc96c7b22013-08-23 13:31:31 -0700625 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(propertyName, converter, path);
626 return ofPropertyValuesHolder(target, pvh);
627 }
628
629 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700630 * Constructs and returns an ObjectAnimator that animates between Object values. A single
George Mount16d2c9c2013-09-17 09:07:48 -0700631 * value implies that that value is the one being animated to. Two values imply starting
Chet Haaseb39f0512011-05-24 14:36:40 -0700632 * and ending values. More than two values imply a starting value, values to animate through
633 * along the way, and an ending value (these values will be distributed evenly across
634 * the duration of the animation).
Chet Haase2794eb32010-10-12 16:29:28 -0700635 *
Chet Haaseb39f0512011-05-24 14:36:40 -0700636 * @param target The object whose property is to be animated.
637 * @param property The property being animated.
638 * @param evaluator A TypeEvaluator that will be called on each animation frame to
639 * provide the necessary interpolation between the Object values to derive the animated
640 * value.
641 * @param values A set of values that the animation will animate between over time.
642 * @return An ObjectAnimator object that is set up to animate between the given values.
643 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700644 @NonNull
Chet Haaseb39f0512011-05-24 14:36:40 -0700645 public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property,
646 TypeEvaluator<V> evaluator, V... values) {
647 ObjectAnimator anim = new ObjectAnimator(target, property);
648 anim.setObjectValues(values);
649 anim.setEvaluator(evaluator);
650 return anim;
651 }
652
653 /**
George Mount16d2c9c2013-09-17 09:07:48 -0700654 * Constructs and returns an ObjectAnimator that animates between Object values. A single
655 * value implies that that value is the one being animated to. Two values imply starting
656 * and ending values. More than two values imply a starting value, values to animate through
657 * along the way, and an ending value (these values will be distributed evenly across
658 * the duration of the animation). This variant supplies a <code>TypeConverter</code> to
659 * convert from the animated values to the type of the property. If only one value is
George Mount42516d12014-05-19 15:49:29 -0700660 * supplied, the <code>TypeConverter</code> must be a
661 * {@link android.animation.BidirectionalTypeConverter} to retrieve the current value.
George Mount16d2c9c2013-09-17 09:07:48 -0700662 *
663 * @param target The object whose property is to be animated.
664 * @param property The property being animated.
665 * @param converter Converts the animated object to the Property type.
666 * @param evaluator A TypeEvaluator that will be called on each animation frame to
667 * provide the necessary interpolation between the Object values to derive the animated
668 * value.
669 * @param values A set of values that the animation will animate between over time.
670 * @return An ObjectAnimator object that is set up to animate between the given values.
671 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700672 @NonNull
George Mount16d2c9c2013-09-17 09:07:48 -0700673 public static <T, V, P> ObjectAnimator ofObject(T target, Property<T, P> property,
674 TypeConverter<V, P> converter, TypeEvaluator<V> evaluator, V... values) {
675 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, evaluator,
676 values);
677 return ofPropertyValuesHolder(target, pvh);
678 }
679
680 /**
George Mountc96c7b22013-08-23 13:31:31 -0700681 * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>.
682 * A <code>Path</code></> animation moves in two dimensions, animating coordinates
683 * <code>(x, y)</code> together to follow the line. This variant animates the coordinates
684 * in a <code>PointF</code> to follow the <code>Path</code>. If <code>property</code>
685 * uses a type other than <code>PointF</code>, <code>converter</code> can be used to change
686 * from <code>PointF</code> to the type associated with the <code>Property</code>.
687 *
George Mount984011f2014-08-21 14:28:01 -0700688 * <p>The PointF passed to <code>converter</code> or <code>property</code>, if
689 * <code>converter</code> is <code>null</code>, is reused on each animation frame and should
690 * not be stored by the setter or TypeConverter.</p>
691 *
George Mountc96c7b22013-08-23 13:31:31 -0700692 * @param target The object whose property is to be animated.
693 * @param property The property being animated. Should not be null.
694 * @param converter Converts a PointF to the type associated with the setter. May be
695 * null if conversion is unnecessary.
696 * @param path The <code>Path</code> to animate values along.
697 * @return An ObjectAnimator object that is set up to animate along <code>path</code>.
698 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700699 @NonNull
700 public static <T, V> ObjectAnimator ofObject(T target, @NonNull Property<T, V> property,
701 @Nullable TypeConverter<PointF, V> converter, Path path) {
George Mountc96c7b22013-08-23 13:31:31 -0700702 PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, path);
703 return ofPropertyValuesHolder(target, pvh);
704 }
705
706 /**
Chet Haaseb39f0512011-05-24 14:36:40 -0700707 * Constructs and returns an ObjectAnimator that animates between the sets of values specified
708 * in <code>PropertyValueHolder</code> objects. This variant should be used when animating
709 * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows
710 * you to associate a set of animation values with a property name.
711 *
712 * @param target The object whose property is to be animated. Depending on how the
713 * PropertyValuesObjects were constructed, the target object should either have the {@link
714 * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the
715 * PropertyValuesHOlder objects were created with property names) the target object should have
716 * public methods on it called <code>setName()</code>, where <code>name</code> is the name of
717 * the property passed in as the <code>propertyName</code> parameter for each of the
718 * PropertyValuesHolder objects.
719 * @param values A set of PropertyValuesHolder objects whose values will be animated between
720 * over time.
721 * @return An ObjectAnimator object that is set up to animate between the given values.
Chet Haase2794eb32010-10-12 16:29:28 -0700722 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700723 @NonNull
Chet Haase2794eb32010-10-12 16:29:28 -0700724 public static ObjectAnimator ofPropertyValuesHolder(Object target,
725 PropertyValuesHolder... values) {
726 ObjectAnimator anim = new ObjectAnimator();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700727 anim.setTarget(target);
Chet Haase2794eb32010-10-12 16:29:28 -0700728 anim.setValues(values);
729 return anim;
Chet Haase3dd207a2010-07-20 14:00:01 -0700730 }
731
Romain Guy83d6e822010-10-14 10:13:53 -0700732 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700733 public void setIntValues(int... values) {
Romain Guy83d6e822010-10-14 10:13:53 -0700734 if (mValues == null || mValues.length == 0) {
735 // No values yet - this animator is being constructed piecemeal. Init the values with
736 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700737 if (mProperty != null) {
738 setValues(PropertyValuesHolder.ofInt(mProperty, values));
739 } else {
740 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
741 }
Romain Guy83d6e822010-10-14 10:13:53 -0700742 } else {
Chet Haase2794eb32010-10-12 16:29:28 -0700743 super.setIntValues(values);
744 }
745 }
746
747 @Override
748 public void setFloatValues(float... values) {
749 if (mValues == null || mValues.length == 0) {
750 // No values yet - this animator is being constructed piecemeal. Init the values with
751 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700752 if (mProperty != null) {
753 setValues(PropertyValuesHolder.ofFloat(mProperty, values));
754 } else {
755 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
756 }
Chet Haase2794eb32010-10-12 16:29:28 -0700757 } else {
758 super.setFloatValues(values);
759 }
760 }
761
762 @Override
Chet Haase2794eb32010-10-12 16:29:28 -0700763 public void setObjectValues(Object... values) {
764 if (mValues == null || mValues.length == 0) {
765 // No values yet - this animator is being constructed piecemeal. Init the values with
766 // whatever the current propertyName is
Chet Haaseb39f0512011-05-24 14:36:40 -0700767 if (mProperty != null) {
Chet Haasebe19e032013-03-15 17:08:55 -0700768 setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700769 } else {
Chet Haasebe19e032013-03-15 17:08:55 -0700770 setValues(PropertyValuesHolder.ofObject(mPropertyName,
771 (TypeEvaluator) null, values));
Chet Haaseb39f0512011-05-24 14:36:40 -0700772 }
Chet Haase2794eb32010-10-12 16:29:28 -0700773 } else {
774 super.setObjectValues(values);
Romain Guy83d6e822010-10-14 10:13:53 -0700775 }
Chet Haase0e0590b2010-09-26 11:57:28 -0700776 }
Romain Guy83d6e822010-10-14 10:13:53 -0700777
Chet Haasebe19e032013-03-15 17:08:55 -0700778 /**
779 * autoCancel controls whether an ObjectAnimator will be canceled automatically
780 * when any other ObjectAnimator with the same target and properties is started.
781 * Setting this flag may make it easier to run different animators on the same target
782 * object without having to keep track of whether there are conflicting animators that
783 * need to be manually canceled. Canceling animators must have the same exact set of
784 * target properties, in the same order.
785 *
786 * @param cancel Whether future ObjectAnimators with the same target and properties
787 * as this ObjectAnimator will cause this ObjectAnimator to be canceled.
788 */
789 public void setAutoCancel(boolean cancel) {
790 mAutoCancel = cancel;
791 }
792
Alan Viverette87ac5f62014-06-04 16:39:21 -0700793 private boolean hasSameTargetAndProperties(@Nullable Animator anim) {
Chet Haasebe19e032013-03-15 17:08:55 -0700794 if (anim instanceof ObjectAnimator) {
795 PropertyValuesHolder[] theirValues = ((ObjectAnimator) anim).getValues();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700796 if (((ObjectAnimator) anim).getTarget() == getTarget() &&
Chet Haasebe19e032013-03-15 17:08:55 -0700797 mValues.length == theirValues.length) {
798 for (int i = 0; i < mValues.length; ++i) {
799 PropertyValuesHolder pvhMine = mValues[i];
800 PropertyValuesHolder pvhTheirs = theirValues[i];
801 if (pvhMine.getPropertyName() == null ||
802 !pvhMine.getPropertyName().equals(pvhTheirs.getPropertyName())) {
803 return false;
804 }
805 }
806 return true;
807 }
808 }
809 return false;
810 }
811
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800812 @Override
813 public void start() {
Doris Liu3618d302015-08-14 11:11:08 -0700814 AnimationHandler.getInstance().autoCancelBasedOn(this);
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800815 if (DBG) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700816 Log.d(LOG_TAG, "Anim target, duration: " + getTarget() + ", " + getDuration());
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800817 for (int i = 0; i < mValues.length; ++i) {
818 PropertyValuesHolder pvh = mValues[i];
Alan Viverette87ac5f62014-06-04 16:39:21 -0700819 Log.d(LOG_TAG, " Values[" + i + "]: " +
George Mount984011f2014-08-21 14:28:01 -0700820 pvh.getPropertyName() + ", " + pvh.mKeyframes.getValue(0) + ", " +
821 pvh.mKeyframes.getValue(1));
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800822 }
823 }
824 super.start();
825 }
826
Doris Liu3618d302015-08-14 11:11:08 -0700827 boolean shouldAutoCancel(AnimationHandler.AnimationFrameCallback anim) {
828 if (anim == null) {
829 return false;
830 }
831
832 if (anim instanceof ObjectAnimator) {
833 ObjectAnimator objAnim = (ObjectAnimator) anim;
834 if (objAnim.mAutoCancel && hasSameTargetAndProperties(objAnim)) {
835 return true;
836 }
837 }
838 return false;
839 }
840
Chet Haase3dd207a2010-07-20 14:00:01 -0700841 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700842 * This function is called immediately before processing the first animation
843 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
844 * function is called after that delay ends.
845 * It takes care of the final initialization steps for the
846 * animation. This includes setting mEvaluator, if the user has not yet
847 * set it up, and the setter/getter methods, if the user did not supply
848 * them.
849 *
850 * <p>Overriders of this method should call the superclass method to cause
851 * internal mechanisms to be set up correctly.</p>
852 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800853 @CallSuper
Chet Haase17fb4b02010-06-28 17:55:07 -0700854 @Override
855 void initAnimation() {
Chet Haase21cd1382010-09-01 17:42:29 -0700856 if (!mInitialized) {
857 // mValueType may change due to setter/getter setup; do this before calling super.init(),
858 // which uses mValueType to set up the default type evaluator.
Alan Viverette87ac5f62014-06-04 16:39:21 -0700859 final Object target = getTarget();
860 if (target != null) {
861 final int numValues = mValues.length;
862 for (int i = 0; i < numValues; ++i) {
863 mValues[i].setupSetterAndGetter(target);
864 }
Chet Haase21cd1382010-09-01 17:42:29 -0700865 }
866 super.initAnimation();
Chet Haase17fb4b02010-06-28 17:55:07 -0700867 }
868 }
869
Chet Haase2794eb32010-10-12 16:29:28 -0700870 /**
871 * Sets the length of the animation. The default duration is 300 milliseconds.
872 *
873 * @param duration The length of the animation, in milliseconds.
874 * @return ObjectAnimator The object called with setDuration(). This return
875 * value makes it easier to compose statements together that construct and then set the
876 * duration, as in
877 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
878 */
879 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700880 @NonNull
Chet Haase2794eb32010-10-12 16:29:28 -0700881 public ObjectAnimator setDuration(long duration) {
882 super.setDuration(duration);
883 return this;
884 }
885
Chet Haase17fb4b02010-06-28 17:55:07 -0700886
887 /**
888 * The target object whose property will be animated by this animation
889 *
Patrick Dubroy51ae5fc2011-01-16 14:23:15 -0800890 * @return The object being animated
Chet Haase17fb4b02010-06-28 17:55:07 -0700891 */
Alan Viverette87ac5f62014-06-04 16:39:21 -0700892 @Nullable
Chet Haase17fb4b02010-06-28 17:55:07 -0700893 public Object getTarget() {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700894 return mTarget == null ? null : mTarget.get();
Chet Haase17fb4b02010-06-28 17:55:07 -0700895 }
896
897 /**
George Mounte48ef2a2014-10-31 14:23:34 -0700898 * Sets the target object whose property will be animated by this animation. If the
899 * animator has been started, it will be canceled.
Chet Haasef54a8d72010-07-22 14:44:59 -0700900 *
901 * @param target The object being animated
902 */
Chet Haase21cd1382010-09-01 17:42:29 -0700903 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700904 public void setTarget(@Nullable Object target) {
905 final Object oldTarget = getTarget();
906 if (oldTarget != target) {
George Mounte48ef2a2014-10-31 14:23:34 -0700907 if (isStarted()) {
908 cancel();
909 }
Alan Viverette87ac5f62014-06-04 16:39:21 -0700910 mTarget = target == null ? null : new WeakReference<Object>(target);
Yigit Boyar8619f482014-07-15 17:28:07 -0700911 // New target should cause re-initialization prior to starting
Chet Haase70d4ba12010-10-06 09:46:45 -0700912 mInitialized = false;
913 }
Chet Haasef54a8d72010-07-22 14:44:59 -0700914 }
915
Chet Haase21cd1382010-09-01 17:42:29 -0700916 @Override
917 public void setupStartValues() {
918 initAnimation();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700919
920 final Object target = getTarget();
921 if (target != null) {
922 final int numValues = mValues.length;
923 for (int i = 0; i < numValues; ++i) {
924 mValues[i].setupStartValue(target);
925 }
Chet Haase21cd1382010-09-01 17:42:29 -0700926 }
927 }
928
929 @Override
930 public void setupEndValues() {
931 initAnimation();
Alan Viverette87ac5f62014-06-04 16:39:21 -0700932
933 final Object target = getTarget();
934 if (target != null) {
935 final int numValues = mValues.length;
936 for (int i = 0; i < numValues; ++i) {
937 mValues[i].setupEndValue(target);
938 }
Chet Haase21cd1382010-09-01 17:42:29 -0700939 }
940 }
941
Chet Haasef54a8d72010-07-22 14:44:59 -0700942 /**
Chet Haase17fb4b02010-06-28 17:55:07 -0700943 * This method is called with the elapsed fraction of the animation during every
944 * animation frame. This function turns the elapsed fraction into an interpolated fraction
945 * and then into an animated value (from the evaluator. The function is called mostly during
946 * animation updates, but it is also called when the <code>end()</code>
947 * function is called, to set the final value on the property.
948 *
949 * <p>Overrides of this method must call the superclass to perform the calculation
950 * of the animated value.</p>
951 *
952 * @param fraction The elapsed fraction of the animation.
953 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800954 @CallSuper
Chet Haase17fb4b02010-06-28 17:55:07 -0700955 @Override
956 void animateValue(float fraction) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700957 final Object target = getTarget();
958 if (mTarget != null && target == null) {
959 // We lost the target reference, cancel and clean up.
960 cancel();
961 return;
962 }
963
Chet Haase17fb4b02010-06-28 17:55:07 -0700964 super.animateValue(fraction);
Chet Haase602e4d32010-08-16 08:57:23 -0700965 int numValues = mValues.length;
966 for (int i = 0; i < numValues; ++i) {
Alan Viverette87ac5f62014-06-04 16:39:21 -0700967 mValues[i].setAnimatedValue(target);
Chet Haase17fb4b02010-06-28 17:55:07 -0700968 }
969 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700970
971 @Override
Chet Haasea18a86b2010-09-07 13:20:00 -0700972 public ObjectAnimator clone() {
973 final ObjectAnimator anim = (ObjectAnimator) super.clone();
Chet Haase49afa5b2010-08-23 11:39:53 -0700974 return anim;
975 }
Chet Haasee9140a72011-02-16 16:23:29 -0800976
977 @Override
Alan Viverette87ac5f62014-06-04 16:39:21 -0700978 @NonNull
Chet Haasee9140a72011-02-16 16:23:29 -0800979 public String toString() {
980 String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " +
Alan Viverette87ac5f62014-06-04 16:39:21 -0700981 getTarget();
Chet Haasee9140a72011-02-16 16:23:29 -0800982 if (mValues != null) {
983 for (int i = 0; i < mValues.length; ++i) {
984 returnVal += "\n " + mValues[i].toString();
985 }
986 }
987 return returnVal;
988 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700989}