40
40
import android .view .ViewConfiguration ;
41
41
import androidx .annotation .Dimension ;
42
42
import androidx .annotation .FloatRange ;
43
+ import androidx .annotation .NonNull ;
43
44
import androidx .annotation .Nullable ;
44
45
import androidx .annotation .Px ;
45
46
import com .google .android .material .animation .AnimationUtils ;
@@ -56,7 +57,7 @@ class ClockHandView extends View {
56
57
private static final int DEFAULT_ANIMATION_DURATION = 200 ;
57
58
private final int animationDuration ;
58
59
private final TimeInterpolator animationInterpolator ;
59
- private final ValueAnimator rotationAnimator = new ValueAnimator ();
60
+ @ NonNull private final ValueAnimator rotationAnimator = new ValueAnimator ();
60
61
private boolean animatingOnTouchUp ;
61
62
private float downX ;
62
63
private float downY ;
@@ -133,6 +134,23 @@ public ClockHandView(Context context, @Nullable AttributeSet attrs, int defStyle
133
134
scaledTouchSlop = ViewConfiguration .get (context ).getScaledTouchSlop ();
134
135
setImportantForAccessibility (View .IMPORTANT_FOR_ACCESSIBILITY_NO );
135
136
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
+ });
136
154
}
137
155
138
156
@ Override
@@ -149,9 +167,7 @@ public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees) {
149
167
}
150
168
151
169
public void setHandRotation (@ FloatRange (from = 0f , to = 360f ) float degrees , boolean animate ) {
152
- if (rotationAnimator != null ) {
153
- rotationAnimator .cancel ();
154
- }
170
+ rotationAnimator .cancel ();
155
171
156
172
if (!animate ) {
157
173
setHandRotationInternal (degrees , false );
@@ -162,19 +178,6 @@ public void setHandRotation(@FloatRange(from = 0f, to = 360f) float degrees, boo
162
178
rotationAnimator .setFloatValues (animationValues .first , animationValues .second );
163
179
rotationAnimator .setDuration (animationDuration );
164
180
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
-
178
181
rotationAnimator .start ();
179
182
}
180
183
0 commit comments