Skip to content

[Internal] Use AnimatorSet#playTogether without workarounds on API 23+ #4601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Animator> 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<Animator> items) {
animatorSet.playTogether(items);
}
}

static class Api21Impl {
static void playTogether(@NonNull AnimatorSet animatorSet, @NonNull Collection<Animator> 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<Animator> animators = new ArrayList<>(items.size() + 1);
animators.add(fix);
animators.addAll(items);
animatorSet.playTogether(animators);
}
}
}