Skip to content

Commit

Permalink
[TimePicker] Prevent the accumulation of listeners in rotationAnimator
Browse files Browse the repository at this point in the history
Resolves #3973

GIT_ORIGIN_REV_ID=1b09e58865abfdf91610da16919e7f4f28739c09
PiperOrigin-RevId: 634850525
  • Loading branch information
pubiqq authored and leticiarossi committed May 17, 2024
1 parent 26bfdd2 commit 182a507
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions lib/java/com/google/android/material/timepicker/ClockHandView.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import android.view.ViewConfiguration;
import androidx.annotation.Dimension;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import com.google.android.material.animation.AnimationUtils;
Expand All @@ -56,7 +57,7 @@ class ClockHandView extends View {
private static final int DEFAULT_ANIMATION_DURATION = 200;
private final int animationDuration;
private final TimeInterpolator animationInterpolator;
private final ValueAnimator rotationAnimator = new ValueAnimator();
@NonNull private final ValueAnimator rotationAnimator = new ValueAnimator();
private boolean animatingOnTouchUp;
private float downX;
private float downY;
Expand Down Expand Up @@ -133,6 +134,23 @@ public ClockHandView(Context context, @Nullable AttributeSet attrs, int defStyle
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
a.recycle();

initRotationAnimator();
}

private void initRotationAnimator() {
rotationAnimator.addUpdateListener(
animation -> {
float animatedValue = (float) animation.getAnimatedValue();
setHandRotationInternal(animatedValue, true);
});

rotationAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
animation.end();
}
});
}

@Override
Expand All @@ -149,9 +167,7 @@ public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees) {
}

public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees, boolean animate) {
if (rotationAnimator != null) {
rotationAnimator.cancel();
}
rotationAnimator.cancel();

if (!animate) {
setHandRotationInternal(degrees, false);
Expand All @@ -162,19 +178,6 @@ public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees, boo
rotationAnimator.setFloatValues(animationValues.first, animationValues.second);
rotationAnimator.setDuration(animationDuration);
rotationAnimator.setInterpolator(animationInterpolator);
rotationAnimator.addUpdateListener(
animation -> {
float animatedValue = (float) animation.getAnimatedValue();
setHandRotationInternal(animatedValue, true);
});

rotationAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
animation.end();
}
});

rotationAnimator.start();
}

Expand Down

0 comments on commit 182a507

Please sign in to comment.