From 576b39e0e0f3c62c0e87ebbb677291792869eff9 Mon Sep 17 00:00:00 2001 From: pubiqq Date: Wed, 12 Feb 2025 22:55:40 +0300 Subject: [PATCH] [Internal] Use AnimatorSet#playTogether without workarounds on API 23+ --- .../material/animation/AnimatorSetCompat.java | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/java/com/google/android/material/animation/AnimatorSetCompat.java b/lib/java/com/google/android/material/animation/AnimatorSetCompat.java index 11f98a99527..c2ca4daf6e6 100644 --- a/lib/java/com/google/android/material/animation/AnimatorSetCompat.java +++ b/lib/java/com/google/android/material/animation/AnimatorSetCompat.java @@ -18,9 +18,14 @@ import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ValueAnimator; +import android.os.Build; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.annotation.RestrictTo; import androidx.annotation.RestrictTo.Scope; + +import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -33,17 +38,35 @@ public class AnimatorSetCompat { /** Sets up this AnimatorSet to play all of the supplied animations at the same time. */ public static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull List items) { - // Fix for pre-M bug where animators with start delay are not played correctly in an - // AnimatorSet. - long totalDuration = 0; - for (int i = 0, count = items.size(); i < count; i++) { - Animator animator = items.get(i); - totalDuration = Math.max(totalDuration, animator.getStartDelay() + animator.getDuration()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Api23Impl.playTogether(animatorSet, items); + } else { + Api21Impl.playTogether(animatorSet, items); } - Animator fix = ValueAnimator.ofInt(0, 0); - fix.setDuration(totalDuration); - items.add(0, fix); + } - animatorSet.playTogether(items); + @RequiresApi(Build.VERSION_CODES.M) + static class Api23Impl { + static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull Collection items) { + animatorSet.playTogether(items); + } + } + + static class Api21Impl { + static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull Collection items) { + // Fix for pre-M bug where animators with start delay are not played correctly in an + // AnimatorSet. + long totalDuration = 0; + for (Animator animator : items) { + totalDuration = Math.max(totalDuration, animator.getStartDelay() + animator.getDuration()); + } + Animator fix = ValueAnimator.ofInt(0, 0); + fix.setDuration(totalDuration); + + List animators = new ArrayList<>(items.size() + 1); + animators.add(fix); + animators.addAll(items); + animatorSet.playTogether(animators); + } } }