Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 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 | |
| 17 | package android.animation; |
| 18 | |
Doris Liu | d744442 | 2015-05-11 13:23:31 -0700 | [diff] [blame] | 19 | import android.util.ArrayMap; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 20 | import android.util.Log; |
Doris Liu | d744442 | 2015-05-11 13:23:31 -0700 | [diff] [blame] | 21 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 23 | import java.util.Collection; |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 24 | import java.util.List; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 27 | * This class plays a set of {@link Animator} objects in the specified order. Animations |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 28 | * can be set up to play together, in sequence, or after a specified delay. |
| 29 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 30 | * <p>There are two different approaches to adding animations to a <code>AnimatorSet</code>: |
| 31 | * either the {@link AnimatorSet#playTogether(Animator[]) playTogether()} or |
| 32 | * {@link AnimatorSet#playSequentially(Animator[]) playSequentially()} methods can be called to add |
| 33 | * a set of animations all at once, or the {@link AnimatorSet#play(Animator)} can be |
| 34 | * used in conjunction with methods in the {@link AnimatorSet.Builder Builder} |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 35 | * class to add animations |
| 36 | * one by one.</p> |
| 37 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 38 | * <p>It is possible to set up a <code>AnimatorSet</code> with circular dependencies between |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 39 | * its animations. For example, an animation a1 could be set up to start before animation a2, a2 |
| 40 | * before a3, and a3 before a1. The results of this configuration are undefined, but will typically |
| 41 | * result in none of the affected animations being played. Because of this (and because |
| 42 | * circular dependencies do not make logical sense anyway), circular dependencies |
| 43 | * should be avoided, and the dependency flow of animations should only be in one direction. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 44 | * |
| 45 | * <div class="special reference"> |
| 46 | * <h3>Developer Guides</h3> |
| 47 | * <p>For more information about animating with {@code AnimatorSet}, read the |
| 48 | * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#choreography">Property |
| 49 | * Animation</a> developer guide.</p> |
| 50 | * </div> |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 51 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 52 | public final class AnimatorSet extends Animator { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 53 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 54 | private static final String TAG = "AnimatorSet"; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 55 | /** |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 56 | * Internal variables |
| 57 | * NOTE: This object implements the clone() method, making a deep copy of any referenced |
| 58 | * objects. As other non-trivial fields are added to this class, make sure to add logic |
| 59 | * to clone() to make deep copies of them. |
| 60 | */ |
| 61 | |
| 62 | /** |
Chet Haase | 3b69b6f | 2010-07-29 09:09:05 -0700 | [diff] [blame] | 63 | * Tracks animations currently being played, so that we know what to |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 64 | * cancel or end when cancel() or end() is called on this AnimatorSet |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 65 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 66 | private ArrayList<Animator> mPlayingSet = new ArrayList<Animator>(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 69 | * Contains all nodes, mapped to their respective Animators. When new |
| 70 | * dependency information is added for an Animator, we want to add it |
| 71 | * to a single node representing that Animator, not create a new Node |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 72 | * if one already exists. |
| 73 | */ |
Doris Liu | d744442 | 2015-05-11 13:23:31 -0700 | [diff] [blame] | 74 | private ArrayMap<Animator, Node> mNodeMap = new ArrayMap<Animator, Node>(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 77 | * Set of all nodes created for this AnimatorSet. This list is used upon |
| 78 | * starting the set, and the nodes are placed in sorted order into the |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 79 | * sortedNodes collection. |
| 80 | */ |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 81 | private ArrayList<Node> mNodes = new ArrayList<Node>(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 84 | * Animator Listener that tracks the lifecycle of each Animator in the set. It will be added |
| 85 | * to each Animator before they start and removed after they end. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 86 | */ |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 87 | private AnimatorSetListener mSetListener = new AnimatorSetListener(this); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 90 | * Flag indicating that the AnimatorSet has been manually |
| 91 | * terminated (by calling cancel() or end()). |
Chet Haase | 010dbaa | 2010-07-19 17:29:49 -0700 | [diff] [blame] | 92 | * This flag is used to avoid starting other animations when currently-playing |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 93 | * child animations of this AnimatorSet end. It also determines whether cancel/end |
| 94 | * notifications are sent out via the normal AnimatorSetListener mechanism. |
Chet Haase | 010dbaa | 2010-07-19 17:29:49 -0700 | [diff] [blame] | 95 | */ |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 96 | private boolean mTerminated = false; |
| 97 | |
| 98 | /** |
| 99 | * Tracks whether any change has been made to the AnimatorSet, which is then used to |
| 100 | * determine whether the dependency graph should be re-constructed. |
| 101 | */ |
| 102 | private boolean mDependencyDirty = false; |
Chet Haase | 010dbaa | 2010-07-19 17:29:49 -0700 | [diff] [blame] | 103 | |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Indicates whether an AnimatorSet has been start()'d, whether or |
| 106 | * not there is a nonzero startDelay. |
| 107 | */ |
| 108 | private boolean mStarted = false; |
| 109 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 110 | // The amount of time in ms to delay starting the animation after start() is called |
| 111 | private long mStartDelay = 0; |
| 112 | |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 113 | // Animator used for a nonzero startDelay |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 114 | private ValueAnimator mDelayAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(0); |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 115 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 116 | // Root of the dependency tree of all the animators in the set. In this tree, parent-child |
| 117 | // relationship captures the order of animation (i.e. parent and child will play sequentially), |
| 118 | // and sibling relationship indicates "with" relationship, as sibling animators start at the |
| 119 | // same time. |
| 120 | private Node mRootNode = new Node(mDelayAnim); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 121 | |
| 122 | // How long the child animations should last in ms. The default value is negative, which |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 123 | // simply means that there is no duration set on the AnimatorSet. When a real duration is |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 124 | // set, it is passed along to the child animations. |
| 125 | private long mDuration = -1; |
| 126 | |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 127 | // Records the interpolator for the set. Null value indicates that no interpolator |
| 128 | // was set on this AnimatorSet, so it should not be passed down to the children. |
| 129 | private TimeInterpolator mInterpolator = null; |
| 130 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 131 | // Whether the AnimatorSet can be reversed. |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 132 | private boolean mReversible = true; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 133 | // The total duration of finishing all the Animators in the set. |
| 134 | private long mTotalDuration = 0; |
| 135 | |
| 136 | public AnimatorSet() { |
| 137 | super(); |
| 138 | mNodeMap.put(mDelayAnim, mRootNode); |
| 139 | mNodes.add(mRootNode); |
| 140 | } |
| 141 | |
Chet Haase | 010dbaa | 2010-07-19 17:29:49 -0700 | [diff] [blame] | 142 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 143 | * Sets up this AnimatorSet to play all of the supplied animations at the same time. |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 144 | * This is equivalent to calling {@link #play(Animator)} with the first animator in the |
| 145 | * set and then {@link Builder#with(Animator)} with each of the other animators. Note that |
| 146 | * an Animator with a {@link Animator#setStartDelay(long) startDelay} will not actually |
| 147 | * start until that delay elapses, which means that if the first animator in the list |
| 148 | * supplied to this constructor has a startDelay, none of the other animators will start |
| 149 | * until that first animator's startDelay has elapsed. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 150 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 151 | * @param items The animations that will be started simultaneously. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 152 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 153 | public void playTogether(Animator... items) { |
| 154 | if (items != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 155 | Builder builder = play(items[0]); |
| 156 | for (int i = 1; i < items.length; ++i) { |
| 157 | builder.with(items[i]); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 163 | * Sets up this AnimatorSet to play all of the supplied animations at the same time. |
| 164 | * |
| 165 | * @param items The animations that will be started simultaneously. |
| 166 | */ |
| 167 | public void playTogether(Collection<Animator> items) { |
| 168 | if (items != null && items.size() > 0) { |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 169 | Builder builder = null; |
| 170 | for (Animator anim : items) { |
| 171 | if (builder == null) { |
| 172 | builder = play(anim); |
| 173 | } else { |
| 174 | builder.with(anim); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 181 | * Sets up this AnimatorSet to play each of the supplied animations when the |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 182 | * previous animation ends. |
| 183 | * |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 184 | * @param items The animations that will be started one after another. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 185 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 186 | public void playSequentially(Animator... items) { |
| 187 | if (items != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 188 | if (items.length == 1) { |
| 189 | play(items[0]); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 190 | } else { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 191 | mReversible = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 192 | for (int i = 0; i < items.length - 1; ++i) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 193 | play(items[i]).before(items[i + 1]); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 200 | * Sets up this AnimatorSet to play each of the supplied animations when the |
| 201 | * previous animation ends. |
| 202 | * |
| 203 | * @param items The animations that will be started one after another. |
| 204 | */ |
| 205 | public void playSequentially(List<Animator> items) { |
| 206 | if (items != null && items.size() > 0) { |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 207 | if (items.size() == 1) { |
| 208 | play(items.get(0)); |
| 209 | } else { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 210 | mReversible = false; |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 211 | for (int i = 0; i < items.size() - 1; ++i) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 212 | play(items.get(i)).before(items.get(i + 1)); |
Chet Haase | 37a7bec | 2010-11-30 15:55:39 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 219 | * Returns the current list of child Animator objects controlled by this |
| 220 | * AnimatorSet. This is a copy of the internal list; modifications to the returned list |
| 221 | * will not affect the AnimatorSet, although changes to the underlying Animator objects |
| 222 | * will affect those objects being managed by the AnimatorSet. |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 223 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 224 | * @return ArrayList<Animator> The list of child animations of this AnimatorSet. |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 225 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 226 | public ArrayList<Animator> getChildAnimations() { |
| 227 | ArrayList<Animator> childList = new ArrayList<Animator>(); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 228 | int size = mNodes.size(); |
| 229 | for (int i = 0; i < size; i++) { |
| 230 | Node node = mNodes.get(i); |
Doris Liu | dbf69e4 | 2015-08-05 20:00:11 -0700 | [diff] [blame] | 231 | if (node != mRootNode) { |
| 232 | childList.add(node.mAnimation); |
| 233 | } |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 234 | } |
| 235 | return childList; |
| 236 | } |
| 237 | |
| 238 | /** |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 239 | * Sets the target object for all current {@link #getChildAnimations() child animations} |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 240 | * of this AnimatorSet that take targets ({@link ObjectAnimator} and |
| 241 | * AnimatorSet). |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 242 | * |
| 243 | * @param target The object being animated |
| 244 | */ |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 245 | @Override |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 246 | public void setTarget(Object target) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 247 | int size = mNodes.size(); |
| 248 | for (int i = 0; i < size; i++) { |
| 249 | Node node = mNodes.get(i); |
| 250 | Animator animation = node.mAnimation; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 251 | if (animation instanceof AnimatorSet) { |
| 252 | ((AnimatorSet)animation).setTarget(target); |
| 253 | } else if (animation instanceof ObjectAnimator) { |
| 254 | ((ObjectAnimator)animation).setTarget(target); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 260 | * @hide |
| 261 | */ |
| 262 | @Override |
| 263 | public int getChangingConfigurations() { |
| 264 | int conf = super.getChangingConfigurations(); |
| 265 | final int nodeCount = mNodes.size(); |
| 266 | for (int i = 0; i < nodeCount; i ++) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 267 | conf |= mNodes.get(i).mAnimation.getChangingConfigurations(); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 268 | } |
| 269 | return conf; |
| 270 | } |
| 271 | |
| 272 | /** |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 273 | * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations} |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 274 | * of this AnimatorSet. The default value is null, which means that no interpolator |
| 275 | * is set on this AnimatorSet. Setting the interpolator to any non-null value |
| 276 | * will cause that interpolator to be set on the child animations |
| 277 | * when the set is started. |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 278 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 279 | * @param interpolator the interpolator to be used by each child animation of this AnimatorSet |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 280 | */ |
| 281 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 282 | public void setInterpolator(TimeInterpolator interpolator) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 283 | mInterpolator = interpolator; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public TimeInterpolator getInterpolator() { |
| 288 | return mInterpolator; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 292 | * This method creates a <code>Builder</code> object, which is used to |
| 293 | * set up playing constraints. This initial <code>play()</code> method |
| 294 | * tells the <code>Builder</code> the animation that is the dependency for |
| 295 | * the succeeding commands to the <code>Builder</code>. For example, |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 296 | * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 297 | * <code>a1</code> and <code>a2</code> at the same time, |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 298 | * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 299 | * <code>a1</code> first, followed by <code>a2</code>, and |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 300 | * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 301 | * <code>a2</code> first, followed by <code>a1</code>. |
| 302 | * |
| 303 | * <p>Note that <code>play()</code> is the only way to tell the |
| 304 | * <code>Builder</code> the animation upon which the dependency is created, |
| 305 | * so successive calls to the various functions in <code>Builder</code> |
| 306 | * will all refer to the initial parameter supplied in <code>play()</code> |
| 307 | * as the dependency of the other animations. For example, calling |
| 308 | * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code> |
| 309 | * and <code>a3</code> when a1 ends; it does not set up a dependency between |
| 310 | * <code>a2</code> and <code>a3</code>.</p> |
| 311 | * |
| 312 | * @param anim The animation that is the dependency used in later calls to the |
| 313 | * methods in the returned <code>Builder</code> object. A null parameter will result |
| 314 | * in a null <code>Builder</code> return value. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 315 | * @return Builder The object that constructs the AnimatorSet based on the dependencies |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 316 | * outlined in the calls to <code>play</code> and the other methods in the |
| 317 | * <code>Builder</code object. |
| 318 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 319 | public Builder play(Animator anim) { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 320 | if (anim != null) { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 321 | return new Builder(anim); |
| 322 | } |
| 323 | return null; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * {@inheritDoc} |
| 328 | * |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 329 | * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it |
| 330 | * is responsible for.</p> |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 331 | */ |
| 332 | @SuppressWarnings("unchecked") |
| 333 | @Override |
| 334 | public void cancel() { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 335 | mTerminated = true; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 336 | if (isStarted()) { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 337 | ArrayList<AnimatorListener> tmpListeners = null; |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 338 | if (mListeners != null) { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 339 | tmpListeners = (ArrayList<AnimatorListener>) mListeners.clone(); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 340 | int size = tmpListeners.size(); |
| 341 | for (int i = 0; i < size; i++) { |
| 342 | tmpListeners.get(i).onAnimationCancel(this); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 345 | ArrayList<Animator> playingSet = new ArrayList<>(mPlayingSet); |
| 346 | int setSize = playingSet.size(); |
| 347 | for (int i = 0; i < setSize; i++) { |
| 348 | playingSet.get(i).cancel(); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 349 | } |
| 350 | if (tmpListeners != null) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 351 | int size = tmpListeners.size(); |
| 352 | for (int i = 0; i < size; i++) { |
| 353 | tmpListeners.get(i).onAnimationEnd(this); |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 356 | mStarted = false; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * {@inheritDoc} |
| 362 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 363 | * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 364 | * responsible for.</p> |
| 365 | */ |
| 366 | @Override |
| 367 | public void end() { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 368 | mTerminated = true; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 369 | if (isStarted()) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 370 | endRemainingAnimations(); |
| 371 | } |
| 372 | if (mListeners != null) { |
| 373 | ArrayList<AnimatorListener> tmpListeners = |
| 374 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 375 | for (int i = 0; i < tmpListeners.size(); i++) { |
| 376 | tmpListeners.get(i).onAnimationEnd(this); |
| 377 | } |
| 378 | } |
| 379 | mStarted = false; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Iterate the animations that haven't finished or haven't started, and end them. |
| 384 | */ |
| 385 | private void endRemainingAnimations() { |
| 386 | ArrayList<Animator> remainingList = new ArrayList<Animator>(mNodes.size()); |
| 387 | remainingList.addAll(mPlayingSet); |
| 388 | |
| 389 | int index = 0; |
| 390 | while (index < remainingList.size()) { |
| 391 | Animator anim = remainingList.get(index); |
| 392 | anim.end(); |
| 393 | index++; |
| 394 | Node node = mNodeMap.get(anim); |
| 395 | if (node.mChildNodes != null) { |
| 396 | int childSize = node.mChildNodes.size(); |
| 397 | for (int i = 0; i < childSize; i++) { |
| 398 | Node child = node.mChildNodes.get(i); |
| 399 | if (child.mLatestParent != node) { |
| 400 | continue; |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 401 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 402 | remainingList.add(child.mAnimation); |
Chet Haase | 1e0ac5a | 2010-08-27 08:32:11 -0700 | [diff] [blame] | 403 | } |
Chet Haase | 1e0ac5a | 2010-08-27 08:32:11 -0700 | [diff] [blame] | 404 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 408 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 409 | /** |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 410 | * Returns true if any of the child animations of this AnimatorSet have been started and have |
Doris Liu | ee68455 | 2015-08-28 13:21:03 -0700 | [diff] [blame] | 411 | * not yet ended. Child animations will not be started until the AnimatorSet has gone past |
| 412 | * its initial delay set through {@link #setStartDelay(long)}. |
| 413 | * |
| 414 | * @return Whether this AnimatorSet has gone past the initial delay, and at least one child |
| 415 | * animation has been started and not yet ended. |
Chet Haase | 673e42f | 2010-08-25 16:32:37 -0700 | [diff] [blame] | 416 | */ |
| 417 | @Override |
| 418 | public boolean isRunning() { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 419 | int size = mNodes.size(); |
| 420 | for (int i = 0; i < size; i++) { |
Doris Liu | 458f20e | 2015-08-10 17:32:54 -0700 | [diff] [blame] | 421 | Node node = mNodes.get(i); |
Doris Liu | ee68455 | 2015-08-28 13:21:03 -0700 | [diff] [blame] | 422 | if (node != mRootNode && node.mAnimation.isStarted()) { |
Chet Haase | 673e42f | 2010-08-25 16:32:37 -0700 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public boolean isStarted() { |
| 431 | return mStarted; |
Chet Haase | 673e42f | 2010-08-25 16:32:37 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 435 | * The amount of time, in milliseconds, to delay starting the animation after |
| 436 | * {@link #start()} is called. |
| 437 | * |
| 438 | * @return the number of milliseconds to delay running the animation |
| 439 | */ |
| 440 | @Override |
| 441 | public long getStartDelay() { |
| 442 | return mStartDelay; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * The amount of time, in milliseconds, to delay starting the animation after |
| 447 | * {@link #start()} is called. |
| 448 | |
| 449 | * @param startDelay The amount of the delay, in milliseconds |
| 450 | */ |
| 451 | @Override |
| 452 | public void setStartDelay(long startDelay) { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 453 | if (mStartDelay > 0) { |
| 454 | mReversible = false; |
| 455 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 456 | long delta = startDelay - mStartDelay; |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 457 | if (delta == 0) { |
| 458 | return; |
| 459 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 460 | mStartDelay = startDelay; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 461 | if (!mDependencyDirty) { |
| 462 | // Dependency graph already constructed, update all the nodes' start/end time |
| 463 | int size = mNodes.size(); |
| 464 | for (int i = 0; i < size; i++) { |
| 465 | Node node = mNodes.get(i); |
| 466 | if (node == mRootNode) { |
| 467 | node.mEndTime = mStartDelay; |
| 468 | } else { |
| 469 | node.mStartTime = node.mStartTime == DURATION_INFINITE ? |
| 470 | DURATION_INFINITE : node.mStartTime + delta; |
| 471 | node.mEndTime = node.mEndTime == DURATION_INFINITE ? |
| 472 | DURATION_INFINITE : node.mEndTime + delta; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 475 | // Update total duration, if necessary. |
| 476 | if (mTotalDuration != DURATION_INFINITE) { |
| 477 | mTotalDuration += delta; |
| 478 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 479 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 483 | * Gets the length of each of the child animations of this AnimatorSet. This value may |
| 484 | * be less than 0, which indicates that no duration has been set on this AnimatorSet |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 485 | * and each of the child animations will use their own duration. |
| 486 | * |
| 487 | * @return The length of the animation, in milliseconds, of each of the child |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 488 | * animations of this AnimatorSet. |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 489 | */ |
| 490 | @Override |
| 491 | public long getDuration() { |
| 492 | return mDuration; |
| 493 | } |
| 494 | |
| 495 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 496 | * Sets the length of each of the current child animations of this AnimatorSet. By default, |
| 497 | * each child animation will use its own duration. If the duration is set on the AnimatorSet, |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 498 | * then each child animation inherits this duration. |
| 499 | * |
| 500 | * @param duration The length of the animation, in milliseconds, of each of the child |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 501 | * animations of this AnimatorSet. |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 502 | */ |
| 503 | @Override |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 504 | public AnimatorSet setDuration(long duration) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 505 | if (duration < 0) { |
| 506 | throw new IllegalArgumentException("duration must be a value of zero or greater"); |
| 507 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 508 | mDependencyDirty = true; |
Chet Haase | c299a33 | 2012-04-12 07:51:50 -0700 | [diff] [blame] | 509 | // Just record the value for now - it will be used later when the AnimatorSet starts |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 510 | mDuration = duration; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 511 | return this; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 514 | @Override |
| 515 | public void setupStartValues() { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 516 | int size = mNodes.size(); |
| 517 | for (int i = 0; i < size; i++) { |
| 518 | Node node = mNodes.get(i); |
Doris Liu | 458f20e | 2015-08-10 17:32:54 -0700 | [diff] [blame] | 519 | if (node != mRootNode) { |
| 520 | node.mAnimation.setupStartValues(); |
| 521 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public void setupEndValues() { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 527 | int size = mNodes.size(); |
| 528 | for (int i = 0; i < size; i++) { |
| 529 | Node node = mNodes.get(i); |
Doris Liu | 458f20e | 2015-08-10 17:32:54 -0700 | [diff] [blame] | 530 | if (node != mRootNode) { |
| 531 | node.mAnimation.setupEndValues(); |
| 532 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 536 | @Override |
| 537 | public void pause() { |
| 538 | boolean previouslyPaused = mPaused; |
| 539 | super.pause(); |
| 540 | if (!previouslyPaused && mPaused) { |
| 541 | if (mDelayAnim != null) { |
| 542 | mDelayAnim.pause(); |
| 543 | } else { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 544 | int size = mNodes.size(); |
| 545 | for (int i = 0; i < size; i++) { |
| 546 | Node node = mNodes.get(i); |
Doris Liu | 458f20e | 2015-08-10 17:32:54 -0700 | [diff] [blame] | 547 | if (node != mRootNode) { |
| 548 | node.mAnimation.pause(); |
| 549 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public void resume() { |
| 557 | boolean previouslyPaused = mPaused; |
| 558 | super.resume(); |
| 559 | if (previouslyPaused && !mPaused) { |
| 560 | if (mDelayAnim != null) { |
| 561 | mDelayAnim.resume(); |
| 562 | } else { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 563 | int size = mNodes.size(); |
| 564 | for (int i = 0; i < size; i++) { |
| 565 | Node node = mNodes.get(i); |
Doris Liu | 458f20e | 2015-08-10 17:32:54 -0700 | [diff] [blame] | 566 | if (node != mRootNode) { |
| 567 | node.mAnimation.resume(); |
| 568 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 574 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 575 | * {@inheritDoc} |
| 576 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 577 | * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 578 | * it is responsible. The details of when exactly those animations are started depends on |
| 579 | * the dependency relationships that have been set up between the animations. |
| 580 | */ |
| 581 | @SuppressWarnings("unchecked") |
| 582 | @Override |
| 583 | public void start() { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 584 | mTerminated = false; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 585 | mStarted = true; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 586 | mPaused = false; |
Chet Haase | 010dbaa | 2010-07-19 17:29:49 -0700 | [diff] [blame] | 587 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 588 | int size = mNodes.size(); |
| 589 | for (int i = 0; i < size; i++) { |
| 590 | Node node = mNodes.get(i); |
| 591 | node.mEnded = false; |
| 592 | node.mAnimation.setAllowRunningAsynchronously(false); |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 595 | if (mInterpolator != null) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 596 | for (int i = 0; i < size; i++) { |
| 597 | Node node = mNodes.get(i); |
| 598 | node.mAnimation.setInterpolator(mInterpolator); |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 602 | updateAnimatorsDuration(); |
| 603 | createDependencyGraph(); |
| 604 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 605 | // Now that all dependencies are set up, start the animations that should be started. |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 606 | boolean setIsEmpty = false; |
| 607 | if (mStartDelay > 0) { |
| 608 | start(mRootNode); |
| 609 | } else if (mNodes.size() > 1) { |
| 610 | // No delay, but there are other animators in the set |
| 611 | onChildAnimatorEnded(mDelayAnim); |
| 612 | } else { |
| 613 | // Set is empty, no delay, no other animation. Skip to end in this case |
| 614 | setIsEmpty = true; |
| 615 | } |
| 616 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 617 | if (mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 618 | ArrayList<AnimatorListener> tmpListeners = |
| 619 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 620 | int numListeners = tmpListeners.size(); |
| 621 | for (int i = 0; i < numListeners; ++i) { |
| 622 | tmpListeners.get(i).onAnimationStart(this); |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 625 | if (setIsEmpty) { |
| 626 | // In the case of empty AnimatorSet, we will trigger the onAnimationEnd() right away. |
| 627 | onChildAnimatorEnded(mDelayAnim); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 631 | private void updateAnimatorsDuration() { |
| 632 | if (mDuration >= 0) { |
| 633 | // If the duration was set on this AnimatorSet, pass it along to all child animations |
| 634 | int size = mNodes.size(); |
| 635 | for (int i = 0; i < size; i++) { |
| 636 | Node node = mNodes.get(i); |
| 637 | // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to |
| 638 | // insert "play-after" delays |
| 639 | node.mAnimation.setDuration(mDuration); |
| 640 | } |
| 641 | } |
| 642 | mDelayAnim.setDuration(mStartDelay); |
| 643 | } |
| 644 | |
| 645 | void start(final Node node) { |
| 646 | final Animator anim = node.mAnimation; |
| 647 | mPlayingSet.add(anim); |
| 648 | anim.addListener(mSetListener); |
| 649 | anim.start(); |
| 650 | } |
| 651 | |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 652 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 653 | public AnimatorSet clone() { |
| 654 | final AnimatorSet anim = (AnimatorSet) super.clone(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 655 | /* |
| 656 | * The basic clone() operation copies all items. This doesn't work very well for |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 657 | * AnimatorSet, because it will copy references that need to be recreated and state |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 658 | * that may not apply. What we need to do now is put the clone in an uninitialized |
| 659 | * state, with fresh, empty data structures. Then we will build up the nodes list |
| 660 | * manually, as we clone each Node (and its animation). The clone will then be sorted, |
| 661 | * and will populate any appropriate lists, when it is started. |
| 662 | */ |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 663 | final int nodeCount = mNodes.size(); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 664 | anim.mTerminated = false; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 665 | anim.mStarted = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 666 | anim.mPlayingSet = new ArrayList<Animator>(); |
Doris Liu | d744442 | 2015-05-11 13:23:31 -0700 | [diff] [blame] | 667 | anim.mNodeMap = new ArrayMap<Animator, Node>(); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 668 | anim.mNodes = new ArrayList<Node>(nodeCount); |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 669 | anim.mReversible = mReversible; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 670 | anim.mSetListener = new AnimatorSetListener(anim); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 671 | |
| 672 | // Walk through the old nodes list, cloning each node and adding it to the new nodemap. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 673 | // One problem is that the old node dependencies point to nodes in the old AnimatorSet. |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 674 | // We need to track the old/new nodes in order to reconstruct the dependencies in the clone. |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 675 | |
| 676 | for (int n = 0; n < nodeCount; n++) { |
| 677 | final Node node = mNodes.get(n); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 678 | Node nodeClone = node.clone(); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 679 | node.mTmpClone = nodeClone; |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 680 | anim.mNodes.add(nodeClone); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 681 | anim.mNodeMap.put(nodeClone.mAnimation, nodeClone); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 682 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 683 | // clear out any listeners that were set up by the AnimatorSet |
| 684 | final ArrayList<AnimatorListener> cloneListeners = nodeClone.mAnimation.getListeners(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 685 | if (cloneListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 686 | for (int i = cloneListeners.size() - 1; i >= 0; i--) { |
| 687 | final AnimatorListener listener = cloneListeners.get(i); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 688 | if (listener instanceof AnimatorSetListener) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 689 | cloneListeners.remove(i); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 694 | |
| 695 | anim.mRootNode = mRootNode.mTmpClone; |
| 696 | anim.mDelayAnim = (ValueAnimator) anim.mRootNode.mAnimation; |
| 697 | |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 698 | // Now that we've cloned all of the nodes, we're ready to walk through their |
| 699 | // dependencies, mapping the old dependencies to the new nodes |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 700 | for (int i = 0; i < nodeCount; i++) { |
| 701 | Node node = mNodes.get(i); |
| 702 | // Update dependencies for node's clone |
| 703 | node.mTmpClone.mLatestParent = node.mLatestParent == null ? |
| 704 | null : node.mLatestParent.mTmpClone; |
| 705 | int size = node.mChildNodes == null ? 0 : node.mChildNodes.size(); |
| 706 | for (int j = 0; j < size; j++) { |
| 707 | node.mTmpClone.mChildNodes.set(j, node.mChildNodes.get(j).mTmpClone); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 708 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 709 | size = node.mSiblings == null ? 0 : node.mSiblings.size(); |
| 710 | for (int j = 0; j < size; j++) { |
| 711 | node.mTmpClone.mSiblings.set(j, node.mSiblings.get(j).mTmpClone); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 712 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 713 | size = node.mParents == null ? 0 : node.mParents.size(); |
| 714 | for (int j = 0; j < size; j++) { |
| 715 | node.mTmpClone.mParents.set(j, node.mParents.get(j).mTmpClone); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 718 | |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 719 | for (int n = 0; n < nodeCount; n++) { |
| 720 | mNodes.get(n).mTmpClone = null; |
| 721 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 722 | return anim; |
| 723 | } |
| 724 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 725 | |
Doris Liu | 6f2276b | 2015-08-06 13:51:31 -0700 | [diff] [blame] | 726 | private static class AnimatorSetListener implements AnimatorListener { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 727 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 728 | private AnimatorSet mAnimatorSet; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 729 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 730 | AnimatorSetListener(AnimatorSet animatorSet) { |
| 731 | mAnimatorSet = animatorSet; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 734 | public void onAnimationCancel(Animator animation) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 735 | |
Doris Liu | 6f2276b | 2015-08-06 13:51:31 -0700 | [diff] [blame] | 736 | if (!mAnimatorSet.mTerminated) { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 737 | // Listeners are already notified of the AnimatorSet canceling in cancel(). |
| 738 | // The logic below only kicks in when animations end normally |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 739 | if (mAnimatorSet.mPlayingSet.size() == 0) { |
| 740 | ArrayList<AnimatorListener> listeners = mAnimatorSet.mListeners; |
| 741 | if (listeners != null) { |
| 742 | int numListeners = listeners.size(); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 743 | for (int i = 0; i < numListeners; ++i) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 744 | listeners.get(i).onAnimationCancel(mAnimatorSet); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 745 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | @SuppressWarnings("unchecked") |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 752 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 753 | animation.removeListener(this); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 754 | mAnimatorSet.mPlayingSet.remove(animation); |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 755 | mAnimatorSet.onChildAnimatorEnded(animation); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // Nothing to do |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 759 | public void onAnimationRepeat(Animator animation) { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // Nothing to do |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 763 | public void onAnimationStart(Animator animation) { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | } |
| 767 | |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 768 | private void onChildAnimatorEnded(Animator animation) { |
| 769 | Node animNode = mNodeMap.get(animation); |
| 770 | animNode.mEnded = true; |
| 771 | |
| 772 | if (!mTerminated) { |
| 773 | List<Node> children = animNode.mChildNodes; |
| 774 | // Start children animations, if any. |
| 775 | int childrenSize = children == null ? 0 : children.size(); |
| 776 | for (int i = 0; i < childrenSize; i++) { |
| 777 | if (children.get(i).mLatestParent == animNode) { |
| 778 | start(children.get(i)); |
| 779 | } |
| 780 | } |
| 781 | // Listeners are already notified of the AnimatorSet ending in cancel() or |
| 782 | // end(); the logic below only kicks in when animations end normally |
| 783 | boolean allDone = true; |
| 784 | // Traverse the tree and find if there's any unfinished node |
| 785 | int size = mNodes.size(); |
| 786 | for (int i = 0; i < size; i++) { |
| 787 | if (!mNodes.get(i).mEnded) { |
| 788 | allDone = false; |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | if (allDone) { |
| 793 | // If this was the last child animation to end, then notify listeners that this |
| 794 | // AnimatorSet has ended |
| 795 | if (mListeners != null) { |
| 796 | ArrayList<AnimatorListener> tmpListeners = |
| 797 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 798 | int numListeners = tmpListeners.size(); |
| 799 | for (int i = 0; i < numListeners; ++i) { |
| 800 | tmpListeners.get(i).onAnimationEnd(this); |
| 801 | } |
| 802 | } |
| 803 | mStarted = false; |
| 804 | mPaused = false; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 809 | /** |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 810 | * AnimatorSet is only reversible when the set contains no sequential animation, and no child |
| 811 | * animators have a start delay. |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 812 | * @hide |
| 813 | */ |
| 814 | @Override |
| 815 | public boolean canReverse() { |
| 816 | if (!mReversible) { |
| 817 | return false; |
| 818 | } |
| 819 | // Loop to make sure all the Nodes can reverse. |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 820 | int size = mNodes.size(); |
| 821 | for (int i = 0; i < size; i++) { |
| 822 | Node node = mNodes.get(i); |
| 823 | if (!node.mAnimation.canReverse() || node.mAnimation.getStartDelay() > 0) { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | } |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * @hide |
| 832 | */ |
| 833 | @Override |
| 834 | public void reverse() { |
| 835 | if (canReverse()) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 836 | int size = mNodes.size(); |
| 837 | for (int i = 0; i < size; i++) { |
| 838 | Node node = mNodes.get(i); |
| 839 | node.mAnimation.reverse(); |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
Chet Haase | d430753 | 2014-12-01 06:32:38 -0800 | [diff] [blame] | 844 | @Override |
| 845 | public String toString() { |
| 846 | String returnVal = "AnimatorSet@" + Integer.toHexString(hashCode()) + "{"; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 847 | int size = mNodes.size(); |
| 848 | for (int i = 0; i < size; i++) { |
| 849 | Node node = mNodes.get(i); |
| 850 | returnVal += "\n " + node.mAnimation.toString(); |
Chet Haase | d430753 | 2014-12-01 06:32:38 -0800 | [diff] [blame] | 851 | } |
| 852 | return returnVal + "\n}"; |
| 853 | } |
| 854 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 855 | private void printChildCount() { |
| 856 | // Print out the child count through a level traverse. |
| 857 | ArrayList<Node> list = new ArrayList<>(mNodes.size()); |
| 858 | list.add(mRootNode); |
| 859 | Log.d(TAG, "Current tree: "); |
| 860 | int index = 0; |
| 861 | while (index < list.size()) { |
| 862 | int listSize = list.size(); |
| 863 | StringBuilder builder = new StringBuilder(); |
| 864 | for (; index < listSize; index++) { |
| 865 | Node node = list.get(index); |
| 866 | int num = 0; |
| 867 | if (node.mChildNodes != null) { |
| 868 | for (int i = 0; i < node.mChildNodes.size(); i++) { |
| 869 | Node child = node.mChildNodes.get(i); |
| 870 | if (child.mLatestParent == node) { |
| 871 | num++; |
| 872 | list.add(child); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | builder.append(" "); |
| 877 | builder.append(num); |
| 878 | } |
| 879 | Log.d(TAG, builder.toString()); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 883 | private void createDependencyGraph() { |
| 884 | if (!mDependencyDirty) { |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 885 | // Check whether any duration of the child animations has changed |
| 886 | boolean durationChanged = false; |
| 887 | for (int i = 0; i < mNodes.size(); i++) { |
| 888 | Animator anim = mNodes.get(i).mAnimation; |
| 889 | if (mNodes.get(i).mTotalDuration != anim.getTotalDuration()) { |
| 890 | durationChanged = true; |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | if (!durationChanged) { |
| 895 | return; |
| 896 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 899 | mDependencyDirty = false; |
| 900 | // Traverse all the siblings and make sure they have all the parents |
| 901 | int size = mNodes.size(); |
| 902 | for (int i = 0; i < size; i++) { |
| 903 | mNodes.get(i).mParentsAdded = false; |
| 904 | } |
| 905 | for (int i = 0; i < size; i++) { |
| 906 | Node node = mNodes.get(i); |
| 907 | if (node.mParentsAdded) { |
| 908 | continue; |
| 909 | } |
| 910 | |
| 911 | node.mParentsAdded = true; |
| 912 | if (node.mSiblings == null) { |
| 913 | continue; |
| 914 | } |
| 915 | |
| 916 | // Find all the siblings |
| 917 | findSiblings(node, node.mSiblings); |
| 918 | node.mSiblings.remove(node); |
| 919 | |
| 920 | // Get parents from all siblings |
| 921 | int siblingSize = node.mSiblings.size(); |
| 922 | for (int j = 0; j < siblingSize; j++) { |
| 923 | node.addParents(node.mSiblings.get(j).mParents); |
| 924 | } |
| 925 | |
| 926 | // Now make sure all siblings share the same set of parents |
| 927 | for (int j = 0; j < siblingSize; j++) { |
| 928 | Node sibling = node.mSiblings.get(j); |
| 929 | sibling.addParents(node.mParents); |
| 930 | sibling.mParentsAdded = true; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | for (int i = 0; i < size; i++) { |
| 935 | Node node = mNodes.get(i); |
| 936 | if (node != mRootNode && node.mParents == null) { |
| 937 | node.addParent(mRootNode); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Do a DFS on the tree |
| 942 | ArrayList<Node> visited = new ArrayList<Node>(mNodes.size()); |
| 943 | // Assign start/end time |
| 944 | mRootNode.mStartTime = 0; |
| 945 | mRootNode.mEndTime = mDelayAnim.getDuration(); |
| 946 | updatePlayTime(mRootNode, visited); |
| 947 | |
| 948 | long maxEndTime = 0; |
| 949 | for (int i = 0; i < size; i++) { |
| 950 | Node node = mNodes.get(i); |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 951 | node.mTotalDuration = node.mAnimation.getTotalDuration(); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 952 | if (node.mEndTime == DURATION_INFINITE) { |
| 953 | maxEndTime = DURATION_INFINITE; |
| 954 | break; |
| 955 | } else { |
| 956 | maxEndTime = node.mEndTime > maxEndTime ? node.mEndTime : maxEndTime; |
| 957 | } |
| 958 | } |
| 959 | mTotalDuration = maxEndTime; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * Based on parent's start/end time, calculate children's start/end time. If cycle exists in |
| 964 | * the graph, all the nodes on the cycle will be marked to start at {@link #DURATION_INFINITE}, |
| 965 | * meaning they will ever play. |
| 966 | */ |
| 967 | private void updatePlayTime(Node parent, ArrayList<Node> visited) { |
| 968 | if (parent.mChildNodes == null) { |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 969 | if (parent == mRootNode) { |
| 970 | // All the animators are in a cycle |
| 971 | for (int i = 0; i < mNodes.size(); i++) { |
| 972 | Node node = mNodes.get(i); |
| 973 | if (node != mRootNode) { |
| 974 | node.mStartTime = DURATION_INFINITE; |
| 975 | node.mEndTime = DURATION_INFINITE; |
| 976 | } |
| 977 | } |
| 978 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
| 982 | visited.add(parent); |
| 983 | int childrenSize = parent.mChildNodes.size(); |
| 984 | for (int i = 0; i < childrenSize; i++) { |
| 985 | Node child = parent.mChildNodes.get(i); |
| 986 | int index = visited.indexOf(child); |
| 987 | if (index >= 0) { |
| 988 | // Child has been visited, cycle found. Mark all the nodes in the cycle. |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 989 | for (int j = index; j < visited.size(); j++) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 990 | visited.get(j).mLatestParent = null; |
| 991 | visited.get(j).mStartTime = DURATION_INFINITE; |
| 992 | visited.get(j).mEndTime = DURATION_INFINITE; |
| 993 | } |
| 994 | child.mStartTime = DURATION_INFINITE; |
| 995 | child.mEndTime = DURATION_INFINITE; |
| 996 | child.mLatestParent = null; |
| 997 | Log.w(TAG, "Cycle found in AnimatorSet: " + this); |
| 998 | continue; |
| 999 | } |
| 1000 | |
| 1001 | if (child.mStartTime != DURATION_INFINITE) { |
| 1002 | if (parent.mEndTime == DURATION_INFINITE) { |
| 1003 | child.mLatestParent = parent; |
| 1004 | child.mStartTime = DURATION_INFINITE; |
| 1005 | child.mEndTime = DURATION_INFINITE; |
| 1006 | } else { |
| 1007 | if (parent.mEndTime >= child.mStartTime) { |
| 1008 | child.mLatestParent = parent; |
| 1009 | child.mStartTime = parent.mEndTime; |
| 1010 | } |
| 1011 | |
| 1012 | long duration = child.mAnimation.getTotalDuration(); |
| 1013 | child.mEndTime = duration == DURATION_INFINITE ? |
| 1014 | DURATION_INFINITE : child.mStartTime + duration; |
| 1015 | } |
| 1016 | } |
| 1017 | updatePlayTime(child, visited); |
| 1018 | } |
| 1019 | visited.remove(parent); |
| 1020 | } |
| 1021 | |
| 1022 | // Recursively find all the siblings |
| 1023 | private void findSiblings(Node node, ArrayList<Node> siblings) { |
| 1024 | if (!siblings.contains(node)) { |
| 1025 | siblings.add(node); |
| 1026 | if (node.mSiblings == null) { |
| 1027 | return; |
| 1028 | } |
| 1029 | for (int i = 0; i < node.mSiblings.size(); i++) { |
| 1030 | findSiblings(node.mSiblings.get(i), siblings); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 1035 | /** |
| 1036 | * @hide |
| 1037 | * TODO: For animatorSet defined in XML, we can use a flag to indicate what the play order |
| 1038 | * if defined (i.e. sequential or together), then we can use the flag instead of calculate |
| 1039 | * dynamically. |
| 1040 | * @return whether all the animators in the set are supposed to play together |
| 1041 | */ |
| 1042 | public boolean shouldPlayTogether() { |
| 1043 | updateAnimatorsDuration(); |
| 1044 | createDependencyGraph(); |
| 1045 | // All the child nodes are set out to play right after the delay animation |
| 1046 | return mRootNode.mChildNodes.size() == mNodes.size() - 1; |
| 1047 | } |
| 1048 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1049 | @Override |
| 1050 | public long getTotalDuration() { |
| 1051 | updateAnimatorsDuration(); |
| 1052 | createDependencyGraph(); |
| 1053 | return mTotalDuration; |
| 1054 | } |
| 1055 | |
| 1056 | private Node getNodeForAnimation(Animator anim) { |
| 1057 | Node node = mNodeMap.get(anim); |
| 1058 | if (node == null) { |
| 1059 | node = new Node(anim); |
| 1060 | mNodeMap.put(anim, node); |
| 1061 | mNodes.add(node); |
| 1062 | } |
| 1063 | return node; |
| 1064 | } |
| 1065 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1066 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1067 | * A Node is an embodiment of both the Animator that it wraps as well as |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1068 | * any dependencies that are associated with that Animation. This includes |
| 1069 | * both dependencies upon other nodes (in the dependencies list) as |
| 1070 | * well as dependencies of other nodes upon this (in the nodeDependents list). |
| 1071 | */ |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 1072 | private static class Node implements Cloneable { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1073 | Animator mAnimation; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1074 | |
| 1075 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1076 | * Child nodes are the nodes associated with animations that will be played immediately |
| 1077 | * after current node. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1078 | */ |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1079 | ArrayList<Node> mChildNodes = null; |
Chet Haase | 1e0ac5a | 2010-08-27 08:32:11 -0700 | [diff] [blame] | 1080 | |
| 1081 | /** |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 1082 | * Temporary field to hold the clone in AnimatorSet#clone. Cleaned after clone is complete |
| 1083 | */ |
| 1084 | private Node mTmpClone = null; |
| 1085 | |
| 1086 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1087 | * Flag indicating whether the animation in this node is finished. This flag |
| 1088 | * is used by AnimatorSet to check, as each animation ends, whether all child animations |
| 1089 | * are mEnded and it's time to send out an end event for the entire AnimatorSet. |
| 1090 | */ |
| 1091 | boolean mEnded = false; |
| 1092 | |
| 1093 | /** |
| 1094 | * Nodes with animations that are defined to play simultaneously with the animation |
| 1095 | * associated with this current node. |
| 1096 | */ |
| 1097 | ArrayList<Node> mSiblings; |
| 1098 | |
| 1099 | /** |
| 1100 | * Parent nodes are the nodes with animations preceding current node's animation. Parent |
| 1101 | * nodes here are derived from user defined animation sequence. |
| 1102 | */ |
| 1103 | ArrayList<Node> mParents; |
| 1104 | |
| 1105 | /** |
| 1106 | * Latest parent is the parent node associated with a animation that finishes after all |
| 1107 | * the other parents' animations. |
| 1108 | */ |
| 1109 | Node mLatestParent = null; |
| 1110 | |
| 1111 | boolean mParentsAdded = false; |
| 1112 | long mStartTime = 0; |
| 1113 | long mEndTime = 0; |
Doris Liu | 49db424 | 2015-08-10 13:33:06 -0700 | [diff] [blame] | 1114 | long mTotalDuration = 0; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1115 | |
| 1116 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1117 | * Constructs the Node with the animation that it encapsulates. A Node has no |
| 1118 | * dependencies by default; dependencies are added via the addDependency() |
| 1119 | * method. |
| 1120 | * |
| 1121 | * @param animation The animation that the Node encapsulates. |
| 1122 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1123 | public Node(Animator animation) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1124 | this.mAnimation = animation; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1125 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 1126 | |
| 1127 | @Override |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 1128 | public Node clone() { |
| 1129 | try { |
| 1130 | Node node = (Node) super.clone(); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1131 | node.mAnimation = mAnimation.clone(); |
| 1132 | if (mChildNodes != null) { |
| 1133 | node.mChildNodes = new ArrayList<>(mChildNodes); |
| 1134 | } |
Doris Liu | 5e66616 | 2015-08-03 16:06:56 -0700 | [diff] [blame] | 1135 | if (mSiblings != null) { |
| 1136 | node.mSiblings = new ArrayList<>(mSiblings); |
| 1137 | } |
| 1138 | if (mParents != null) { |
| 1139 | node.mParents = new ArrayList<>(mParents); |
| 1140 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1141 | node.mEnded = false; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 1142 | return node; |
| 1143 | } catch (CloneNotSupportedException e) { |
| 1144 | throw new AssertionError(); |
| 1145 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 1146 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1147 | |
| 1148 | void addChild(Node node) { |
| 1149 | if (mChildNodes == null) { |
| 1150 | mChildNodes = new ArrayList<>(); |
| 1151 | } |
| 1152 | if (!mChildNodes.contains(node)) { |
| 1153 | mChildNodes.add(node); |
| 1154 | node.addParent(this); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | public void addSibling(Node node) { |
| 1159 | if (mSiblings == null) { |
| 1160 | mSiblings = new ArrayList<Node>(); |
| 1161 | } |
| 1162 | if (!mSiblings.contains(node)) { |
| 1163 | mSiblings.add(node); |
| 1164 | node.addSibling(this); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | public void addParent(Node node) { |
| 1169 | if (mParents == null) { |
| 1170 | mParents = new ArrayList<Node>(); |
| 1171 | } |
| 1172 | if (!mParents.contains(node)) { |
| 1173 | mParents.add(node); |
| 1174 | node.addChild(this); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | public void addParents(ArrayList<Node> parents) { |
| 1179 | if (parents == null) { |
| 1180 | return; |
| 1181 | } |
| 1182 | int size = parents.size(); |
| 1183 | for (int i = 0; i < size; i++) { |
| 1184 | addParent(parents.get(i)); |
| 1185 | } |
| 1186 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * The <code>Builder</code> object is a utility class to facilitate adding animations to a |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1191 | * <code>AnimatorSet</code> along with the relationships between the various animations. The |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1192 | * intention of the <code>Builder</code> methods, along with the {@link |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1193 | * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible |
| 1194 | * to express the dependency relationships of animations in a natural way. Developers can also |
| 1195 | * use the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1196 | * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need, |
| 1197 | * but it might be easier in some situations to express the AnimatorSet of animations in pairs. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1198 | * <p/> |
| 1199 | * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1200 | * internally via a call to {@link AnimatorSet#play(Animator)}.</p> |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1201 | * <p/> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1202 | * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1203 | * play when anim2 finishes, and anim4 to play when anim3 finishes:</p> |
| 1204 | * <pre> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1205 | * AnimatorSet s = new AnimatorSet(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1206 | * s.play(anim1).with(anim2); |
| 1207 | * s.play(anim2).before(anim3); |
| 1208 | * s.play(anim4).after(anim3); |
| 1209 | * </pre> |
| 1210 | * <p/> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1211 | * <p>Note in the example that both {@link Builder#before(Animator)} and {@link |
| 1212 | * Builder#after(Animator)} are used. These are just different ways of expressing the same |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1213 | * relationship and are provided to make it easier to say things in a way that is more natural, |
| 1214 | * depending on the situation.</p> |
| 1215 | * <p/> |
| 1216 | * <p>It is possible to make several calls into the same <code>Builder</code> object to express |
| 1217 | * multiple relationships. However, note that it is only the animation passed into the initial |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1218 | * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1219 | * calls to the <code>Builder</code> object. For example, the following code starts both anim2 |
| 1220 | * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and |
| 1221 | * anim3: |
| 1222 | * <pre> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1223 | * AnimatorSet s = new AnimatorSet(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1224 | * s.play(anim1).before(anim2).before(anim3); |
| 1225 | * </pre> |
| 1226 | * If the desired result is to play anim1 then anim2 then anim3, this code expresses the |
| 1227 | * relationship correctly:</p> |
| 1228 | * <pre> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1229 | * AnimatorSet s = new AnimatorSet(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1230 | * s.play(anim1).before(anim2); |
| 1231 | * s.play(anim2).before(anim3); |
| 1232 | * </pre> |
| 1233 | * <p/> |
| 1234 | * <p>Note that it is possible to express relationships that cannot be resolved and will not |
| 1235 | * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no |
| 1236 | * sense. In general, circular dependencies like this one (or more indirect ones where a depends |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1237 | * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets |
| 1238 | * that can boil down to a simple, one-way relationship of animations starting with, before, and |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1239 | * after other, different, animations.</p> |
| 1240 | */ |
| 1241 | public class Builder { |
| 1242 | |
| 1243 | /** |
| 1244 | * This tracks the current node being processed. It is supplied to the play() method |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1245 | * of AnimatorSet and passed into the constructor of Builder. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1246 | */ |
| 1247 | private Node mCurrentNode; |
| 1248 | |
| 1249 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1250 | * package-private constructor. Builders are only constructed by AnimatorSet, when the |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1251 | * play() method is called. |
| 1252 | * |
| 1253 | * @param anim The animation that is the dependency for the other animations passed into |
| 1254 | * the other methods of this Builder object. |
| 1255 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1256 | Builder(Animator anim) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1257 | mDependencyDirty = true; |
| 1258 | mCurrentNode = getNodeForAnimation(anim); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * Sets up the given animation to play at the same time as the animation supplied in the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1263 | * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1264 | * |
| 1265 | * @param anim The animation that will play when the animation supplied to the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1266 | * {@link AnimatorSet#play(Animator)} method starts. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1267 | */ |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1268 | public Builder with(Animator anim) { |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1269 | Node node = getNodeForAnimation(anim); |
| 1270 | mCurrentNode.addSibling(node); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1271 | return this; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * Sets up the given animation to play when the animation supplied in the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1276 | * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1277 | * ends. |
| 1278 | * |
| 1279 | * @param anim The animation that will play when the animation supplied to the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1280 | * {@link AnimatorSet#play(Animator)} method ends. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1281 | */ |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1282 | public Builder before(Animator anim) { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1283 | mReversible = false; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1284 | Node node = getNodeForAnimation(anim); |
| 1285 | mCurrentNode.addChild(node); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1286 | return this; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * Sets up the given animation to play when the animation supplied in the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1291 | * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1292 | * to start when the animation supplied in this method call ends. |
| 1293 | * |
| 1294 | * @param anim The animation whose end will cause the animation supplied to the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1295 | * {@link AnimatorSet#play(Animator)} method to play. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1296 | */ |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1297 | public Builder after(Animator anim) { |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1298 | mReversible = false; |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 1299 | Node node = getNodeForAnimation(anim); |
| 1300 | mCurrentNode.addParent(node); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1301 | return this; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Sets up the animation supplied in the |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1306 | * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1307 | * to play when the given amount of time elapses. |
| 1308 | * |
| 1309 | * @param delay The number of milliseconds that should elapse before the |
| 1310 | * animation starts. |
| 1311 | */ |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1312 | public Builder after(long delay) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1313 | // setup dummy ValueAnimator just to run the clock |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 1314 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
| 1315 | anim.setDuration(delay); |
| 1316 | after(anim); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1317 | return this; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | } |
| 1321 | |
| 1322 | } |