4040import android .view .ViewConfiguration ;
4141import androidx .annotation .Dimension ;
4242import androidx .annotation .FloatRange ;
43+ import androidx .annotation .NonNull ;
4344import androidx .annotation .Nullable ;
4445import androidx .annotation .Px ;
4546import 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