Skip to content

Commit 576b39e

Browse files
committed
[Internal] Use AnimatorSet#playTogether without workarounds on API 23+
1 parent 79bd7d7 commit 576b39e

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

lib/java/com/google/android/material/animation/AnimatorSetCompat.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
import android.animation.Animator;
1919
import android.animation.AnimatorSet;
2020
import android.animation.ValueAnimator;
21+
import android.os.Build;
2122
import androidx.annotation.NonNull;
23+
import androidx.annotation.RequiresApi;
2224
import androidx.annotation.RestrictTo;
2325
import androidx.annotation.RestrictTo.Scope;
26+
27+
import java.util.ArrayList;
28+
import java.util.Collection;
2429
import java.util.List;
2530

2631
/**
@@ -33,17 +38,35 @@ public class AnimatorSetCompat {
3338

3439
/** Sets up this AnimatorSet to play all of the supplied animations at the same time. */
3540
public static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull List<Animator> items) {
36-
// Fix for pre-M bug where animators with start delay are not played correctly in an
37-
// AnimatorSet.
38-
long totalDuration = 0;
39-
for (int i = 0, count = items.size(); i < count; i++) {
40-
Animator animator = items.get(i);
41-
totalDuration = Math.max(totalDuration, animator.getStartDelay() + animator.getDuration());
41+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
42+
Api23Impl.playTogether(animatorSet, items);
43+
} else {
44+
Api21Impl.playTogether(animatorSet, items);
4245
}
43-
Animator fix = ValueAnimator.ofInt(0, 0);
44-
fix.setDuration(totalDuration);
45-
items.add(0, fix);
46+
}
4647

47-
animatorSet.playTogether(items);
48+
@RequiresApi(Build.VERSION_CODES.M)
49+
static class Api23Impl {
50+
static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull Collection<Animator> items) {
51+
animatorSet.playTogether(items);
52+
}
53+
}
54+
55+
static class Api21Impl {
56+
static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull Collection<Animator> items) {
57+
// Fix for pre-M bug where animators with start delay are not played correctly in an
58+
// AnimatorSet.
59+
long totalDuration = 0;
60+
for (Animator animator : items) {
61+
totalDuration = Math.max(totalDuration, animator.getStartDelay() + animator.getDuration());
62+
}
63+
Animator fix = ValueAnimator.ofInt(0, 0);
64+
fix.setDuration(totalDuration);
65+
66+
List<Animator> animators = new ArrayList<>(items.size() + 1);
67+
animators.add(fix);
68+
animators.addAll(items);
69+
animatorSet.playTogether(animators);
70+
}
4871
}
4972
}

0 commit comments

Comments
 (0)