Skip to content

Commit 182a507

Browse files
pubiqqleticiarossi
authored andcommitted
[TimePicker] Prevent the accumulation of listeners in rotationAnimator
Resolves #3973 GIT_ORIGIN_REV_ID=1b09e58865abfdf91610da16919e7f4f28739c09 PiperOrigin-RevId: 634850525
1 parent 26bfdd2 commit 182a507

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

lib/java/com/google/android/material/timepicker/ClockHandView.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import android.view.ViewConfiguration;
4141
import androidx.annotation.Dimension;
4242
import androidx.annotation.FloatRange;
43+
import androidx.annotation.NonNull;
4344
import androidx.annotation.Nullable;
4445
import androidx.annotation.Px;
4546
import com.google.android.material.animation.AnimationUtils;
@@ -56,7 +57,7 @@ class ClockHandView extends View {
5657
private static final int DEFAULT_ANIMATION_DURATION = 200;
5758
private final int animationDuration;
5859
private final TimeInterpolator animationInterpolator;
59-
private final ValueAnimator rotationAnimator = new ValueAnimator();
60+
@NonNull private final ValueAnimator rotationAnimator = new ValueAnimator();
6061
private boolean animatingOnTouchUp;
6162
private float downX;
6263
private float downY;
@@ -133,6 +134,23 @@ public ClockHandView(Context context, @Nullable AttributeSet attrs, int defStyle
133134
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
134135
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
135136
a.recycle();
137+
138+
initRotationAnimator();
139+
}
140+
141+
private void initRotationAnimator() {
142+
rotationAnimator.addUpdateListener(
143+
animation -> {
144+
float animatedValue = (float) animation.getAnimatedValue();
145+
setHandRotationInternal(animatedValue, true);
146+
});
147+
148+
rotationAnimator.addListener(new AnimatorListenerAdapter() {
149+
@Override
150+
public void onAnimationCancel(Animator animation) {
151+
animation.end();
152+
}
153+
});
136154
}
137155

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

151169
public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees, boolean animate) {
152-
if (rotationAnimator != null) {
153-
rotationAnimator.cancel();
154-
}
170+
rotationAnimator.cancel();
155171

156172
if (!animate) {
157173
setHandRotationInternal(degrees, false);
@@ -162,19 +178,6 @@ public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees, boo
162178
rotationAnimator.setFloatValues(animationValues.first, animationValues.second);
163179
rotationAnimator.setDuration(animationDuration);
164180
rotationAnimator.setInterpolator(animationInterpolator);
165-
rotationAnimator.addUpdateListener(
166-
animation -> {
167-
float animatedValue = (float) animation.getAnimatedValue();
168-
setHandRotationInternal(animatedValue, true);
169-
});
170-
171-
rotationAnimator.addListener(new AnimatorListenerAdapter() {
172-
@Override
173-
public void onAnimationCancel(Animator animation) {
174-
animation.end();
175-
}
176-
});
177-
178181
rotationAnimator.start();
179182
}
180183

0 commit comments

Comments
 (0)