blob: f18b7acf64f0d3475c3d1cbc25f4c89d95bad54b [file] [log] [blame]
Chet Haasea00f3862011-02-22 06:34:40 -08001/*
Chet Haased666cf32011-03-01 07:31:30 -08002 * Copyright (C) 2011 The Android Open Source Project
Chet Haasea00f3862011-02-22 06:34:40 -08003 *
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.view;
18
19import android.animation.Animator;
20import android.animation.ValueAnimator;
21import android.animation.TimeInterpolator;
Chet Haasea00f3862011-02-22 06:34:40 -080022import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.Set;
25
26/**
27 * This class enables automatic and optimized animation of select properties on View objects.
28 * If only one or two properties on a View object are being animated, then using an
29 * {@link android.animation.ObjectAnimator} is fine; the property setters called by ObjectAnimator
30 * are well equipped to do the right thing to set the property and invalidate the view
31 * appropriately. But if several properties are animated simultaneously, or if you just want a
32 * more convenient syntax to animate a specific property, then ViewPropertyAnimator might be
33 * more well-suited to the task.
34 *
Chet Haased666cf32011-03-01 07:31:30 -080035 * <p>This class may provide better performance for several simultaneous animations, because
36 * it will optimize invalidate calls to take place only once for several properties instead of each
37 * animated property independently causing its own invalidation. Also, the syntax of using this
Chet Haasea00f3862011-02-22 06:34:40 -080038 * class could be easier to use because the caller need only tell the View object which
Chet Haased666cf32011-03-01 07:31:30 -080039 * property to animate, and the value to animate either to or by, and this class handles the
Chet Haasea00f3862011-02-22 06:34:40 -080040 * details of configuring the underlying Animator class and starting it.</p>
41 *
42 * <p>This class is not constructed by the caller, but rather by the View whose properties
43 * it will animate. Calls to {@link android.view.View#animate()} will return a reference
44 * to the appropriate ViewPropertyAnimator object for that View.</p>
45 *
46 */
47public class ViewPropertyAnimator {
48
49 /**
50 * The View whose properties are being animated by this class. This is set at
51 * construction time.
52 */
John Reck44244ff2014-06-20 14:44:58 -070053 final View mView;
Chet Haasea00f3862011-02-22 06:34:40 -080054
55 /**
56 * The duration of the underlying Animator object. By default, we don't set the duration
57 * on the Animator and just use its default duration. If the duration is ever set on this
58 * Animator, then we use the duration that it was set to.
59 */
60 private long mDuration;
61
62 /**
63 * A flag indicating whether the duration has been set on this object. If not, we don't set
64 * the duration on the underlying Animator, but instead just use its default duration.
65 */
66 private boolean mDurationSet = false;
67
68 /**
Chet Haase8d5f8082011-05-24 08:03:16 -070069 * The startDelay of the underlying Animator object. By default, we don't set the startDelay
70 * on the Animator and just use its default startDelay. If the startDelay is ever set on this
71 * Animator, then we use the startDelay that it was set to.
72 */
73 private long mStartDelay = 0;
74
75 /**
76 * A flag indicating whether the startDelay has been set on this object. If not, we don't set
77 * the startDelay on the underlying Animator, but instead just use its default startDelay.
78 */
79 private boolean mStartDelaySet = false;
80
81 /**
Chet Haasea00f3862011-02-22 06:34:40 -080082 * The interpolator of the underlying Animator object. By default, we don't set the interpolator
Vladislav Kaznacheev78f146f2015-06-15 06:58:59 +000083 * on the Animator and just use its default interpolator. If the interpolator is ever set on
84 * this Animator, then we use the interpolator that it was set to.
Chet Haasea00f3862011-02-22 06:34:40 -080085 */
86 private TimeInterpolator mInterpolator;
87
88 /**
Vladislav Kaznacheev78f146f2015-06-15 06:58:59 +000089 * A flag indicating whether the interpolator has been set on this object. If not, we don't set
90 * the interpolator on the underlying Animator, but instead just use its default interpolator.
91 */
92 private boolean mInterpolatorSet = false;
93
94 /**
Chet Haase87f4ae62013-09-06 18:29:56 -070095 * Listener for the lifecycle events of the underlying ValueAnimator object.
Chet Haasea00f3862011-02-22 06:34:40 -080096 */
97 private Animator.AnimatorListener mListener = null;
98
99 /**
Chet Haase87f4ae62013-09-06 18:29:56 -0700100 * Listener for the update events of the underlying ValueAnimator object.
101 */
102 private ValueAnimator.AnimatorUpdateListener mUpdateListener = null;
103
104 /**
Cyril Mottierd40acfb2013-07-25 10:08:29 +0200105 * A lazily-created ValueAnimator used in order to get some default animator properties
106 * (duration, start delay, interpolator, etc.).
107 */
108 private ValueAnimator mTempValueAnimator;
109
110 /**
John Reck918988c2014-05-19 10:28:35 -0700111 * A RenderThread-driven backend that may intercept startAnimation
112 */
113 private ViewPropertyAnimatorRT mRTBackend;
114
115 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800116 * This listener is the mechanism by which the underlying Animator causes changes to the
117 * properties currently being animated, as well as the cleanup after an animation is
118 * complete.
119 */
120 private AnimatorEventListener mAnimatorEventListener = new AnimatorEventListener();
121
122 /**
123 * This list holds the properties that have been asked to animate. We allow the caller to
124 * request several animations prior to actually starting the underlying animator. This
125 * enables us to run one single animator to handle several properties in parallel. Each
126 * property is tossed onto the pending list until the animation actually starts (which is
127 * done by posting it onto mView), at which time the pending list is cleared and the properties
128 * on that list are added to the list of properties associated with that animator.
129 */
130 ArrayList<NameValuesHolder> mPendingAnimations = new ArrayList<NameValuesHolder>();
Chet Haasec1ca6652012-01-31 07:33:48 -0800131 private Runnable mPendingSetupAction;
132 private Runnable mPendingCleanupAction;
133 private Runnable mPendingOnStartAction;
134 private Runnable mPendingOnEndAction;
Chet Haasea00f3862011-02-22 06:34:40 -0800135
136 /**
137 * Constants used to associate a property being requested and the mechanism used to set
Chet Haased666cf32011-03-01 07:31:30 -0800138 * the property (this class calls directly into View to set the properties in question).
Chet Haasea00f3862011-02-22 06:34:40 -0800139 */
John Recke45b1fd2014-04-15 09:50:16 -0700140 static final int NONE = 0x0000;
141 static final int TRANSLATION_X = 0x0001;
142 static final int TRANSLATION_Y = 0x0002;
143 static final int TRANSLATION_Z = 0x0004;
144 static final int SCALE_X = 0x0008;
145 static final int SCALE_Y = 0x0010;
146 static final int ROTATION = 0x0020;
147 static final int ROTATION_X = 0x0040;
148 static final int ROTATION_Y = 0x0080;
149 static final int X = 0x0100;
150 static final int Y = 0x0200;
151 static final int Z = 0x0400;
152 static final int ALPHA = 0x0800;
Chet Haasea00f3862011-02-22 06:34:40 -0800153
Chris Craikf57776b2013-10-25 18:30:17 -0700154 private static final int TRANSFORM_MASK = TRANSLATION_X | TRANSLATION_Y | TRANSLATION_Z |
Chris Craikcc39e162014-04-25 18:34:11 -0700155 SCALE_X | SCALE_Y | ROTATION | ROTATION_X | ROTATION_Y | X | Y | Z;
Chet Haasea00f3862011-02-22 06:34:40 -0800156
157 /**
158 * The mechanism by which the user can request several properties that are then animated
159 * together works by posting this Runnable to start the underlying Animator. Every time
160 * a property animation is requested, we cancel any previous postings of the Runnable
161 * and re-post it. This means that we will only ever run the Runnable (and thus start the
162 * underlying animator) after the caller is done setting the properties that should be
163 * animated together.
164 */
165 private Runnable mAnimationStarter = new Runnable() {
166 @Override
167 public void run() {
168 startAnimation();
169 }
170 };
171
172 /**
173 * This class holds information about the overall animation being run on the set of
174 * properties. The mask describes which properties are being animated and the
175 * values holder is the list of all property/value objects.
176 */
177 private static class PropertyBundle {
178 int mPropertyMask;
179 ArrayList<NameValuesHolder> mNameValuesHolder;
Chet Haaseba592d202011-02-25 11:35:17 -0800180
Chet Haasea00f3862011-02-22 06:34:40 -0800181 PropertyBundle(int propertyMask, ArrayList<NameValuesHolder> nameValuesHolder) {
182 mPropertyMask = propertyMask;
183 mNameValuesHolder = nameValuesHolder;
184 }
Chet Haaseba592d202011-02-25 11:35:17 -0800185
186 /**
187 * Removes the given property from being animated as a part of this
188 * PropertyBundle. If the property was a part of this bundle, it returns
189 * true to indicate that it was, in fact, canceled. This is an indication
190 * to the caller that a cancellation actually occurred.
191 *
192 * @param propertyConstant The property whose cancellation is requested.
193 * @return true if the given property is a part of this bundle and if it
194 * has therefore been canceled.
195 */
196 boolean cancel(int propertyConstant) {
197 if ((mPropertyMask & propertyConstant) != 0 && mNameValuesHolder != null) {
198 int count = mNameValuesHolder.size();
199 for (int i = 0; i < count; ++i) {
200 NameValuesHolder nameValuesHolder = mNameValuesHolder.get(i);
201 if (nameValuesHolder.mNameConstant == propertyConstant) {
202 mNameValuesHolder.remove(i);
203 mPropertyMask &= ~propertyConstant;
204 return true;
205 }
206 }
207 }
208 return false;
209 }
Chet Haasea00f3862011-02-22 06:34:40 -0800210 }
211
212 /**
213 * This list tracks the list of properties being animated by any particular animator.
214 * In most situations, there would only ever be one animator running at a time. But it is
215 * possible to request some properties to animate together, then while those properties
216 * are animating, to request some other properties to animate together. The way that
217 * works is by having this map associate the group of properties being animated with the
218 * animator handling the animation. On every update event for an Animator, we ask the
219 * map for the associated properties and set them accordingly.
220 */
221 private HashMap<Animator, PropertyBundle> mAnimatorMap =
222 new HashMap<Animator, PropertyBundle>();
Chet Haasec1ca6652012-01-31 07:33:48 -0800223 private HashMap<Animator, Runnable> mAnimatorSetupMap;
224 private HashMap<Animator, Runnable> mAnimatorCleanupMap;
225 private HashMap<Animator, Runnable> mAnimatorOnStartMap;
226 private HashMap<Animator, Runnable> mAnimatorOnEndMap;
Chet Haasea00f3862011-02-22 06:34:40 -0800227
228 /**
229 * This is the information we need to set each property during the animation.
230 * mNameConstant is used to set the appropriate field in View, and the from/delta
231 * values are used to calculate the animated value for a given animation fraction
232 * during the animation.
233 */
John Reck918988c2014-05-19 10:28:35 -0700234 static class NameValuesHolder {
Chet Haasea00f3862011-02-22 06:34:40 -0800235 int mNameConstant;
236 float mFromValue;
237 float mDeltaValue;
238 NameValuesHolder(int nameConstant, float fromValue, float deltaValue) {
239 mNameConstant = nameConstant;
240 mFromValue = fromValue;
241 mDeltaValue = deltaValue;
242 }
243 }
244
245 /**
246 * Constructor, called by View. This is private by design, as the user should only
247 * get a ViewPropertyAnimator by calling View.animate().
248 *
249 * @param view The View associated with this ViewPropertyAnimator
250 */
251 ViewPropertyAnimator(View view) {
252 mView = view;
Dianne Hackbornddb715b2011-09-09 14:43:39 -0700253 view.ensureTransformationInfo();
Chet Haasea00f3862011-02-22 06:34:40 -0800254 }
255
256 /**
257 * Sets the duration for the underlying animator that animates the requested properties.
258 * By default, the animator uses the default value for ValueAnimator. Calling this method
259 * will cause the declared value to be used instead.
260 * @param duration The length of ensuing property animations, in milliseconds. The value
261 * cannot be negative.
262 * @return This object, allowing calls to methods in this class to be chained.
263 */
264 public ViewPropertyAnimator setDuration(long duration) {
265 if (duration < 0) {
266 throw new IllegalArgumentException("Animators cannot have negative duration: " +
267 duration);
268 }
269 mDurationSet = true;
270 mDuration = duration;
271 return this;
272 }
273
274 /**
Chet Haase8d5f8082011-05-24 08:03:16 -0700275 * Returns the current duration of property animations. If the duration was set on this
276 * object, that value is returned. Otherwise, the default value of the underlying Animator
277 * is returned.
278 *
279 * @see #setDuration(long)
280 * @return The duration of animations, in milliseconds.
281 */
282 public long getDuration() {
Chet Haasecbbd93a2011-08-08 17:34:25 -0700283 if (mDurationSet) {
284 return mDuration;
Chet Haase8d5f8082011-05-24 08:03:16 -0700285 } else {
286 // Just return the default from ValueAnimator, since that's what we'd get if
287 // the value has not been set otherwise
Cyril Mottierd40acfb2013-07-25 10:08:29 +0200288 if (mTempValueAnimator == null) {
289 mTempValueAnimator = new ValueAnimator();
290 }
291 return mTempValueAnimator.getDuration();
Chet Haase8d5f8082011-05-24 08:03:16 -0700292 }
293 }
294
295 /**
296 * Returns the current startDelay of property animations. If the startDelay was set on this
297 * object, that value is returned. Otherwise, the default value of the underlying Animator
298 * is returned.
299 *
300 * @see #setStartDelay(long)
301 * @return The startDelay of animations, in milliseconds.
302 */
303 public long getStartDelay() {
304 if (mStartDelaySet) {
305 return mStartDelay;
306 } else {
307 // Just return the default from ValueAnimator (0), since that's what we'd get if
308 // the value has not been set otherwise
309 return 0;
310 }
311 }
312
313 /**
314 * Sets the startDelay for the underlying animator that animates the requested properties.
315 * By default, the animator uses the default value for ValueAnimator. Calling this method
316 * will cause the declared value to be used instead.
317 * @param startDelay The delay of ensuing property animations, in milliseconds. The value
318 * cannot be negative.
319 * @return This object, allowing calls to methods in this class to be chained.
320 */
321 public ViewPropertyAnimator setStartDelay(long startDelay) {
322 if (startDelay < 0) {
Cyril Mottier772bafa2013-10-02 16:57:57 +0200323 throw new IllegalArgumentException("Animators cannot have negative start " +
324 "delay: " + startDelay);
Chet Haase8d5f8082011-05-24 08:03:16 -0700325 }
326 mStartDelaySet = true;
327 mStartDelay = startDelay;
328 return this;
329 }
330
331 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800332 * Sets the interpolator for the underlying animator that animates the requested properties.
333 * By default, the animator uses the default interpolator for ValueAnimator. Calling this method
334 * will cause the declared object to be used instead.
335 *
John Recka0b13bd2014-07-01 08:34:47 -0700336 * @param interpolator The TimeInterpolator to be used for ensuing property animations. A value
337 * of <code>null</code> will result in linear interpolation.
Chet Haasea00f3862011-02-22 06:34:40 -0800338 * @return This object, allowing calls to methods in this class to be chained.
339 */
340 public ViewPropertyAnimator setInterpolator(TimeInterpolator interpolator) {
Vladislav Kaznacheev78f146f2015-06-15 06:58:59 +0000341 mInterpolatorSet = true;
Chet Haasea00f3862011-02-22 06:34:40 -0800342 mInterpolator = interpolator;
343 return this;
344 }
345
346 /**
Chet Haase430742f2013-04-12 11:18:36 -0700347 * Returns the timing interpolator that this animation uses.
348 *
349 * @return The timing interpolator for this animation.
350 */
351 public TimeInterpolator getInterpolator() {
Vladislav Kaznacheev78f146f2015-06-15 06:58:59 +0000352 if (mInterpolatorSet) {
Cyril Mottierd40acfb2013-07-25 10:08:29 +0200353 return mInterpolator;
354 } else {
355 // Just return the default from ValueAnimator, since that's what we'd get if
356 // the value has not been set otherwise
357 if (mTempValueAnimator == null) {
358 mTempValueAnimator = new ValueAnimator();
359 }
360 return mTempValueAnimator.getInterpolator();
361 }
Chet Haase430742f2013-04-12 11:18:36 -0700362 }
363
364 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800365 * Sets a listener for events in the underlying Animators that run the property
366 * animations.
367 *
Chet Haase87f4ae62013-09-06 18:29:56 -0700368 * @see Animator.AnimatorListener
369 *
370 * @param listener The listener to be called with AnimatorListener events. A value of
371 * <code>null</code> removes any existing listener.
Chet Haasea00f3862011-02-22 06:34:40 -0800372 * @return This object, allowing calls to methods in this class to be chained.
373 */
374 public ViewPropertyAnimator setListener(Animator.AnimatorListener listener) {
375 mListener = listener;
376 return this;
377 }
378
John Reck918988c2014-05-19 10:28:35 -0700379 Animator.AnimatorListener getListener() {
380 return mListener;
381 }
382
Chet Haasea00f3862011-02-22 06:34:40 -0800383 /**
Chet Haase87f4ae62013-09-06 18:29:56 -0700384 * Sets a listener for update events in the underlying ValueAnimator that runs
385 * the property animations. Note that the underlying animator is animating between
386 * 0 and 1 (these values are then turned into the actual property values internally
387 * by ViewPropertyAnimator). So the animator cannot give information on the current
388 * values of the properties being animated by this ViewPropertyAnimator, although
389 * the view object itself can be queried to get the current values.
390 *
391 * @see android.animation.ValueAnimator.AnimatorUpdateListener
392 *
393 * @param listener The listener to be called with update events. A value of
394 * <code>null</code> removes any existing listener.
395 * @return This object, allowing calls to methods in this class to be chained.
396 */
397 public ViewPropertyAnimator setUpdateListener(ValueAnimator.AnimatorUpdateListener listener) {
398 mUpdateListener = listener;
399 return this;
400 }
401
John Reck918988c2014-05-19 10:28:35 -0700402 ValueAnimator.AnimatorUpdateListener getUpdateListener() {
403 return mUpdateListener;
404 }
405
Chet Haase87f4ae62013-09-06 18:29:56 -0700406 /**
Chet Haase8d5f8082011-05-24 08:03:16 -0700407 * Starts the currently pending property animations immediately. Calling <code>start()</code>
408 * is optional because all animations start automatically at the next opportunity. However,
409 * if the animations are needed to start immediately and synchronously (not at the time when
410 * the next event is processed by the hierarchy, which is when the animations would begin
411 * otherwise), then this method can be used.
412 */
413 public void start() {
Michael Jurka500998d2012-05-13 15:35:02 -0700414 mView.removeCallbacks(mAnimationStarter);
Chet Haase8d5f8082011-05-24 08:03:16 -0700415 startAnimation();
416 }
417
418 /**
419 * Cancels all property animations that are currently running or pending.
420 */
421 public void cancel() {
422 if (mAnimatorMap.size() > 0) {
423 HashMap<Animator, PropertyBundle> mAnimatorMapCopy =
424 (HashMap<Animator, PropertyBundle>)mAnimatorMap.clone();
425 Set<Animator> animatorSet = mAnimatorMapCopy.keySet();
426 for (Animator runningAnim : animatorSet) {
427 runningAnim.cancel();
428 }
429 }
430 mPendingAnimations.clear();
Chet Haase5637b7d2014-08-28 07:53:59 -0700431 mPendingSetupAction = null;
432 mPendingCleanupAction = null;
433 mPendingOnStartAction = null;
434 mPendingOnEndAction = null;
Chet Haase3a000a52011-06-16 13:55:11 -0700435 mView.removeCallbacks(mAnimationStarter);
John Reck22184722014-06-20 07:19:30 -0700436 if (mRTBackend != null) {
437 mRTBackend.cancelAll();
438 }
Chet Haase8d5f8082011-05-24 08:03:16 -0700439 }
440
441 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800442 * This method will cause the View's <code>x</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800443 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800444 *
445 * @param value The value to be animated to.
446 * @see View#setX(float)
447 * @return This object, allowing calls to methods in this class to be chained.
448 */
449 public ViewPropertyAnimator x(float value) {
450 animateProperty(X, value);
451 return this;
452 }
453
454 /**
455 * This method will cause the View's <code>x</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800456 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800457 *
458 * @param value The amount to be animated by, as an offset from the current value.
459 * @see View#setX(float)
460 * @return This object, allowing calls to methods in this class to be chained.
461 */
462 public ViewPropertyAnimator xBy(float value) {
463 animatePropertyBy(X, value);
464 return this;
465 }
466
467 /**
468 * This method will cause the View's <code>y</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800469 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800470 *
471 * @param value The value to be animated to.
472 * @see View#setY(float)
473 * @return This object, allowing calls to methods in this class to be chained.
474 */
475 public ViewPropertyAnimator y(float value) {
476 animateProperty(Y, value);
477 return this;
478 }
479
480 /**
481 * This method will cause the View's <code>y</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800482 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800483 *
484 * @param value The amount to be animated by, as an offset from the current value.
485 * @see View#setY(float)
486 * @return This object, allowing calls to methods in this class to be chained.
487 */
488 public ViewPropertyAnimator yBy(float value) {
489 animatePropertyBy(Y, value);
490 return this;
491 }
492
493 /**
Chris Craikcc39e162014-04-25 18:34:11 -0700494 * This method will cause the View's <code>z</code> property to be animated to the
495 * specified value. Animations already running on the property will be canceled.
496 *
497 * @param value The value to be animated to.
498 * @see View#setZ(float)
499 * @return This object, allowing calls to methods in this class to be chained.
500 */
501 public ViewPropertyAnimator z(float value) {
502 animateProperty(Z, value);
503 return this;
504 }
505
506 /**
507 * This method will cause the View's <code>z</code> property to be animated by the
508 * specified value. Animations already running on the property will be canceled.
509 *
510 * @param value The amount to be animated by, as an offset from the current value.
511 * @see View#setZ(float)
512 * @return This object, allowing calls to methods in this class to be chained.
513 */
514 public ViewPropertyAnimator zBy(float value) {
515 animatePropertyBy(Z, value);
516 return this;
517 }
518
519 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800520 * This method will cause the View's <code>rotation</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800521 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800522 *
523 * @param value The value to be animated to.
524 * @see View#setRotation(float)
525 * @return This object, allowing calls to methods in this class to be chained.
526 */
527 public ViewPropertyAnimator rotation(float value) {
528 animateProperty(ROTATION, value);
529 return this;
530 }
531
532 /**
533 * This method will cause the View's <code>rotation</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800534 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800535 *
536 * @param value The amount to be animated by, as an offset from the current value.
537 * @see View#setRotation(float)
538 * @return This object, allowing calls to methods in this class to be chained.
539 */
540 public ViewPropertyAnimator rotationBy(float value) {
541 animatePropertyBy(ROTATION, value);
542 return this;
543 }
544
545 /**
546 * This method will cause the View's <code>rotationX</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800547 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800548 *
549 * @param value The value to be animated to.
550 * @see View#setRotationX(float)
551 * @return This object, allowing calls to methods in this class to be chained.
552 */
553 public ViewPropertyAnimator rotationX(float value) {
554 animateProperty(ROTATION_X, value);
555 return this;
556 }
557
558 /**
559 * This method will cause the View's <code>rotationX</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800560 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800561 *
562 * @param value The amount to be animated by, as an offset from the current value.
563 * @see View#setRotationX(float)
564 * @return This object, allowing calls to methods in this class to be chained.
565 */
566 public ViewPropertyAnimator rotationXBy(float value) {
567 animatePropertyBy(ROTATION_X, value);
568 return this;
569 }
570
571 /**
572 * This method will cause the View's <code>rotationY</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800573 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800574 *
575 * @param value The value to be animated to.
576 * @see View#setRotationY(float)
577 * @return This object, allowing calls to methods in this class to be chained.
578 */
579 public ViewPropertyAnimator rotationY(float value) {
580 animateProperty(ROTATION_Y, value);
581 return this;
582 }
583
584 /**
585 * This method will cause the View's <code>rotationY</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800586 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800587 *
588 * @param value The amount to be animated by, as an offset from the current value.
589 * @see View#setRotationY(float)
590 * @return This object, allowing calls to methods in this class to be chained.
591 */
592 public ViewPropertyAnimator rotationYBy(float value) {
593 animatePropertyBy(ROTATION_Y, value);
594 return this;
595 }
596
597 /**
598 * This method will cause the View's <code>translationX</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800599 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800600 *
601 * @param value The value to be animated to.
602 * @see View#setTranslationX(float)
603 * @return This object, allowing calls to methods in this class to be chained.
604 */
605 public ViewPropertyAnimator translationX(float value) {
606 animateProperty(TRANSLATION_X, value);
607 return this;
608 }
609
610 /**
611 * This method will cause the View's <code>translationX</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800612 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800613 *
614 * @param value The amount to be animated by, as an offset from the current value.
615 * @see View#setTranslationX(float)
616 * @return This object, allowing calls to methods in this class to be chained.
617 */
618 public ViewPropertyAnimator translationXBy(float value) {
619 animatePropertyBy(TRANSLATION_X, value);
620 return this;
621 }
622
623 /**
624 * This method will cause the View's <code>translationY</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800625 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800626 *
627 * @param value The value to be animated to.
628 * @see View#setTranslationY(float)
629 * @return This object, allowing calls to methods in this class to be chained.
630 */
631 public ViewPropertyAnimator translationY(float value) {
632 animateProperty(TRANSLATION_Y, value);
633 return this;
634 }
635
636 /**
637 * This method will cause the View's <code>translationY</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800638 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800639 *
640 * @param value The amount to be animated by, as an offset from the current value.
641 * @see View#setTranslationY(float)
642 * @return This object, allowing calls to methods in this class to be chained.
643 */
644 public ViewPropertyAnimator translationYBy(float value) {
645 animatePropertyBy(TRANSLATION_Y, value);
646 return this;
647 }
648
649 /**
Chris Craikd863a102013-12-19 13:31:15 -0800650 * This method will cause the View's <code>translationZ</code> property to be animated to the
651 * specified value. Animations already running on the property will be canceled.
652 *
653 * @param value The value to be animated to.
654 * @see View#setTranslationZ(float)
655 * @return This object, allowing calls to methods in this class to be chained.
656 */
657 public ViewPropertyAnimator translationZ(float value) {
658 animateProperty(TRANSLATION_Z, value);
659 return this;
660 }
661
662 /**
663 * This method will cause the View's <code>translationZ</code> property to be animated by the
664 * specified value. Animations already running on the property will be canceled.
665 *
666 * @param value The amount to be animated by, as an offset from the current value.
667 * @see View#setTranslationZ(float)
668 * @return This object, allowing calls to methods in this class to be chained.
669 */
670 public ViewPropertyAnimator translationZBy(float value) {
671 animatePropertyBy(TRANSLATION_Z, value);
672 return this;
673 }
674 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800675 * This method will cause the View's <code>scaleX</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800676 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800677 *
678 * @param value The value to be animated to.
679 * @see View#setScaleX(float)
680 * @return This object, allowing calls to methods in this class to be chained.
681 */
682 public ViewPropertyAnimator scaleX(float value) {
683 animateProperty(SCALE_X, value);
684 return this;
685 }
686
687 /**
688 * This method will cause the View's <code>scaleX</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800689 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800690 *
691 * @param value The amount to be animated by, as an offset from the current value.
692 * @see View#setScaleX(float)
693 * @return This object, allowing calls to methods in this class to be chained.
694 */
695 public ViewPropertyAnimator scaleXBy(float value) {
696 animatePropertyBy(SCALE_X, value);
697 return this;
698 }
699
700 /**
701 * This method will cause the View's <code>scaleY</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800702 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800703 *
704 * @param value The value to be animated to.
705 * @see View#setScaleY(float)
706 * @return This object, allowing calls to methods in this class to be chained.
707 */
708 public ViewPropertyAnimator scaleY(float value) {
709 animateProperty(SCALE_Y, value);
710 return this;
711 }
712
713 /**
714 * This method will cause the View's <code>scaleY</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800715 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800716 *
717 * @param value The amount to be animated by, as an offset from the current value.
718 * @see View#setScaleY(float)
719 * @return This object, allowing calls to methods in this class to be chained.
720 */
721 public ViewPropertyAnimator scaleYBy(float value) {
722 animatePropertyBy(SCALE_Y, value);
723 return this;
724 }
725
726 /**
727 * This method will cause the View's <code>alpha</code> property to be animated to the
Chet Haased666cf32011-03-01 07:31:30 -0800728 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800729 *
730 * @param value The value to be animated to.
731 * @see View#setAlpha(float)
732 * @return This object, allowing calls to methods in this class to be chained.
733 */
734 public ViewPropertyAnimator alpha(float value) {
735 animateProperty(ALPHA, value);
736 return this;
737 }
738
739 /**
740 * This method will cause the View's <code>alpha</code> property to be animated by the
Chet Haased666cf32011-03-01 07:31:30 -0800741 * specified value. Animations already running on the property will be canceled.
Chet Haasea00f3862011-02-22 06:34:40 -0800742 *
743 * @param value The amount to be animated by, as an offset from the current value.
744 * @see View#setAlpha(float)
745 * @return This object, allowing calls to methods in this class to be chained.
746 */
747 public ViewPropertyAnimator alphaBy(float value) {
748 animatePropertyBy(ALPHA, value);
749 return this;
750 }
751
752 /**
Chet Haasec1ca6652012-01-31 07:33:48 -0800753 * The View associated with this ViewPropertyAnimator will have its
754 * {@link View#setLayerType(int, android.graphics.Paint) layer type} set to
Chet Haasecb150fe2012-05-03 15:15:05 -0700755 * {@link View#LAYER_TYPE_HARDWARE} for the duration of the next animation.
756 * As stated in the documentation for {@link View#LAYER_TYPE_HARDWARE},
757 * the actual type of layer used internally depends on the runtime situation of the
758 * view. If the activity and this view are hardware-accelerated, then the layer will be
759 * accelerated as well. If the activity or the view is not accelerated, then the layer will
760 * effectively be the same as {@link View#LAYER_TYPE_SOFTWARE}.
761 *
762 * <p>This state is not persistent, either on the View or on this ViewPropertyAnimator: the
763 * layer type of the View will be restored when the animation ends to what it was when this
764 * method was called, and this setting on ViewPropertyAnimator is only valid for the next
765 * animation. Note that calling this method and then independently setting the layer type of
766 * the View (by a direct call to {@link View#setLayerType(int, android.graphics.Paint)}) will
767 * result in some inconsistency, including having the layer type restored to its pre-withLayer()
768 * value when the animation ends.</p>
Chet Haasec1ca6652012-01-31 07:33:48 -0800769 *
770 * @see View#setLayerType(int, android.graphics.Paint)
771 * @return This object, allowing calls to methods in this class to be chained.
772 */
773 public ViewPropertyAnimator withLayer() {
774 mPendingSetupAction= new Runnable() {
775 @Override
776 public void run() {
777 mView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Chet Haase5fd37232013-09-13 19:01:52 -0700778 if (mView.isAttachedToWindow()) {
779 mView.buildLayer();
780 }
Chet Haasec1ca6652012-01-31 07:33:48 -0800781 }
782 };
783 final int currentLayerType = mView.getLayerType();
784 mPendingCleanupAction = new Runnable() {
785 @Override
786 public void run() {
787 mView.setLayerType(currentLayerType, null);
788 }
789 };
790 if (mAnimatorSetupMap == null) {
791 mAnimatorSetupMap = new HashMap<Animator, Runnable>();
792 }
793 if (mAnimatorCleanupMap == null) {
794 mAnimatorCleanupMap = new HashMap<Animator, Runnable>();
795 }
796
797 return this;
798 }
799
800 /**
801 * Specifies an action to take place when the next animation runs. If there is a
802 * {@link #setStartDelay(long) startDelay} set on this ViewPropertyAnimator, then the
803 * action will run after that startDelay expires, when the actual animation begins.
804 * This method, along with {@link #withEndAction(Runnable)}, is intended to help facilitate
805 * choreographing ViewPropertyAnimator animations with other animations or actions
806 * in the application.
807 *
808 * @param runnable The action to run when the next animation starts.
809 * @return This object, allowing calls to methods in this class to be chained.
810 */
811 public ViewPropertyAnimator withStartAction(Runnable runnable) {
812 mPendingOnStartAction = runnable;
813 if (runnable != null && mAnimatorOnStartMap == null) {
814 mAnimatorOnStartMap = new HashMap<Animator, Runnable>();
815 }
816 return this;
817 }
818
819 /**
820 * Specifies an action to take place when the next animation ends. The action is only
821 * run if the animation ends normally; if the ViewPropertyAnimator is canceled during
822 * that animation, the runnable will not run.
823 * This method, along with {@link #withStartAction(Runnable)}, is intended to help facilitate
824 * choreographing ViewPropertyAnimator animations with other animations or actions
825 * in the application.
826 *
827 * <p>For example, the following code animates a view to x=200 and then back to 0:</p>
828 * <pre>
829 * Runnable endAction = new Runnable() {
830 * public void run() {
831 * view.animate().x(0);
832 * }
833 * };
Robert Ly8ee2a702012-12-17 15:00:08 -0800834 * view.animate().x(200).withEndAction(endAction);
Chet Haasec1ca6652012-01-31 07:33:48 -0800835 * </pre>
836 *
837 * @param runnable The action to run when the next animation ends.
838 * @return This object, allowing calls to methods in this class to be chained.
839 */
840 public ViewPropertyAnimator withEndAction(Runnable runnable) {
841 mPendingOnEndAction = runnable;
842 if (runnable != null && mAnimatorOnEndMap == null) {
843 mAnimatorOnEndMap = new HashMap<Animator, Runnable>();
844 }
845 return this;
846 }
847
John Reck918988c2014-05-19 10:28:35 -0700848 boolean hasActions() {
849 return mPendingSetupAction != null
850 || mPendingCleanupAction != null
851 || mPendingOnStartAction != null
852 || mPendingOnEndAction != null;
853 }
854
Chet Haasec1ca6652012-01-31 07:33:48 -0800855 /**
Chet Haasea00f3862011-02-22 06:34:40 -0800856 * Starts the underlying Animator for a set of properties. We use a single animator that
857 * simply runs from 0 to 1, and then use that fractional value to set each property
858 * value accordingly.
859 */
860 private void startAnimation() {
John Reck918988c2014-05-19 10:28:35 -0700861 if (mRTBackend != null && mRTBackend.startAnimation(this)) {
862 return;
863 }
Chet Haase563d4f22012-04-18 16:20:08 -0700864 mView.setHasTransientState(true);
Chet Haasea00f3862011-02-22 06:34:40 -0800865 ValueAnimator animator = ValueAnimator.ofFloat(1.0f);
866 ArrayList<NameValuesHolder> nameValueList =
867 (ArrayList<NameValuesHolder>) mPendingAnimations.clone();
868 mPendingAnimations.clear();
869 int propertyMask = 0;
870 int propertyCount = nameValueList.size();
871 for (int i = 0; i < propertyCount; ++i) {
872 NameValuesHolder nameValuesHolder = nameValueList.get(i);
873 propertyMask |= nameValuesHolder.mNameConstant;
874 }
Chet Haasea00f3862011-02-22 06:34:40 -0800875 mAnimatorMap.put(animator, new PropertyBundle(propertyMask, nameValueList));
Chet Haasec1ca6652012-01-31 07:33:48 -0800876 if (mPendingSetupAction != null) {
877 mAnimatorSetupMap.put(animator, mPendingSetupAction);
878 mPendingSetupAction = null;
879 }
880 if (mPendingCleanupAction != null) {
881 mAnimatorCleanupMap.put(animator, mPendingCleanupAction);
882 mPendingCleanupAction = null;
883 }
884 if (mPendingOnStartAction != null) {
885 mAnimatorOnStartMap.put(animator, mPendingOnStartAction);
886 mPendingOnStartAction = null;
887 }
888 if (mPendingOnEndAction != null) {
889 mAnimatorOnEndMap.put(animator, mPendingOnEndAction);
890 mPendingOnEndAction = null;
891 }
Chet Haasea00f3862011-02-22 06:34:40 -0800892 animator.addUpdateListener(mAnimatorEventListener);
893 animator.addListener(mAnimatorEventListener);
Chet Haasecbbd93a2011-08-08 17:34:25 -0700894 if (mStartDelaySet) {
895 animator.setStartDelay(mStartDelay);
896 }
Chet Haasea00f3862011-02-22 06:34:40 -0800897 if (mDurationSet) {
898 animator.setDuration(mDuration);
899 }
Vladislav Kaznacheev78f146f2015-06-15 06:58:59 +0000900 if (mInterpolatorSet) {
Chet Haasea00f3862011-02-22 06:34:40 -0800901 animator.setInterpolator(mInterpolator);
902 }
903 animator.start();
904 }
905
906 /**
907 * Utility function, called by the various x(), y(), etc. methods. This stores the
Chet Haased666cf32011-03-01 07:31:30 -0800908 * constant name for the property along with the from/delta values that will be used to
Chet Haasea00f3862011-02-22 06:34:40 -0800909 * calculate and set the property during the animation. This structure is added to the
910 * pending animations, awaiting the eventual start() of the underlying animator. A
911 * Runnable is posted to start the animation, and any pending such Runnable is canceled
912 * (which enables us to end up starting just one animator for all of the properties
913 * specified at one time).
914 *
915 * @param constantName The specifier for the property being animated
916 * @param toValue The value to which the property will animate
917 */
918 private void animateProperty(int constantName, float toValue) {
919 float fromValue = getValue(constantName);
920 float deltaValue = toValue - fromValue;
921 animatePropertyBy(constantName, fromValue, deltaValue);
922 }
923
924 /**
925 * Utility function, called by the various xBy(), yBy(), etc. methods. This method is
926 * just like animateProperty(), except the value is an offset from the property's
927 * current value, instead of an absolute "to" value.
928 *
929 * @param constantName The specifier for the property being animated
930 * @param byValue The amount by which the property will change
931 */
932 private void animatePropertyBy(int constantName, float byValue) {
933 float fromValue = getValue(constantName);
934 animatePropertyBy(constantName, fromValue, byValue);
935 }
936
937 /**
Chet Haased666cf32011-03-01 07:31:30 -0800938 * Utility function, called by animateProperty() and animatePropertyBy(), which handles the
Chet Haasea00f3862011-02-22 06:34:40 -0800939 * details of adding a pending animation and posting the request to start the animation.
940 *
941 * @param constantName The specifier for the property being animated
Chet Haased666cf32011-03-01 07:31:30 -0800942 * @param startValue The starting value of the property
Chet Haasea00f3862011-02-22 06:34:40 -0800943 * @param byValue The amount by which the property will change
944 */
Chet Haased666cf32011-03-01 07:31:30 -0800945 private void animatePropertyBy(int constantName, float startValue, float byValue) {
Chet Haaseba592d202011-02-25 11:35:17 -0800946 // First, cancel any existing animations on this property
947 if (mAnimatorMap.size() > 0) {
948 Animator animatorToCancel = null;
949 Set<Animator> animatorSet = mAnimatorMap.keySet();
950 for (Animator runningAnim : animatorSet) {
951 PropertyBundle bundle = mAnimatorMap.get(runningAnim);
952 if (bundle.cancel(constantName)) {
953 // property was canceled - cancel the animation if it's now empty
954 // Note that it's safe to break out here because every new animation
955 // on a property will cancel a previous animation on that property, so
956 // there can only ever be one such animation running.
957 if (bundle.mPropertyMask == NONE) {
Chet Haase8d5f8082011-05-24 08:03:16 -0700958 // the animation is no longer changing anything - cancel it
Chet Haaseba592d202011-02-25 11:35:17 -0800959 animatorToCancel = runningAnim;
960 break;
961 }
962 }
963 }
964 if (animatorToCancel != null) {
965 animatorToCancel.cancel();
966 }
967 }
968
Chet Haasea00f3862011-02-22 06:34:40 -0800969 NameValuesHolder nameValuePair = new NameValuesHolder(constantName, startValue, byValue);
970 mPendingAnimations.add(nameValuePair);
Chet Haase3a000a52011-06-16 13:55:11 -0700971 mView.removeCallbacks(mAnimationStarter);
Chet Haasedcfbd6a2013-05-09 14:40:21 -0700972 mView.postOnAnimation(mAnimationStarter);
Chet Haasea00f3862011-02-22 06:34:40 -0800973 }
974
975 /**
976 * This method handles setting the property values directly in the View object's fields.
977 * propertyConstant tells it which property should be set, value is the value to set
978 * the property to.
979 *
980 * @param propertyConstant The property to be set
981 * @param value The value to set the property to
982 */
983 private void setValue(int propertyConstant, float value) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -0700984 final View.TransformationInfo info = mView.mTransformationInfo;
Chris Craik64a12e12014-03-28 18:12:12 -0700985 final RenderNode renderNode = mView.mRenderNode;
Chet Haasea00f3862011-02-22 06:34:40 -0800986 switch (propertyConstant) {
987 case TRANSLATION_X:
Chris Craik49e6c732014-03-31 12:34:11 -0700988 renderNode.setTranslationX(value);
Chet Haasea00f3862011-02-22 06:34:40 -0800989 break;
990 case TRANSLATION_Y:
Chris Craik49e6c732014-03-31 12:34:11 -0700991 renderNode.setTranslationY(value);
Chet Haasea00f3862011-02-22 06:34:40 -0800992 break;
Chris Craikf57776b2013-10-25 18:30:17 -0700993 case TRANSLATION_Z:
Chris Craik49e6c732014-03-31 12:34:11 -0700994 renderNode.setTranslationZ(value);
Chris Craikf57776b2013-10-25 18:30:17 -0700995 break;
Chet Haasea00f3862011-02-22 06:34:40 -0800996 case ROTATION:
Chris Craik49e6c732014-03-31 12:34:11 -0700997 renderNode.setRotation(value);
Chet Haasea00f3862011-02-22 06:34:40 -0800998 break;
999 case ROTATION_X:
Chris Craik49e6c732014-03-31 12:34:11 -07001000 renderNode.setRotationX(value);
Chet Haasea00f3862011-02-22 06:34:40 -08001001 break;
1002 case ROTATION_Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001003 renderNode.setRotationY(value);
Chet Haasea00f3862011-02-22 06:34:40 -08001004 break;
1005 case SCALE_X:
Chris Craik49e6c732014-03-31 12:34:11 -07001006 renderNode.setScaleX(value);
Chet Haasea00f3862011-02-22 06:34:40 -08001007 break;
1008 case SCALE_Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001009 renderNode.setScaleY(value);
Chet Haasea00f3862011-02-22 06:34:40 -08001010 break;
1011 case X:
Chris Craik49e6c732014-03-31 12:34:11 -07001012 renderNode.setTranslationX(value - mView.mLeft);
Chet Haasea00f3862011-02-22 06:34:40 -08001013 break;
1014 case Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001015 renderNode.setTranslationY(value - mView.mTop);
Chet Haasea00f3862011-02-22 06:34:40 -08001016 break;
Chris Craikcc39e162014-04-25 18:34:11 -07001017 case Z:
1018 renderNode.setTranslationZ(value - renderNode.getElevation());
1019 break;
Chet Haasea00f3862011-02-22 06:34:40 -08001020 case ALPHA:
Dianne Hackbornddb715b2011-09-09 14:43:39 -07001021 info.mAlpha = value;
Chris Craik49e6c732014-03-31 12:34:11 -07001022 renderNode.setAlpha(value);
Chet Haasea00f3862011-02-22 06:34:40 -08001023 break;
1024 }
1025 }
1026
1027 /**
1028 * This method gets the value of the named property from the View object.
1029 *
1030 * @param propertyConstant The property whose value should be returned
1031 * @return float The value of the named property
1032 */
1033 private float getValue(int propertyConstant) {
Chris Craik49e6c732014-03-31 12:34:11 -07001034 final RenderNode node = mView.mRenderNode;
Chet Haasea00f3862011-02-22 06:34:40 -08001035 switch (propertyConstant) {
1036 case TRANSLATION_X:
Chris Craik49e6c732014-03-31 12:34:11 -07001037 return node.getTranslationX();
Chet Haasea00f3862011-02-22 06:34:40 -08001038 case TRANSLATION_Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001039 return node.getTranslationY();
Chris Craikf57776b2013-10-25 18:30:17 -07001040 case TRANSLATION_Z:
Chris Craik49e6c732014-03-31 12:34:11 -07001041 return node.getTranslationZ();
Chet Haasea00f3862011-02-22 06:34:40 -08001042 case ROTATION:
Chris Craik49e6c732014-03-31 12:34:11 -07001043 return node.getRotation();
Chet Haasea00f3862011-02-22 06:34:40 -08001044 case ROTATION_X:
Chris Craik49e6c732014-03-31 12:34:11 -07001045 return node.getRotationX();
Chet Haasea00f3862011-02-22 06:34:40 -08001046 case ROTATION_Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001047 return node.getRotationY();
Chet Haasea00f3862011-02-22 06:34:40 -08001048 case SCALE_X:
Chris Craik49e6c732014-03-31 12:34:11 -07001049 return node.getScaleX();
Chet Haasea00f3862011-02-22 06:34:40 -08001050 case SCALE_Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001051 return node.getScaleY();
Chet Haasea00f3862011-02-22 06:34:40 -08001052 case X:
Chris Craik49e6c732014-03-31 12:34:11 -07001053 return mView.mLeft + node.getTranslationX();
Chet Haasea00f3862011-02-22 06:34:40 -08001054 case Y:
Chris Craik49e6c732014-03-31 12:34:11 -07001055 return mView.mTop + node.getTranslationY();
Chris Craikcc39e162014-04-25 18:34:11 -07001056 case Z:
1057 return node.getElevation() + node.getTranslationZ();
Chet Haasea00f3862011-02-22 06:34:40 -08001058 case ALPHA:
Chris Craik49e6c732014-03-31 12:34:11 -07001059 return mView.mTransformationInfo.mAlpha;
Chet Haasea00f3862011-02-22 06:34:40 -08001060 }
1061 return 0;
1062 }
1063
1064 /**
1065 * Utility class that handles the various Animator events. The only ones we care
1066 * about are the end event (which we use to clean up the animator map when an animator
1067 * finishes) and the update event (which we use to calculate the current value of each
1068 * property and then set it on the view object).
1069 */
1070 private class AnimatorEventListener
1071 implements Animator.AnimatorListener, ValueAnimator.AnimatorUpdateListener {
1072 @Override
1073 public void onAnimationStart(Animator animation) {
Chet Haasec1ca6652012-01-31 07:33:48 -08001074 if (mAnimatorSetupMap != null) {
1075 Runnable r = mAnimatorSetupMap.get(animation);
1076 if (r != null) {
1077 r.run();
1078 }
1079 mAnimatorSetupMap.remove(animation);
1080 }
1081 if (mAnimatorOnStartMap != null) {
1082 Runnable r = mAnimatorOnStartMap.get(animation);
1083 if (r != null) {
1084 r.run();
1085 }
1086 mAnimatorOnStartMap.remove(animation);
1087 }
Chet Haasea00f3862011-02-22 06:34:40 -08001088 if (mListener != null) {
1089 mListener.onAnimationStart(animation);
1090 }
1091 }
1092
1093 @Override
1094 public void onAnimationCancel(Animator animation) {
1095 if (mListener != null) {
1096 mListener.onAnimationCancel(animation);
1097 }
Chet Haasec1ca6652012-01-31 07:33:48 -08001098 if (mAnimatorOnEndMap != null) {
1099 mAnimatorOnEndMap.remove(animation);
1100 }
Chet Haasea00f3862011-02-22 06:34:40 -08001101 }
1102
1103 @Override
1104 public void onAnimationRepeat(Animator animation) {
1105 if (mListener != null) {
1106 mListener.onAnimationRepeat(animation);
1107 }
1108 }
1109
1110 @Override
1111 public void onAnimationEnd(Animator animation) {
Chet Haase563d4f22012-04-18 16:20:08 -07001112 mView.setHasTransientState(false);
Chet Haasea00f3862011-02-22 06:34:40 -08001113 if (mListener != null) {
1114 mListener.onAnimationEnd(animation);
1115 }
Chet Haasec1ca6652012-01-31 07:33:48 -08001116 if (mAnimatorOnEndMap != null) {
1117 Runnable r = mAnimatorOnEndMap.get(animation);
1118 if (r != null) {
1119 r.run();
1120 }
1121 mAnimatorOnEndMap.remove(animation);
1122 }
1123 if (mAnimatorCleanupMap != null) {
1124 Runnable r = mAnimatorCleanupMap.get(animation);
1125 if (r != null) {
1126 r.run();
1127 }
1128 mAnimatorCleanupMap.remove(animation);
1129 }
Chet Haasea00f3862011-02-22 06:34:40 -08001130 mAnimatorMap.remove(animation);
1131 }
1132
1133 /**
1134 * Calculate the current value for each property and set it on the view. Invalidate
1135 * the view object appropriately, depending on which properties are being animated.
1136 *
1137 * @param animation The animator associated with the properties that need to be
1138 * set. This animator holds the animation fraction which we will use to calculate
1139 * the current value of each property.
1140 */
1141 @Override
1142 public void onAnimationUpdate(ValueAnimator animation) {
Chet Haase0c6d83cc2011-12-01 08:38:13 -08001143 PropertyBundle propertyBundle = mAnimatorMap.get(animation);
1144 if (propertyBundle == null) {
1145 // Shouldn't happen, but just to play it safe
1146 return;
1147 }
Chris Craik30485452014-05-27 19:18:10 -07001148
1149 boolean hardwareAccelerated = mView.isHardwareAccelerated();
Chet Haase9d1992d2012-03-13 11:03:25 -07001150
Chet Haasea00f3862011-02-22 06:34:40 -08001151 // alpha requires slightly different treatment than the other (transform) properties.
1152 // The logic in setAlpha() is not simply setting mAlpha, plus the invalidation
1153 // logic is dependent on how the view handles an internal call to onSetAlpha().
1154 // We track what kinds of properties are set, and how alpha is handled when it is
1155 // set, and perform the invalidation steps appropriately.
1156 boolean alphaHandled = false;
Chris Craik30485452014-05-27 19:18:10 -07001157 if (!hardwareAccelerated) {
Chet Haase9d1992d2012-03-13 11:03:25 -07001158 mView.invalidateParentCaches();
1159 }
Chet Haasea00f3862011-02-22 06:34:40 -08001160 float fraction = animation.getAnimatedFraction();
Chet Haasea00f3862011-02-22 06:34:40 -08001161 int propertyMask = propertyBundle.mPropertyMask;
1162 if ((propertyMask & TRANSFORM_MASK) != 0) {
Chris Craik30485452014-05-27 19:18:10 -07001163 mView.invalidateViewProperty(hardwareAccelerated, false);
Chet Haasea00f3862011-02-22 06:34:40 -08001164 }
1165 ArrayList<NameValuesHolder> valueList = propertyBundle.mNameValuesHolder;
1166 if (valueList != null) {
1167 int count = valueList.size();
1168 for (int i = 0; i < count; ++i) {
1169 NameValuesHolder values = valueList.get(i);
1170 float value = values.mFromValue + fraction * values.mDeltaValue;
1171 if (values.mNameConstant == ALPHA) {
1172 alphaHandled = mView.setAlphaNoInvalidation(value);
1173 } else {
1174 setValue(values.mNameConstant, value);
1175 }
1176 }
1177 }
1178 if ((propertyMask & TRANSFORM_MASK) != 0) {
Chris Craik30485452014-05-27 19:18:10 -07001179 if (!hardwareAccelerated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001180 mView.mPrivateFlags |= View.PFLAG_DRAWN; // force another invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07001181 }
Chet Haasea00f3862011-02-22 06:34:40 -08001182 }
1183 // invalidate(false) in all cases except if alphaHandled gets set to true
1184 // via the call to setAlphaNoInvalidation(), above
Chet Haase9d1992d2012-03-13 11:03:25 -07001185 if (alphaHandled) {
1186 mView.invalidate(true);
1187 } else {
1188 mView.invalidateViewProperty(false, false);
1189 }
Chet Haase87f4ae62013-09-06 18:29:56 -07001190 if (mUpdateListener != null) {
1191 mUpdateListener.onAnimationUpdate(animation);
1192 }
Chet Haasea00f3862011-02-22 06:34:40 -08001193 }
1194 }
1195}