AnimatorSet.java revision 2970c499388b4dcd1232cd622a9b80b395eeb2b4
117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/* 217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project 317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License. 617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at 717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * http://www.apache.org/licenses/LICENSE-2.0 917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software 1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS, 1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and 1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License. 1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation; 1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 1917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.ArrayList; 2017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.HashMap; 2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/** 23a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class plays a set of {@link Animator} objects in the specified order. Animations 2417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * can be set up to play together, in sequence, or after a specified delay. 2517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There are two different approaches to adding animations to a <code>AnimatorSet</code>: 27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * either the {@link AnimatorSet#playTogether(Animator[]) playTogether()} or 28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#playSequentially(Animator[]) playSequentially()} methods can be called to add 29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * a set of animations all at once, or the {@link AnimatorSet#play(Animator)} can be 30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * used in conjunction with methods in the {@link AnimatorSet.Builder Builder} 3117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * class to add animations 3217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * one by one.</p> 3317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>It is possible to set up a <code>AnimatorSet</code> with circular dependencies between 3517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * its animations. For example, an animation a1 could be set up to start before animation a2, a2 3617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * before a3, and a3 before a1. The results of this configuration are undefined, but will typically 3717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * result in none of the affected animations being played. Because of this (and because 3817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * circular dependencies do not make logical sense anyway), circular dependencies 3917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be avoided, and the dependency flow of animations should only be in one direction. 4017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepublic final class AnimatorSet extends Animator { 4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 4449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * Internal variables 4549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * NOTE: This object implements the clone() method, making a deep copy of any referenced 4649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * objects. As other non-trivial fields are added to this class, make sure to add logic 4749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * to clone() to make deep copies of them. 4849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase */ 4949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase 5049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase /** 513b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase * Tracks animations currently being played, so that we know what to 52a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * cancel or end when cancel() or end() is called on this AnimatorSet 5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private ArrayList<Animator> mPlayingSet = new ArrayList<Animator>(); 5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 57a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Contains all nodes, mapped to their respective Animators. When new 58a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * dependency information is added for an Animator, we want to add it 59a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to a single node representing that Animator, not create a new Node 6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * if one already exists. 6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 62a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private HashMap<Animator, Node> mNodeMap = new HashMap<Animator, Node>(); 6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Set of all nodes created for this AnimatorSet. This list is used upon 66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * starting the set, and the nodes are placed in sorted order into the 6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * sortedNodes collection. 6817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 6949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase private ArrayList<Node> mNodes = new ArrayList<Node>(); 7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 7217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The sorted list of nodes. This is the order in which the animations will 7317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * be played. The details about when exactly they will be played depend 7417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * on the dependency relationships of the nodes. 7517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 7649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase private ArrayList<Node> mSortedNodes = new ArrayList<Node>(); 7717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Flag indicating whether the nodes should be sorted prior to playing. This 8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * flag allows us to cache the previous sorted nodes so that if the sequence 8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is replayed with no changes, it does not have to re-sort the nodes again. 8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean mNeedsSort = true; 8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private AnimatorSetListener mSetListener = null; 8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 88a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag indicating that the AnimatorSet has been canceled (by calling cancel() or end()). 89010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * This flag is used to avoid starting other animations when currently-playing 90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * child animations of this AnimatorSet end. 91010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase */ 92010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase boolean mCanceled = false; 93010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase 9421cd1389d2ef218b20994b617c57af120841a57fChet Haase // The amount of time in ms to delay starting the animation after start() is called 9521cd1389d2ef218b20994b617c57af120841a57fChet Haase private long mStartDelay = 0; 9621cd1389d2ef218b20994b617c57af120841a57fChet Haase 9721cd1389d2ef218b20994b617c57af120841a57fChet Haase 9821cd1389d2ef218b20994b617c57af120841a57fChet Haase // How long the child animations should last in ms. The default value is negative, which 99a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // simply means that there is no duration set on the AnimatorSet. When a real duration is 10021cd1389d2ef218b20994b617c57af120841a57fChet Haase // set, it is passed along to the child animations. 10121cd1389d2ef218b20994b617c57af120841a57fChet Haase private long mDuration = -1; 10221cd1389d2ef218b20994b617c57af120841a57fChet Haase 10321cd1389d2ef218b20994b617c57af120841a57fChet Haase 104010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase /** 105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets up this AnimatorSet to play all of the supplied animations at the same time. 10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param items The animations that will be started simultaneously. 10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void playTogether(Animator... items) { 110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (items != null) { 11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNeedsSort = true; 112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase Builder builder = play(items[0]); 113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 1; i < items.length; ++i) { 114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase builder.with(items[i]); 11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets up this AnimatorSet to play each of the supplied animations when the 12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * previous animation ends. 12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param items The aniamtions that will be started one after another. 12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void playSequentially(Animator... items) { 126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (items != null) { 12717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNeedsSort = true; 128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (items.length == 1) { 129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase play(items[0]); 13017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < items.length - 1; ++i) { 132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase play(items[i]).before(items[i+1]); 13317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 13417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 13517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 13717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the current list of child Animator objects controlled by this 140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet. This is a copy of the internal list; modifications to the returned list 141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will not affect the AnimatorSet, although changes to the underlying Animator objects 142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will affect those objects being managed by the AnimatorSet. 143f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * 144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return ArrayList<Animator> The list of child animations of this AnimatorSet. 145f54a8d7c479485174941c38f151ea7083c658da3Chet Haase */ 146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ArrayList<Animator> getChildAnimations() { 147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<Animator> childList = new ArrayList<Animator>(); 148f54a8d7c479485174941c38f151ea7083c658da3Chet Haase for (Node node : mNodes) { 149f54a8d7c479485174941c38f151ea7083c658da3Chet Haase childList.add(node.animation); 150f54a8d7c479485174941c38f151ea7083c658da3Chet Haase } 151f54a8d7c479485174941c38f151ea7083c658da3Chet Haase return childList; 152f54a8d7c479485174941c38f151ea7083c658da3Chet Haase } 153f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 154f54a8d7c479485174941c38f151ea7083c658da3Chet Haase /** 155811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase * Sets the target object for all current {@link #getChildAnimations() child animations} 156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of this AnimatorSet that take targets ({@link ObjectAnimator} and 157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet). 158811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase * 159811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase * @param target The object being animated 160811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase */ 16121cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 162811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase public void setTarget(Object target) { 163811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase for (Node node : mNodes) { 164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase Animator animation = node.animation; 165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (animation instanceof AnimatorSet) { 166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ((AnimatorSet)animation).setTarget(target); 167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else if (animation instanceof ObjectAnimator) { 168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ((ObjectAnimator)animation).setTarget(target); 169811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase } 170811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase } 171811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase } 172811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase 173811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase /** 174e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations} 175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of this AnimatorSet. 17621cd1389d2ef218b20994b617c57af120841a57fChet Haase * 177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param interpolator the interpolator to be used by each child animation of this AnimatorSet 17821cd1389d2ef218b20994b617c57af120841a57fChet Haase */ 17921cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 180e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public void setInterpolator(TimeInterpolator interpolator) { 18121cd1389d2ef218b20994b617c57af120841a57fChet Haase for (Node node : mNodes) { 18221cd1389d2ef218b20994b617c57af120841a57fChet Haase node.animation.setInterpolator(interpolator); 18321cd1389d2ef218b20994b617c57af120841a57fChet Haase } 18421cd1389d2ef218b20994b617c57af120841a57fChet Haase } 18521cd1389d2ef218b20994b617c57af120841a57fChet Haase 18621cd1389d2ef218b20994b617c57af120841a57fChet Haase /** 18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This method creates a <code>Builder</code> object, which is used to 18817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * set up playing constraints. This initial <code>play()</code> method 18917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * tells the <code>Builder</code> the animation that is the dependency for 19017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the succeeding commands to the <code>Builder</code>. For example, 191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play 19217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>a1</code> and <code>a2</code> at the same time, 193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play 19417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>a1</code> first, followed by <code>a2</code>, and 195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play 19617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>a2</code> first, followed by <code>a1</code>. 19717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 19817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Note that <code>play()</code> is the only way to tell the 19917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>Builder</code> the animation upon which the dependency is created, 20017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * so successive calls to the various functions in <code>Builder</code> 20117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * will all refer to the initial parameter supplied in <code>play()</code> 20217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * as the dependency of the other animations. For example, calling 20317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code> 20417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * and <code>a3</code> when a1 ends; it does not set up a dependency between 20517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>a2</code> and <code>a3</code>.</p> 20617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 20717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param anim The animation that is the dependency used in later calls to the 20817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * methods in the returned <code>Builder</code> object. A null parameter will result 20917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * in a null <code>Builder</code> return value. 210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return Builder The object that constructs the AnimatorSet based on the dependencies 21117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * outlined in the calls to <code>play</code> and the other methods in the 21217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>Builder</code object. 21317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Builder play(Animator anim) { 21517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim != null) { 21617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNeedsSort = true; 21717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return new Builder(anim); 21817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 21917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return null; 22017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 22117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 22217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 22317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@inheritDoc} 22417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it is 22617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * responsible for.</p> 22717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @SuppressWarnings("unchecked") 22917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @Override 23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void cancel() { 231010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase mCanceled = true; 23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (AnimatorListener listener : tmpListeners) { 23617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationCancel(this); 23717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 23817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 239010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase if (mSortedNodes.size() > 0) { 240010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase for (Node node : mSortedNodes) { 241010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase node.animation.cancel(); 24217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 24317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 24417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 24517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 24617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 24717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@inheritDoc} 24817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is 25017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * responsible for.</p> 25117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 25217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @Override 25317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void end() { 254010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase mCanceled = true; 255f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase if (mSortedNodes.size() != mNodes.size()) { 256f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase // hasn't been started yet - sort the nodes now, then end them 257f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase sortNodes(); 2581e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase for (Node node : mSortedNodes) { 259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mSetListener == null) { 260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mSetListener = new AnimatorSetListener(this); 2611e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase } 262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase node.animation.addListener(mSetListener); 2631e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase } 264f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase } 265010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase if (mSortedNodes.size() > 0) { 266010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase for (Node node : mSortedNodes) { 267010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase node.animation.end(); 26817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 26917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 27117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns true if any of the child animations of this AnimatorSet have been started and have not 274673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase * yet ended. 275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return Whether this AnimatorSet has been started and has not yet ended. 276673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase */ 277673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase @Override 278673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase public boolean isRunning() { 279673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase for (Node node : mNodes) { 280673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase if (node.animation.isRunning()) { 281673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase return true; 282673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase } 283673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase } 284673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase return false; 285673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase } 286673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase 287673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase /** 28821cd1389d2ef218b20994b617c57af120841a57fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 28921cd1389d2ef218b20994b617c57af120841a57fChet Haase * {@link #start()} is called. 29021cd1389d2ef218b20994b617c57af120841a57fChet Haase * 29121cd1389d2ef218b20994b617c57af120841a57fChet Haase * @return the number of milliseconds to delay running the animation 29221cd1389d2ef218b20994b617c57af120841a57fChet Haase */ 29321cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 29421cd1389d2ef218b20994b617c57af120841a57fChet Haase public long getStartDelay() { 29521cd1389d2ef218b20994b617c57af120841a57fChet Haase return mStartDelay; 29621cd1389d2ef218b20994b617c57af120841a57fChet Haase } 29721cd1389d2ef218b20994b617c57af120841a57fChet Haase 29821cd1389d2ef218b20994b617c57af120841a57fChet Haase /** 29921cd1389d2ef218b20994b617c57af120841a57fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 30021cd1389d2ef218b20994b617c57af120841a57fChet Haase * {@link #start()} is called. 30121cd1389d2ef218b20994b617c57af120841a57fChet Haase 30221cd1389d2ef218b20994b617c57af120841a57fChet Haase * @param startDelay The amount of the delay, in milliseconds 30321cd1389d2ef218b20994b617c57af120841a57fChet Haase */ 30421cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 30521cd1389d2ef218b20994b617c57af120841a57fChet Haase public void setStartDelay(long startDelay) { 30621cd1389d2ef218b20994b617c57af120841a57fChet Haase mStartDelay = startDelay; 30721cd1389d2ef218b20994b617c57af120841a57fChet Haase } 30821cd1389d2ef218b20994b617c57af120841a57fChet Haase 30921cd1389d2ef218b20994b617c57af120841a57fChet Haase /** 310a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Gets the length of each of the child animations of this AnimatorSet. This value may 311a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be less than 0, which indicates that no duration has been set on this AnimatorSet 31221cd1389d2ef218b20994b617c57af120841a57fChet Haase * and each of the child animations will use their own duration. 31321cd1389d2ef218b20994b617c57af120841a57fChet Haase * 31421cd1389d2ef218b20994b617c57af120841a57fChet Haase * @return The length of the animation, in milliseconds, of each of the child 315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations of this AnimatorSet. 31621cd1389d2ef218b20994b617c57af120841a57fChet Haase */ 31721cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 31821cd1389d2ef218b20994b617c57af120841a57fChet Haase public long getDuration() { 31921cd1389d2ef218b20994b617c57af120841a57fChet Haase return mDuration; 32021cd1389d2ef218b20994b617c57af120841a57fChet Haase } 32121cd1389d2ef218b20994b617c57af120841a57fChet Haase 32221cd1389d2ef218b20994b617c57af120841a57fChet Haase /** 323a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the length of each of the current child animations of this AnimatorSet. By default, 324a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * each child animation will use its own duration. If the duration is set on the AnimatorSet, 32521cd1389d2ef218b20994b617c57af120841a57fChet Haase * then each child animation inherits this duration. 32621cd1389d2ef218b20994b617c57af120841a57fChet Haase * 32721cd1389d2ef218b20994b617c57af120841a57fChet Haase * @param duration The length of the animation, in milliseconds, of each of the child 328a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations of this AnimatorSet. 32921cd1389d2ef218b20994b617c57af120841a57fChet Haase */ 33021cd1389d2ef218b20994b617c57af120841a57fChet Haase @Override 3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public AnimatorSet setDuration(long duration) { 33221cd1389d2ef218b20994b617c57af120841a57fChet Haase if (duration < 0) { 33321cd1389d2ef218b20994b617c57af120841a57fChet Haase throw new IllegalArgumentException("duration must be a value of zero or greater"); 33421cd1389d2ef218b20994b617c57af120841a57fChet Haase } 33521cd1389d2ef218b20994b617c57af120841a57fChet Haase for (Node node : mNodes) { 336a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to 33721cd1389d2ef218b20994b617c57af120841a57fChet Haase // insert "play-after" delays 33821cd1389d2ef218b20994b617c57af120841a57fChet Haase node.animation.setDuration(duration); 33921cd1389d2ef218b20994b617c57af120841a57fChet Haase } 34021cd1389d2ef218b20994b617c57af120841a57fChet Haase mDuration = duration; 3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return this; 34221cd1389d2ef218b20994b617c57af120841a57fChet Haase } 34321cd1389d2ef218b20994b617c57af120841a57fChet Haase 3442970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase @Override 3452970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public void setupStartValues() { 3462970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase for (Node node : mNodes) { 3472970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase node.animation.setupStartValues(); 3482970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 3492970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 3502970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase 3512970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase @Override 3522970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public void setupEndValues() { 3532970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase for (Node node : mNodes) { 3542970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase node.animation.setupEndValues(); 3552970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 3562970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 3572970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase 35821cd1389d2ef218b20994b617c57af120841a57fChet Haase /** 35917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@inheritDoc} 36017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 361a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which 36217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * it is responsible. The details of when exactly those animations are started depends on 36317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the dependency relationships that have been set up between the animations. 36417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 36517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @SuppressWarnings("unchecked") 36617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @Override 36717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void start() { 368010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase mCanceled = false; 369010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase 37017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // First, sort the nodes (if necessary). This will ensure that sortedNodes 37117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // contains the animation nodes in the correct order. 37217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sortNodes(); 37317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 37417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // nodesToStart holds the list of nodes to be started immediately. We don't want to 37517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // start the animations in the loop directly because we first need to set up 37617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // dependencies on all of the nodes. For example, we don't want to start an animation 37717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // when some other animation also wants to start when the first animation begins. 37821cd1389d2ef218b20994b617c57af120841a57fChet Haase final ArrayList<Node> nodesToStart = new ArrayList<Node>(); 3797c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numSortedNodes = mSortedNodes.size(); 3807c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numSortedNodes; ++i) { 3817c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node node = mSortedNodes.get(i); 382a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mSetListener == null) { 383a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mSetListener = new AnimatorSetListener(this); 38417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 38517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node.dependencies == null || node.dependencies.size() == 0) { 38617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase nodesToStart.add(node); 38717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 3887c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numDependencies = node.dependencies.size(); 3897c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int j = 0; j < numDependencies; ++j) { 3907c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Dependency dependency = node.dependencies.get(j); 39117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependency.node.animation.addListener( 392010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase new DependencyListener(this, node, dependency.rule)); 39317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 39417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.tmpDependencies = (ArrayList<Dependency>) node.dependencies.clone(); 39517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 396a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase node.animation.addListener(mSetListener); 39717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 39817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Now that all dependencies are set up, start the animations that should be started. 39921cd1389d2ef218b20994b617c57af120841a57fChet Haase if (mStartDelay <= 0) { 40021cd1389d2ef218b20994b617c57af120841a57fChet Haase for (Node node : nodesToStart) { 40121cd1389d2ef218b20994b617c57af120841a57fChet Haase node.animation.start(); 40221cd1389d2ef218b20994b617c57af120841a57fChet Haase mPlayingSet.add(node.animation); 40321cd1389d2ef218b20994b617c57af120841a57fChet Haase } 40421cd1389d2ef218b20994b617c57af120841a57fChet Haase } else { 40521cd1389d2ef218b20994b617c57af120841a57fChet Haase // TODO: Need to cancel out of the delay appropriately 4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator delayAnim = ValueAnimator.ofFloat(0f, 1f); 4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase delayAnim.setDuration(mStartDelay); 408a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase delayAnim.addListener(new AnimatorListenerAdapter() { 409a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationEnd(Animator anim) { 4107c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numNodes = nodesToStart.size(); 4117c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numNodes; ++i) { 4127c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node node = nodesToStart.get(i); 41321cd1389d2ef218b20994b617c57af120841a57fChet Haase node.animation.start(); 41421cd1389d2ef218b20994b617c57af120841a57fChet Haase mPlayingSet.add(node.animation); 41521cd1389d2ef218b20994b617c57af120841a57fChet Haase } 41621cd1389d2ef218b20994b617c57af120841a57fChet Haase } 41721cd1389d2ef218b20994b617c57af120841a57fChet Haase }); 4182970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase delayAnim.start(); 41917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 42017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 421a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 422a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 4237c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 4247c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 4257c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationStart(this); 4262970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (mNodes.size() == 0) { 4272970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // Handle unusual case where empty AnimatorSet is started - should send out 4282970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // end event immediately since the event will not be sent out at all otherwise 4292970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase tmpListeners.get(i).onAnimationEnd(this); 4302970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 43549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase @Override 436a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public AnimatorSet clone() { 437a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase final AnimatorSet anim = (AnimatorSet) super.clone(); 43849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase /* 43949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * The basic clone() operation copies all items. This doesn't work very well for 440a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet, because it will copy references that need to be recreated and state 44149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * that may not apply. What we need to do now is put the clone in an uninitialized 44249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * state, with fresh, empty data structures. Then we will build up the nodes list 44349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * manually, as we clone each Node (and its animation). The clone will then be sorted, 44449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase * and will populate any appropriate lists, when it is started. 44549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase */ 44649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mNeedsSort = true; 44749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mCanceled = false; 448a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mPlayingSet = new ArrayList<Animator>(); 449a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mNodeMap = new HashMap<Animator, Node>(); 45049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mNodes = new ArrayList<Node>(); 45149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mSortedNodes = new ArrayList<Node>(); 45249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase 45349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // Walk through the old nodes list, cloning each node and adding it to the new nodemap. 454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // One problem is that the old node dependencies point to nodes in the old AnimatorSet. 45549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // We need to track the old/new nodes in order to reconstruct the dependencies in the clone. 45649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase HashMap<Node, Node> nodeCloneMap = new HashMap<Node, Node>(); // <old, new> 45749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase for (Node node : mNodes) { 45849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase Node nodeClone = node.clone(); 45949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeCloneMap.put(node, nodeClone); 46049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mNodes.add(nodeClone); 46149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase anim.mNodeMap.put(nodeClone.animation, nodeClone); 46249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // Clear out the dependencies in the clone; we'll set these up manually later 46349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeClone.dependencies = null; 46449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeClone.tmpDependencies = null; 46549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeClone.nodeDependents = null; 46649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeClone.nodeDependencies = null; 467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // clear out any listeners that were set up by the AnimatorSet; these will 46849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // be set up when the clone's nodes are sorted 469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> cloneListeners = nodeClone.animation.getListeners(); 47049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase if (cloneListeners != null) { 471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> listenersToRemove = null; 472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (AnimatorListener listener : cloneListeners) { 473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (listener instanceof AnimatorSetListener) { 47449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase if (listenersToRemove == null) { 475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase listenersToRemove = new ArrayList<AnimatorListener>(); 47649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 47749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase listenersToRemove.add(listener); 47849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 47949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 48049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase if (listenersToRemove != null) { 481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (AnimatorListener listener : listenersToRemove) { 48249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase cloneListeners.remove(listener); 48349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 48449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 48549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 48649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 48749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // Now that we've cloned all of the nodes, we're ready to walk through their 48849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase // dependencies, mapping the old dependencies to the new nodes 48949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase for (Node node : mNodes) { 49049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase Node nodeClone = nodeCloneMap.get(node); 49149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase if (node.dependencies != null) { 49249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase for (Dependency dependency : node.dependencies) { 49349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase Node clonedDependencyNode = nodeCloneMap.get(dependency.node); 49449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase Dependency cloneDependency = new Dependency(clonedDependencyNode, 49549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase dependency.rule); 49649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase nodeClone.addDependency(cloneDependency); 49749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 49849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 49949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 50049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase 50149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase return anim; 50249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 50349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase 50417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 50517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This class is the mechanism by which animations are started based on events in other 50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animations. If an animation has multiple dependencies on other animations, then 50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * all dependencies must be satisfied before the animation is started. 50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private static class DependencyListener implements AnimatorListener { 51017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private AnimatorSet mAnimatorSet; 512010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase 51317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The node upon which the dependency is based. 51417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Node mNode; 51517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 51617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The Dependency rule (WITH or AFTER) that the listener should wait for on 51717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // the node 51817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mRule; 51917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public DependencyListener(AnimatorSet animatorSet, Node node, int rule) { 521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase this.mAnimatorSet = animatorSet; 52217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.mNode = node; 52317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.mRule = rule; 52417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 52617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 527010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * Ignore cancel events for now. We may want to handle this eventually, 528010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * to prevent follow-on animations from running when some dependency 529010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * animation is canceled. 53017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationCancel(Animator animation) { 53217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 53317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 53417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 53517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * An end event is received - see if this is an event we are listening for 53617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationEnd(Animator animation) { 53817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRule == Dependency.AFTER) { 53917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase startIfReady(animation); 54017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 54117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 54217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 54317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Ignore repeat events for now 54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationRepeat(Animator animation) { 54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 54917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * A start event is received - see if this is an event we are listening for 55117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 552a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationStart(Animator animation) { 55317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRule == Dependency.WITH) { 55417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase startIfReady(animation); 55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 55717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 55817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 55917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Check whether the event received is one that the node was waiting for. 56017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * If so, mark it as complete and see whether it's time to start 56117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the animation. 56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param dependencyAnimation the animation that sent the event. 56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 564a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private void startIfReady(Animator dependencyAnimation) { 565a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mAnimatorSet.mCanceled) { 566a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // if the parent AnimatorSet was canceled, then don't start any dependent anims 567010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase return; 568010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase } 56917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Dependency dependencyToRemove = null; 5707c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numDependencies = mNode.tmpDependencies.size(); 5717c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numDependencies; ++i) { 5727c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Dependency dependency = mNode.tmpDependencies.get(i); 57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (dependency.rule == mRule && 57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependency.node.animation == dependencyAnimation) { 57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // rule fired - remove the dependency and listener and check to 57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // see whether it's time to start the animation 57717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencyToRemove = dependency; 57817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencyAnimation.removeListener(this); 57917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 58017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNode.tmpDependencies.remove(dependencyToRemove); 58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mNode.tmpDependencies.size() == 0) { 58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // all dependencies satisfied: start the animation 58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNode.animation.start(); 586a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mAnimatorSet.mPlayingSet.add(mNode.animation); 58717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 59017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 59117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 592a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private class AnimatorSetListener implements AnimatorListener { 59317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 594a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private AnimatorSet mAnimatorSet; 59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 596a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase AnimatorSetListener(AnimatorSet animatorSet) { 597a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mAnimatorSet = animatorSet; 59817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 59917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 600a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationCancel(Animator animation) { 60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mPlayingSet.size() == 0) { 60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 6037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = mListeners.size(); 6047c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 6057c608f25d494c8a0a671e7373efbb47ca635367eChet Haase mListeners.get(i).onAnimationCancel(mAnimatorSet); 60617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @SuppressWarnings("unchecked") 612a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationEnd(Animator animation) { 61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animation.removeListener(this); 61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingSet.remove(animation); 615a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase Node animNode = mAnimatorSet.mNodeMap.get(animation); 6161e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase animNode.done = true; 617a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes; 618a6e4a1757728efc91627ece602b0899d75303659Chet Haase boolean allDone = true; 6197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numSortedNodes = sortedNodes.size(); 6207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numSortedNodes; ++i) { 6217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase if (!sortedNodes.get(i).done) { 622a6e4a1757728efc91627ece602b0899d75303659Chet Haase allDone = false; 623a6e4a1757728efc91627ece602b0899d75303659Chet Haase break; 624a6e4a1757728efc91627ece602b0899d75303659Chet Haase } 625a6e4a1757728efc91627ece602b0899d75303659Chet Haase } 626a6e4a1757728efc91627ece602b0899d75303659Chet Haase if (allDone) { 62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // If this was the last child animation to end, then notify listeners that this 628a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // AnimatorSet has ended 62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 630a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 631a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 6327c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 6337c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 6347c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationEnd(mAnimatorSet); 63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Nothing to do 641a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationRepeat(Animator animation) { 64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 64417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Nothing to do 645a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void onAnimationStart(Animator animation) { 64617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 64817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This method sorts the current set of nodes, if needed. The sort is a simple 65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * DependencyGraph sort, which goes like this: 65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - All nodes without dependencies become 'roots' 65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - while roots list is not null 65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - for each root r 65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - add r to sorted list 65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - remove r as a dependency from any other node 65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * - any nodes with no dependencies are added to the roots list 65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private void sortNodes() { 66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mNeedsSort) { 66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mSortedNodes.clear(); 66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<Node> roots = new ArrayList<Node>(); 6647c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numNodes = mNodes.size(); 6657c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numNodes; ++i) { 6667c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node node = mNodes.get(i); 66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node.dependencies == null || node.dependencies.size() == 0) { 66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase roots.add(node); 66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<Node> tmpRoots = new ArrayList<Node>(); 67217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase while (roots.size() > 0) { 6737c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numRoots = roots.size(); 6747c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numRoots; ++i) { 6757c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node root = roots.get(i); 67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mSortedNodes.add(root); 67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (root.nodeDependents != null) { 6787c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numDependents = root.nodeDependents.size(); 6797c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int j = 0; j < numDependents; ++j) { 6807c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node node = root.nodeDependents.get(j); 68117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.nodeDependencies.remove(root); 68217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node.nodeDependencies.size() == 0) { 68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase tmpRoots.add(node); 68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 688010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase roots.clear(); 68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase roots.addAll(tmpRoots); 69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase tmpRoots.clear(); 69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNeedsSort = false; 69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mSortedNodes.size() != mNodes.size()) { 69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase throw new IllegalStateException("Circular dependencies cannot exist" 695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase + " in AnimatorSet"); 69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Doesn't need sorting, but still need to add in the nodeDependencies list 69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // because these get removed as the event listeners fire and the dependencies 70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // are satisfied 7017c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numNodes = mNodes.size(); 7027c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numNodes; ++i) { 7037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Node node = mNodes.get(i); 70417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node.dependencies != null && node.dependencies.size() > 0) { 7057c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numDependencies = node.dependencies.size(); 7067c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int j = 0; j < numDependencies; ++j) { 7077c608f25d494c8a0a671e7373efbb47ca635367eChet Haase Dependency dependency = node.dependencies.get(j); 70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node.nodeDependencies == null) { 70917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.nodeDependencies = new ArrayList<Node>(); 71017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (!node.nodeDependencies.contains(dependency.node)) { 71217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.nodeDependencies.add(dependency.node); 71317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 7161e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase node.done = false; 71717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 72017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Dependency holds information about the node that some other node is 72317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * dependent upon and the nature of that dependency. 72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static class Dependency { 72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase static final int WITH = 0; // dependent node must start with this dependency node 72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase static final int AFTER = 1; // dependent node must start when this dependency node finishes 72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The node that the other node with this Dependency is dependent upon 73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Node node; 73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The nature of the dependency (WITH or AFTER) 73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public int rule; 73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Dependency(Node node, int rule) { 73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.node = node; 73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.rule = rule; 73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 74217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * A Node is an embodiment of both the Animator that it wraps as well as 74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * any dependencies that are associated with that Animation. This includes 74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * both dependencies upon other nodes (in the dependencies list) as 74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * well as dependencies of other nodes upon this (in the nodeDependents list). 74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 74849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase private static class Node implements Cloneable { 749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Animator animation; 75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * These are the dependencies that this node's animation has on other 75317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * nodes. For example, if this node's animation should begin with some 75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * other animation ends, then there will be an item in this node's 75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * dependencies list for that other animation's node. 75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public ArrayList<Dependency> dependencies = null; 75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * tmpDependencies is a runtime detail. We use the dependencies list for sorting. 76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * But we also use the list to keep track of when multiple dependencies are satisfied, 76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * but removing each dependency as it is satisfied. We do not want to remove 76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the dependency itself from the list, because we need to retain that information 764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * if the AnimatorSet is launched in the future. So we create a copy of the dependency 765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * list when the AnimatorSet starts and use this tmpDependencies list to track the 76617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * list of satisfied dependencies. 76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public ArrayList<Dependency> tmpDependencies = null; 76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * nodeDependencies is just a list of the nodes that this Node is dependent upon. 77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This information is used in sortNodes(), to determine when a node is a root. 77317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public ArrayList<Node> nodeDependencies = null; 77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * nodeDepdendents is the list of nodes that have this node as a dependency. This 77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is a utility field used in sortNodes to facilitate removing this node as a 77917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * dependency when it is a root node. 78017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 78117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public ArrayList<Node> nodeDependents = null; 78217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 78317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 7841e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase * Flag indicating whether the animation in this node is finished. This flag 785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is used by AnimatorSet to check, as each animation ends, whether all child animations 786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * are done and it's time to send out an end event for the entire AnimatorSet. 7871e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase */ 7881e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase public boolean done = false; 7891e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase 7901e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase /** 79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Constructs the Node with the animation that it encapsulates. A Node has no 79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * dependencies by default; dependencies are added via the addDependency() 79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * method. 79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param animation The animation that the Node encapsulates. 79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Node(Animator animation) { 79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.animation = animation; 79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Add a dependency to this Node. The dependency includes information about the 80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * node that this node is dependency upon and the nature of the dependency. 80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param dependency 80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void addDependency(Dependency dependency) { 80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (dependencies == null) { 80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencies = new ArrayList<Dependency>(); 80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase nodeDependencies = new ArrayList<Node>(); 81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencies.add(dependency); 81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (!nodeDependencies.contains(dependency.node)) { 81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase nodeDependencies.add(dependency.node); 81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Node dependencyNode = dependency.node; 81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (dependencyNode.nodeDependents == null) { 81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencyNode.nodeDependents = new ArrayList<Node>(); 81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase dependencyNode.nodeDependents.add(this); 82017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 82149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase 82249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase @Override 82321cd1389d2ef218b20994b617c57af120841a57fChet Haase public Node clone() { 82421cd1389d2ef218b20994b617c57af120841a57fChet Haase try { 82521cd1389d2ef218b20994b617c57af120841a57fChet Haase Node node = (Node) super.clone(); 826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase node.animation = (Animator) animation.clone(); 82721cd1389d2ef218b20994b617c57af120841a57fChet Haase return node; 82821cd1389d2ef218b20994b617c57af120841a57fChet Haase } catch (CloneNotSupportedException e) { 82921cd1389d2ef218b20994b617c57af120841a57fChet Haase throw new AssertionError(); 83021cd1389d2ef218b20994b617c57af120841a57fChet Haase } 83149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase } 83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The <code>Builder</code> object is a utility class to facilitate adding animations to a 836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>AnimatorSet</code> along with the relationships between the various animations. The 83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * intention of the <code>Builder</code> methods, along with the {@link 838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible to 83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * express the dependency relationships of animations in a natural way. Developers can also use 840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link 841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need, 842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * but it might be easier in some situations to express the AnimatorSet of animations in pairs. 84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p/> 84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed 845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * internally via a call to {@link AnimatorSet#play(Animator)}.</p> 84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p/> 847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to 84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * play when anim2 finishes, and anim4 to play when anim3 finishes:</p> 84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <pre> 850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet s = new AnimatorSet(); 85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim1).with(anim2); 85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim2).before(anim3); 85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim4).after(anim3); 85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * </pre> 85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p/> 856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Note in the example that both {@link Builder#before(Animator)} and {@link 857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Builder#after(Animator)} are used. These are just different ways of expressing the same 85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * relationship and are provided to make it easier to say things in a way that is more natural, 85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * depending on the situation.</p> 86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p/> 86117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>It is possible to make several calls into the same <code>Builder</code> object to express 86217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * multiple relationships. However, note that it is only the animation passed into the initial 863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive 86417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * calls to the <code>Builder</code> object. For example, the following code starts both anim2 86517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and 86617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * anim3: 86717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <pre> 868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet s = new AnimatorSet(); 86917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim1).before(anim2).before(anim3); 87017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * </pre> 87117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * If the desired result is to play anim1 then anim2 then anim3, this code expresses the 87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * relationship correctly:</p> 87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <pre> 874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * AnimatorSet s = new AnimatorSet(); 87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim1).before(anim2); 87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * s.play(anim2).before(anim3); 87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * </pre> 87817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p/> 87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Note that it is possible to express relationships that cannot be resolved and will not 88017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no 88117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * sense. In general, circular dependencies like this one (or more indirect ones where a depends 882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets 883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * that can boil down to a simple, one-way relationship of animations starting with, before, and 88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * after other, different, animations.</p> 88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public class Builder { 88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This tracks the current node being processed. It is supplied to the play() method 890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of AnimatorSet and passed into the constructor of Builder. 89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Node mCurrentNode; 89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * package-private constructor. Builders are only constructed by AnimatorSet, when the 89617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * play() method is called. 89717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param anim The animation that is the dependency for the other animations passed into 89917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the other methods of this Builder object. 90017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase Builder(Animator anim) { 90217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mCurrentNode = mNodeMap.get(anim); 90317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mCurrentNode == null) { 90417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mCurrentNode = new Node(anim); 90517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodeMap.put(anim, mCurrentNode); 90617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodes.add(mCurrentNode); 90717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 90817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 90917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 91017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 91117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets up the given animation to play at the same time as the animation supplied in the 912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object. 91317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 91417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param anim The animation that will play when the animation supplied to the 915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} method starts. 91617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 9172970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public Builder with(Animator anim) { 91817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Node node = mNodeMap.get(anim); 91917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node == null) { 92017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node = new Node(anim); 92117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodeMap.put(anim, node); 92217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodes.add(node); 92317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Dependency dependency = new Dependency(mCurrentNode, Dependency.WITH); 92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.addDependency(dependency); 9262970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase return this; 92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets up the given animation to play when the animation supplied in the 931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * ends. 93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param anim The animation that will play when the animation supplied to the 935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} method ends. 93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 9372970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public Builder before(Animator anim) { 93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Node node = mNodeMap.get(anim); 93917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node == null) { 94017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node = new Node(anim); 94117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodeMap.put(anim, node); 94217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodes.add(node); 94317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 94417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Dependency dependency = new Dependency(mCurrentNode, Dependency.AFTER); 94517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node.addDependency(dependency); 9462970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase return this; 94717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 94817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 94917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 95017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets up the given animation to play when the animation supplied in the 951a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 95217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * to start when the animation supplied in this method call ends. 95317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 95417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param anim The animation whose end will cause the animation supplied to the 955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} method to play. 95617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 9572970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public Builder after(Animator anim) { 95817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Node node = mNodeMap.get(anim); 95917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (node == null) { 96017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase node = new Node(anim); 96117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodeMap.put(anim, node); 96217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mNodes.add(node); 96317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 96417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Dependency dependency = new Dependency(node, Dependency.AFTER); 96517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mCurrentNode.addDependency(dependency); 9662970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase return this; 96717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 96817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 96917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 97017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets up the animation supplied in the 971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object 97217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * to play when the given amount of time elapses. 97317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 97417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param delay The number of milliseconds that should elapse before the 97517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation starts. 97617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 9772970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase public Builder after(long delay) { 978a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // setup dummy ValueAnimator just to run the clock 9792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 9802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setDuration(delay); 9812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase after(anim); 9822970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase return this; 98317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 98417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 98517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 98617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 98717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase} 988