AnimatorSet.java revision e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.animation; 18 19import java.util.ArrayList; 20import java.util.HashMap; 21 22/** 23 * This class plays a set of {@link Animator} objects in the specified order. Animations 24 * can be set up to play together, in sequence, or after a specified delay. 25 * 26 * <p>There are two different approaches to adding animations to a <code>AnimatorSet</code>: 27 * either the {@link AnimatorSet#playTogether(Animator[]) playTogether()} or 28 * {@link AnimatorSet#playSequentially(Animator[]) playSequentially()} methods can be called to add 29 * a set of animations all at once, or the {@link AnimatorSet#play(Animator)} can be 30 * used in conjunction with methods in the {@link AnimatorSet.Builder Builder} 31 * class to add animations 32 * one by one.</p> 33 * 34 * <p>It is possible to set up a <code>AnimatorSet</code> with circular dependencies between 35 * its animations. For example, an animation a1 could be set up to start before animation a2, a2 36 * before a3, and a3 before a1. The results of this configuration are undefined, but will typically 37 * result in none of the affected animations being played. Because of this (and because 38 * circular dependencies do not make logical sense anyway), circular dependencies 39 * should be avoided, and the dependency flow of animations should only be in one direction. 40 */ 41public final class AnimatorSet extends Animator { 42 43 /** 44 * Internal variables 45 * NOTE: This object implements the clone() method, making a deep copy of any referenced 46 * objects. As other non-trivial fields are added to this class, make sure to add logic 47 * to clone() to make deep copies of them. 48 */ 49 50 /** 51 * Tracks animations currently being played, so that we know what to 52 * cancel or end when cancel() or end() is called on this AnimatorSet 53 */ 54 private ArrayList<Animator> mPlayingSet = new ArrayList<Animator>(); 55 56 /** 57 * Contains all nodes, mapped to their respective Animators. When new 58 * dependency information is added for an Animator, we want to add it 59 * to a single node representing that Animator, not create a new Node 60 * if one already exists. 61 */ 62 private HashMap<Animator, Node> mNodeMap = new HashMap<Animator, Node>(); 63 64 /** 65 * Set of all nodes created for this AnimatorSet. This list is used upon 66 * starting the set, and the nodes are placed in sorted order into the 67 * sortedNodes collection. 68 */ 69 private ArrayList<Node> mNodes = new ArrayList<Node>(); 70 71 /** 72 * The sorted list of nodes. This is the order in which the animations will 73 * be played. The details about when exactly they will be played depend 74 * on the dependency relationships of the nodes. 75 */ 76 private ArrayList<Node> mSortedNodes = new ArrayList<Node>(); 77 78 /** 79 * Flag indicating whether the nodes should be sorted prior to playing. This 80 * flag allows us to cache the previous sorted nodes so that if the sequence 81 * is replayed with no changes, it does not have to re-sort the nodes again. 82 */ 83 private boolean mNeedsSort = true; 84 85 private AnimatorSetListener mSetListener = null; 86 87 /** 88 * Flag indicating that the AnimatorSet has been canceled (by calling cancel() or end()). 89 * This flag is used to avoid starting other animations when currently-playing 90 * child animations of this AnimatorSet end. 91 */ 92 boolean mCanceled = false; 93 94 // The amount of time in ms to delay starting the animation after start() is called 95 private long mStartDelay = 0; 96 97 98 // How long the child animations should last in ms. The default value is negative, which 99 // simply means that there is no duration set on the AnimatorSet. When a real duration is 100 // set, it is passed along to the child animations. 101 private long mDuration = -1; 102 103 104 /** 105 * Sets up this AnimatorSet to play all of the supplied animations at the same time. 106 * 107 * @param items The animations that will be started simultaneously. 108 */ 109 public void playTogether(Animator... items) { 110 if (items != null) { 111 mNeedsSort = true; 112 Builder builder = play(items[0]); 113 for (int i = 1; i < items.length; ++i) { 114 builder.with(items[i]); 115 } 116 } 117 } 118 119 /** 120 * Sets up this AnimatorSet to play each of the supplied animations when the 121 * previous animation ends. 122 * 123 * @param items The aniamtions that will be started one after another. 124 */ 125 public void playSequentially(Animator... items) { 126 if (items != null) { 127 mNeedsSort = true; 128 if (items.length == 1) { 129 play(items[0]); 130 } else { 131 for (int i = 0; i < items.length - 1; ++i) { 132 play(items[i]).before(items[i+1]); 133 } 134 } 135 } 136 } 137 138 /** 139 * Returns the current list of child Animator objects controlled by this 140 * AnimatorSet. This is a copy of the internal list; modifications to the returned list 141 * will not affect the AnimatorSet, although changes to the underlying Animator objects 142 * will affect those objects being managed by the AnimatorSet. 143 * 144 * @return ArrayList<Animator> The list of child animations of this AnimatorSet. 145 */ 146 public ArrayList<Animator> getChildAnimations() { 147 ArrayList<Animator> childList = new ArrayList<Animator>(); 148 for (Node node : mNodes) { 149 childList.add(node.animation); 150 } 151 return childList; 152 } 153 154 /** 155 * Sets the target object for all current {@link #getChildAnimations() child animations} 156 * of this AnimatorSet that take targets ({@link ObjectAnimator} and 157 * AnimatorSet). 158 * 159 * @param target The object being animated 160 */ 161 @Override 162 public void setTarget(Object target) { 163 for (Node node : mNodes) { 164 Animator animation = node.animation; 165 if (animation instanceof AnimatorSet) { 166 ((AnimatorSet)animation).setTarget(target); 167 } else if (animation instanceof ObjectAnimator) { 168 ((ObjectAnimator)animation).setTarget(target); 169 } 170 } 171 } 172 173 /** 174 * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations} 175 * of this AnimatorSet. 176 * 177 * @param interpolator the interpolator to be used by each child animation of this AnimatorSet 178 */ 179 @Override 180 public void setInterpolator(TimeInterpolator interpolator) { 181 for (Node node : mNodes) { 182 node.animation.setInterpolator(interpolator); 183 } 184 } 185 186 /** 187 * This method creates a <code>Builder</code> object, which is used to 188 * set up playing constraints. This initial <code>play()</code> method 189 * tells the <code>Builder</code> the animation that is the dependency for 190 * the succeeding commands to the <code>Builder</code>. For example, 191 * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play 192 * <code>a1</code> and <code>a2</code> at the same time, 193 * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play 194 * <code>a1</code> first, followed by <code>a2</code>, and 195 * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play 196 * <code>a2</code> first, followed by <code>a1</code>. 197 * 198 * <p>Note that <code>play()</code> is the only way to tell the 199 * <code>Builder</code> the animation upon which the dependency is created, 200 * so successive calls to the various functions in <code>Builder</code> 201 * will all refer to the initial parameter supplied in <code>play()</code> 202 * as the dependency of the other animations. For example, calling 203 * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code> 204 * and <code>a3</code> when a1 ends; it does not set up a dependency between 205 * <code>a2</code> and <code>a3</code>.</p> 206 * 207 * @param anim The animation that is the dependency used in later calls to the 208 * methods in the returned <code>Builder</code> object. A null parameter will result 209 * in a null <code>Builder</code> return value. 210 * @return Builder The object that constructs the AnimatorSet based on the dependencies 211 * outlined in the calls to <code>play</code> and the other methods in the 212 * <code>Builder</code object. 213 */ 214 public Builder play(Animator anim) { 215 if (anim != null) { 216 mNeedsSort = true; 217 return new Builder(anim); 218 } 219 return null; 220 } 221 222 /** 223 * {@inheritDoc} 224 * 225 * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it is 226 * responsible for.</p> 227 */ 228 @SuppressWarnings("unchecked") 229 @Override 230 public void cancel() { 231 mCanceled = true; 232 if (mListeners != null) { 233 ArrayList<AnimatorListener> tmpListeners = 234 (ArrayList<AnimatorListener>) mListeners.clone(); 235 for (AnimatorListener listener : tmpListeners) { 236 listener.onAnimationCancel(this); 237 } 238 } 239 if (mSortedNodes.size() > 0) { 240 for (Node node : mSortedNodes) { 241 node.animation.cancel(); 242 } 243 } 244 } 245 246 /** 247 * {@inheritDoc} 248 * 249 * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is 250 * responsible for.</p> 251 */ 252 @Override 253 public void end() { 254 mCanceled = true; 255 if (mSortedNodes.size() != mNodes.size()) { 256 // hasn't been started yet - sort the nodes now, then end them 257 sortNodes(); 258 for (Node node : mSortedNodes) { 259 if (mSetListener == null) { 260 mSetListener = new AnimatorSetListener(this); 261 } 262 node.animation.addListener(mSetListener); 263 } 264 } 265 if (mSortedNodes.size() > 0) { 266 for (Node node : mSortedNodes) { 267 node.animation.end(); 268 } 269 } 270 } 271 272 /** 273 * Returns true if any of the child animations of this AnimatorSet have been started and have not 274 * yet ended. 275 * @return Whether this AnimatorSet has been started and has not yet ended. 276 */ 277 @Override 278 public boolean isRunning() { 279 for (Node node : mNodes) { 280 if (node.animation.isRunning()) { 281 return true; 282 } 283 } 284 return false; 285 } 286 287 /** 288 * The amount of time, in milliseconds, to delay starting the animation after 289 * {@link #start()} is called. 290 * 291 * @return the number of milliseconds to delay running the animation 292 */ 293 @Override 294 public long getStartDelay() { 295 return mStartDelay; 296 } 297 298 /** 299 * The amount of time, in milliseconds, to delay starting the animation after 300 * {@link #start()} is called. 301 302 * @param startDelay The amount of the delay, in milliseconds 303 */ 304 @Override 305 public void setStartDelay(long startDelay) { 306 mStartDelay = startDelay; 307 } 308 309 /** 310 * Gets the length of each of the child animations of this AnimatorSet. This value may 311 * be less than 0, which indicates that no duration has been set on this AnimatorSet 312 * and each of the child animations will use their own duration. 313 * 314 * @return The length of the animation, in milliseconds, of each of the child 315 * animations of this AnimatorSet. 316 */ 317 @Override 318 public long getDuration() { 319 return mDuration; 320 } 321 322 /** 323 * Sets the length of each of the current child animations of this AnimatorSet. By default, 324 * each child animation will use its own duration. If the duration is set on the AnimatorSet, 325 * then each child animation inherits this duration. 326 * 327 * @param duration The length of the animation, in milliseconds, of each of the child 328 * animations of this AnimatorSet. 329 */ 330 @Override 331 public void setDuration(long duration) { 332 if (duration < 0) { 333 throw new IllegalArgumentException("duration must be a value of zero or greater"); 334 } 335 for (Node node : mNodes) { 336 // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to 337 // insert "play-after" delays 338 node.animation.setDuration(duration); 339 } 340 mDuration = duration; 341 } 342 343 /** 344 * {@inheritDoc} 345 * 346 * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which 347 * it is responsible. The details of when exactly those animations are started depends on 348 * the dependency relationships that have been set up between the animations. 349 */ 350 @SuppressWarnings("unchecked") 351 @Override 352 public void start() { 353 mCanceled = false; 354 355 // First, sort the nodes (if necessary). This will ensure that sortedNodes 356 // contains the animation nodes in the correct order. 357 sortNodes(); 358 359 // nodesToStart holds the list of nodes to be started immediately. We don't want to 360 // start the animations in the loop directly because we first need to set up 361 // dependencies on all of the nodes. For example, we don't want to start an animation 362 // when some other animation also wants to start when the first animation begins. 363 final ArrayList<Node> nodesToStart = new ArrayList<Node>(); 364 for (Node node : mSortedNodes) { 365 if (mSetListener == null) { 366 mSetListener = new AnimatorSetListener(this); 367 } 368 if (node.dependencies == null || node.dependencies.size() == 0) { 369 nodesToStart.add(node); 370 } else { 371 for (Dependency dependency : node.dependencies) { 372 dependency.node.animation.addListener( 373 new DependencyListener(this, node, dependency.rule)); 374 } 375 node.tmpDependencies = (ArrayList<Dependency>) node.dependencies.clone(); 376 } 377 node.animation.addListener(mSetListener); 378 } 379 // Now that all dependencies are set up, start the animations that should be started. 380 if (mStartDelay <= 0) { 381 for (Node node : nodesToStart) { 382 node.animation.start(); 383 mPlayingSet.add(node.animation); 384 } 385 } else { 386 // TODO: Need to cancel out of the delay appropriately 387 ValueAnimator delayAnim = new ValueAnimator(mStartDelay, 0f, 1f); 388 delayAnim.addListener(new AnimatorListenerAdapter() { 389 public void onAnimationEnd(Animator anim) { 390 for (Node node : nodesToStart) { 391 node.animation.start(); 392 mPlayingSet.add(node.animation); 393 } 394 } 395 }); 396 } 397 if (mListeners != null) { 398 ArrayList<AnimatorListener> tmpListeners = 399 (ArrayList<AnimatorListener>) mListeners.clone(); 400 for (AnimatorListener listener : tmpListeners) { 401 listener.onAnimationStart(this); 402 } 403 } 404 } 405 406 @Override 407 public AnimatorSet clone() { 408 final AnimatorSet anim = (AnimatorSet) super.clone(); 409 /* 410 * The basic clone() operation copies all items. This doesn't work very well for 411 * AnimatorSet, because it will copy references that need to be recreated and state 412 * that may not apply. What we need to do now is put the clone in an uninitialized 413 * state, with fresh, empty data structures. Then we will build up the nodes list 414 * manually, as we clone each Node (and its animation). The clone will then be sorted, 415 * and will populate any appropriate lists, when it is started. 416 */ 417 anim.mNeedsSort = true; 418 anim.mCanceled = false; 419 anim.mPlayingSet = new ArrayList<Animator>(); 420 anim.mNodeMap = new HashMap<Animator, Node>(); 421 anim.mNodes = new ArrayList<Node>(); 422 anim.mSortedNodes = new ArrayList<Node>(); 423 424 // Walk through the old nodes list, cloning each node and adding it to the new nodemap. 425 // One problem is that the old node dependencies point to nodes in the old AnimatorSet. 426 // We need to track the old/new nodes in order to reconstruct the dependencies in the clone. 427 HashMap<Node, Node> nodeCloneMap = new HashMap<Node, Node>(); // <old, new> 428 for (Node node : mNodes) { 429 Node nodeClone = node.clone(); 430 nodeCloneMap.put(node, nodeClone); 431 anim.mNodes.add(nodeClone); 432 anim.mNodeMap.put(nodeClone.animation, nodeClone); 433 // Clear out the dependencies in the clone; we'll set these up manually later 434 nodeClone.dependencies = null; 435 nodeClone.tmpDependencies = null; 436 nodeClone.nodeDependents = null; 437 nodeClone.nodeDependencies = null; 438 // clear out any listeners that were set up by the AnimatorSet; these will 439 // be set up when the clone's nodes are sorted 440 ArrayList<AnimatorListener> cloneListeners = nodeClone.animation.getListeners(); 441 if (cloneListeners != null) { 442 ArrayList<AnimatorListener> listenersToRemove = null; 443 for (AnimatorListener listener : cloneListeners) { 444 if (listener instanceof AnimatorSetListener) { 445 if (listenersToRemove == null) { 446 listenersToRemove = new ArrayList<AnimatorListener>(); 447 } 448 listenersToRemove.add(listener); 449 } 450 } 451 if (listenersToRemove != null) { 452 for (AnimatorListener listener : listenersToRemove) { 453 cloneListeners.remove(listener); 454 } 455 } 456 } 457 } 458 // Now that we've cloned all of the nodes, we're ready to walk through their 459 // dependencies, mapping the old dependencies to the new nodes 460 for (Node node : mNodes) { 461 Node nodeClone = nodeCloneMap.get(node); 462 if (node.dependencies != null) { 463 for (Dependency dependency : node.dependencies) { 464 Node clonedDependencyNode = nodeCloneMap.get(dependency.node); 465 Dependency cloneDependency = new Dependency(clonedDependencyNode, 466 dependency.rule); 467 nodeClone.addDependency(cloneDependency); 468 } 469 } 470 } 471 472 return anim; 473 } 474 475 /** 476 * This class is the mechanism by which animations are started based on events in other 477 * animations. If an animation has multiple dependencies on other animations, then 478 * all dependencies must be satisfied before the animation is started. 479 */ 480 private static class DependencyListener implements AnimatorListener { 481 482 private AnimatorSet mAnimatorSet; 483 484 // The node upon which the dependency is based. 485 private Node mNode; 486 487 // The Dependency rule (WITH or AFTER) that the listener should wait for on 488 // the node 489 private int mRule; 490 491 public DependencyListener(AnimatorSet animatorSet, Node node, int rule) { 492 this.mAnimatorSet = animatorSet; 493 this.mNode = node; 494 this.mRule = rule; 495 } 496 497 /** 498 * Ignore cancel events for now. We may want to handle this eventually, 499 * to prevent follow-on animations from running when some dependency 500 * animation is canceled. 501 */ 502 public void onAnimationCancel(Animator animation) { 503 } 504 505 /** 506 * An end event is received - see if this is an event we are listening for 507 */ 508 public void onAnimationEnd(Animator animation) { 509 if (mRule == Dependency.AFTER) { 510 startIfReady(animation); 511 } 512 } 513 514 /** 515 * Ignore repeat events for now 516 */ 517 public void onAnimationRepeat(Animator animation) { 518 } 519 520 /** 521 * A start event is received - see if this is an event we are listening for 522 */ 523 public void onAnimationStart(Animator animation) { 524 if (mRule == Dependency.WITH) { 525 startIfReady(animation); 526 } 527 } 528 529 /** 530 * Check whether the event received is one that the node was waiting for. 531 * If so, mark it as complete and see whether it's time to start 532 * the animation. 533 * @param dependencyAnimation the animation that sent the event. 534 */ 535 private void startIfReady(Animator dependencyAnimation) { 536 if (mAnimatorSet.mCanceled) { 537 // if the parent AnimatorSet was canceled, then don't start any dependent anims 538 return; 539 } 540 Dependency dependencyToRemove = null; 541 for (Dependency dependency : mNode.tmpDependencies) { 542 if (dependency.rule == mRule && 543 dependency.node.animation == dependencyAnimation) { 544 // rule fired - remove the dependency and listener and check to 545 // see whether it's time to start the animation 546 dependencyToRemove = dependency; 547 dependencyAnimation.removeListener(this); 548 break; 549 } 550 } 551 mNode.tmpDependencies.remove(dependencyToRemove); 552 if (mNode.tmpDependencies.size() == 0) { 553 // all dependencies satisfied: start the animation 554 mNode.animation.start(); 555 mAnimatorSet.mPlayingSet.add(mNode.animation); 556 } 557 } 558 559 } 560 561 private class AnimatorSetListener implements AnimatorListener { 562 563 private AnimatorSet mAnimatorSet; 564 565 AnimatorSetListener(AnimatorSet animatorSet) { 566 mAnimatorSet = animatorSet; 567 } 568 569 public void onAnimationCancel(Animator animation) { 570 if (mPlayingSet.size() == 0) { 571 if (mListeners != null) { 572 for (AnimatorListener listener : mListeners) { 573 listener.onAnimationCancel(mAnimatorSet); 574 } 575 } 576 } 577 } 578 579 @SuppressWarnings("unchecked") 580 public void onAnimationEnd(Animator animation) { 581 animation.removeListener(this); 582 mPlayingSet.remove(animation); 583 Node animNode = mAnimatorSet.mNodeMap.get(animation); 584 animNode.done = true; 585 ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes; 586 boolean allDone = true; 587 for (Node node : sortedNodes) { 588 if (!node.done) { 589 allDone = false; 590 break; 591 } 592 } 593 if (allDone) { 594 // If this was the last child animation to end, then notify listeners that this 595 // AnimatorSet has ended 596 if (mListeners != null) { 597 ArrayList<AnimatorListener> tmpListeners = 598 (ArrayList<AnimatorListener>) mListeners.clone(); 599 for (AnimatorListener listener : tmpListeners) { 600 listener.onAnimationEnd(mAnimatorSet); 601 } 602 } 603 } 604 } 605 606 // Nothing to do 607 public void onAnimationRepeat(Animator animation) { 608 } 609 610 // Nothing to do 611 public void onAnimationStart(Animator animation) { 612 } 613 614 } 615 616 /** 617 * This method sorts the current set of nodes, if needed. The sort is a simple 618 * DependencyGraph sort, which goes like this: 619 * - All nodes without dependencies become 'roots' 620 * - while roots list is not null 621 * - for each root r 622 * - add r to sorted list 623 * - remove r as a dependency from any other node 624 * - any nodes with no dependencies are added to the roots list 625 */ 626 private void sortNodes() { 627 if (mNeedsSort) { 628 mSortedNodes.clear(); 629 ArrayList<Node> roots = new ArrayList<Node>(); 630 for (Node node : mNodes) { 631 if (node.dependencies == null || node.dependencies.size() == 0) { 632 roots.add(node); 633 } 634 } 635 ArrayList<Node> tmpRoots = new ArrayList<Node>(); 636 while (roots.size() > 0) { 637 for (Node root : roots) { 638 mSortedNodes.add(root); 639 if (root.nodeDependents != null) { 640 for (Node node : root.nodeDependents) { 641 node.nodeDependencies.remove(root); 642 if (node.nodeDependencies.size() == 0) { 643 tmpRoots.add(node); 644 } 645 } 646 } 647 } 648 roots.clear(); 649 roots.addAll(tmpRoots); 650 tmpRoots.clear(); 651 } 652 mNeedsSort = false; 653 if (mSortedNodes.size() != mNodes.size()) { 654 throw new IllegalStateException("Circular dependencies cannot exist" 655 + " in AnimatorSet"); 656 } 657 } else { 658 // Doesn't need sorting, but still need to add in the nodeDependencies list 659 // because these get removed as the event listeners fire and the dependencies 660 // are satisfied 661 for (Node node : mNodes) { 662 if (node.dependencies != null && node.dependencies.size() > 0) { 663 for (Dependency dependency : node.dependencies) { 664 if (node.nodeDependencies == null) { 665 node.nodeDependencies = new ArrayList<Node>(); 666 } 667 if (!node.nodeDependencies.contains(dependency.node)) { 668 node.nodeDependencies.add(dependency.node); 669 } 670 } 671 } 672 node.done = false; 673 } 674 } 675 } 676 677 /** 678 * Dependency holds information about the node that some other node is 679 * dependent upon and the nature of that dependency. 680 * 681 */ 682 private static class Dependency { 683 static final int WITH = 0; // dependent node must start with this dependency node 684 static final int AFTER = 1; // dependent node must start when this dependency node finishes 685 686 // The node that the other node with this Dependency is dependent upon 687 public Node node; 688 689 // The nature of the dependency (WITH or AFTER) 690 public int rule; 691 692 public Dependency(Node node, int rule) { 693 this.node = node; 694 this.rule = rule; 695 } 696 } 697 698 /** 699 * A Node is an embodiment of both the Animator that it wraps as well as 700 * any dependencies that are associated with that Animation. This includes 701 * both dependencies upon other nodes (in the dependencies list) as 702 * well as dependencies of other nodes upon this (in the nodeDependents list). 703 */ 704 private static class Node implements Cloneable { 705 public Animator animation; 706 707 /** 708 * These are the dependencies that this node's animation has on other 709 * nodes. For example, if this node's animation should begin with some 710 * other animation ends, then there will be an item in this node's 711 * dependencies list for that other animation's node. 712 */ 713 public ArrayList<Dependency> dependencies = null; 714 715 /** 716 * tmpDependencies is a runtime detail. We use the dependencies list for sorting. 717 * But we also use the list to keep track of when multiple dependencies are satisfied, 718 * but removing each dependency as it is satisfied. We do not want to remove 719 * the dependency itself from the list, because we need to retain that information 720 * if the AnimatorSet is launched in the future. So we create a copy of the dependency 721 * list when the AnimatorSet starts and use this tmpDependencies list to track the 722 * list of satisfied dependencies. 723 */ 724 public ArrayList<Dependency> tmpDependencies = null; 725 726 /** 727 * nodeDependencies is just a list of the nodes that this Node is dependent upon. 728 * This information is used in sortNodes(), to determine when a node is a root. 729 */ 730 public ArrayList<Node> nodeDependencies = null; 731 732 /** 733 * nodeDepdendents is the list of nodes that have this node as a dependency. This 734 * is a utility field used in sortNodes to facilitate removing this node as a 735 * dependency when it is a root node. 736 */ 737 public ArrayList<Node> nodeDependents = null; 738 739 /** 740 * Flag indicating whether the animation in this node is finished. This flag 741 * is used by AnimatorSet to check, as each animation ends, whether all child animations 742 * are done and it's time to send out an end event for the entire AnimatorSet. 743 */ 744 public boolean done = false; 745 746 /** 747 * Constructs the Node with the animation that it encapsulates. A Node has no 748 * dependencies by default; dependencies are added via the addDependency() 749 * method. 750 * 751 * @param animation The animation that the Node encapsulates. 752 */ 753 public Node(Animator animation) { 754 this.animation = animation; 755 } 756 757 /** 758 * Add a dependency to this Node. The dependency includes information about the 759 * node that this node is dependency upon and the nature of the dependency. 760 * @param dependency 761 */ 762 public void addDependency(Dependency dependency) { 763 if (dependencies == null) { 764 dependencies = new ArrayList<Dependency>(); 765 nodeDependencies = new ArrayList<Node>(); 766 } 767 dependencies.add(dependency); 768 if (!nodeDependencies.contains(dependency.node)) { 769 nodeDependencies.add(dependency.node); 770 } 771 Node dependencyNode = dependency.node; 772 if (dependencyNode.nodeDependents == null) { 773 dependencyNode.nodeDependents = new ArrayList<Node>(); 774 } 775 dependencyNode.nodeDependents.add(this); 776 } 777 778 @Override 779 public Node clone() { 780 try { 781 Node node = (Node) super.clone(); 782 node.animation = (Animator) animation.clone(); 783 return node; 784 } catch (CloneNotSupportedException e) { 785 throw new AssertionError(); 786 } 787 } 788 } 789 790 /** 791 * The <code>Builder</code> object is a utility class to facilitate adding animations to a 792 * <code>AnimatorSet</code> along with the relationships between the various animations. The 793 * intention of the <code>Builder</code> methods, along with the {@link 794 * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible to 795 * express the dependency relationships of animations in a natural way. Developers can also use 796 * the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link 797 * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need, 798 * but it might be easier in some situations to express the AnimatorSet of animations in pairs. 799 * <p/> 800 * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed 801 * internally via a call to {@link AnimatorSet#play(Animator)}.</p> 802 * <p/> 803 * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to 804 * play when anim2 finishes, and anim4 to play when anim3 finishes:</p> 805 * <pre> 806 * AnimatorSet s = new AnimatorSet(); 807 * s.play(anim1).with(anim2); 808 * s.play(anim2).before(anim3); 809 * s.play(anim4).after(anim3); 810 * </pre> 811 * <p/> 812 * <p>Note in the example that both {@link Builder#before(Animator)} and {@link 813 * Builder#after(Animator)} are used. These are just different ways of expressing the same 814 * relationship and are provided to make it easier to say things in a way that is more natural, 815 * depending on the situation.</p> 816 * <p/> 817 * <p>It is possible to make several calls into the same <code>Builder</code> object to express 818 * multiple relationships. However, note that it is only the animation passed into the initial 819 * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive 820 * calls to the <code>Builder</code> object. For example, the following code starts both anim2 821 * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and 822 * anim3: 823 * <pre> 824 * AnimatorSet s = new AnimatorSet(); 825 * s.play(anim1).before(anim2).before(anim3); 826 * </pre> 827 * If the desired result is to play anim1 then anim2 then anim3, this code expresses the 828 * relationship correctly:</p> 829 * <pre> 830 * AnimatorSet s = new AnimatorSet(); 831 * s.play(anim1).before(anim2); 832 * s.play(anim2).before(anim3); 833 * </pre> 834 * <p/> 835 * <p>Note that it is possible to express relationships that cannot be resolved and will not 836 * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no 837 * sense. In general, circular dependencies like this one (or more indirect ones where a depends 838 * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets 839 * that can boil down to a simple, one-way relationship of animations starting with, before, and 840 * after other, different, animations.</p> 841 */ 842 public class Builder { 843 844 /** 845 * This tracks the current node being processed. It is supplied to the play() method 846 * of AnimatorSet and passed into the constructor of Builder. 847 */ 848 private Node mCurrentNode; 849 850 /** 851 * package-private constructor. Builders are only constructed by AnimatorSet, when the 852 * play() method is called. 853 * 854 * @param anim The animation that is the dependency for the other animations passed into 855 * the other methods of this Builder object. 856 */ 857 Builder(Animator anim) { 858 mCurrentNode = mNodeMap.get(anim); 859 if (mCurrentNode == null) { 860 mCurrentNode = new Node(anim); 861 mNodeMap.put(anim, mCurrentNode); 862 mNodes.add(mCurrentNode); 863 } 864 } 865 866 /** 867 * Sets up the given animation to play at the same time as the animation supplied in the 868 * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object. 869 * 870 * @param anim The animation that will play when the animation supplied to the 871 * {@link AnimatorSet#play(Animator)} method starts. 872 */ 873 public void with(Animator anim) { 874 Node node = mNodeMap.get(anim); 875 if (node == null) { 876 node = new Node(anim); 877 mNodeMap.put(anim, node); 878 mNodes.add(node); 879 } 880 Dependency dependency = new Dependency(mCurrentNode, Dependency.WITH); 881 node.addDependency(dependency); 882 } 883 884 /** 885 * Sets up the given animation to play when the animation supplied in the 886 * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 887 * ends. 888 * 889 * @param anim The animation that will play when the animation supplied to the 890 * {@link AnimatorSet#play(Animator)} method ends. 891 */ 892 public void before(Animator anim) { 893 Node node = mNodeMap.get(anim); 894 if (node == null) { 895 node = new Node(anim); 896 mNodeMap.put(anim, node); 897 mNodes.add(node); 898 } 899 Dependency dependency = new Dependency(mCurrentNode, Dependency.AFTER); 900 node.addDependency(dependency); 901 } 902 903 /** 904 * Sets up the given animation to play when the animation supplied in the 905 * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 906 * to start when the animation supplied in this method call ends. 907 * 908 * @param anim The animation whose end will cause the animation supplied to the 909 * {@link AnimatorSet#play(Animator)} method to play. 910 */ 911 public void after(Animator anim) { 912 Node node = mNodeMap.get(anim); 913 if (node == null) { 914 node = new Node(anim); 915 mNodeMap.put(anim, node); 916 mNodes.add(node); 917 } 918 Dependency dependency = new Dependency(node, Dependency.AFTER); 919 mCurrentNode.addDependency(dependency); 920 } 921 922 /** 923 * Sets up the animation supplied in the 924 * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 925 * to play when the given amount of time elapses. 926 * 927 * @param delay The number of milliseconds that should elapse before the 928 * animation starts. 929 */ 930 public void after(long delay) { 931 // setup dummy ValueAnimator just to run the clock 932 after(new ValueAnimator(delay, 0f, 1f)); 933 } 934 935 } 936 937} 938