18
18
import android .animation .Animator ;
19
19
import android .animation .AnimatorSet ;
20
20
import android .animation .ValueAnimator ;
21
+ import android .os .Build ;
21
22
import androidx .annotation .NonNull ;
23
+ import androidx .annotation .RequiresApi ;
22
24
import androidx .annotation .RestrictTo ;
23
25
import androidx .annotation .RestrictTo .Scope ;
26
+
27
+ import java .util .ArrayList ;
28
+ import java .util .Collection ;
24
29
import java .util .List ;
25
30
26
31
/**
@@ -33,17 +38,35 @@ public class AnimatorSetCompat {
33
38
34
39
/** Sets up this AnimatorSet to play all of the supplied animations at the same time. */
35
40
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 );
42
45
}
43
- Animator fix = ValueAnimator .ofInt (0 , 0 );
44
- fix .setDuration (totalDuration );
45
- items .add (0 , fix );
46
+ }
46
47
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
+ }
48
71
}
49
72
}
0 commit comments