1212import android .graphics .Rect ;
1313import android .graphics .RectF ;
1414import android .graphics .Shader ;
15- import android .graphics .drawable .Animatable ;
1615import android .graphics .drawable .Drawable ;
1716import android .support .annotation .NonNull ;
1817import android .support .annotation .Nullable ;
1918
20- import com .github .nikartm .support .constant .Constants ;
2119
2220/**
2321 * @author Ivan V on 30.03.2018.
2422 * @version 1.0
2523 */
26- public class AnimatedStripedDrawable extends Drawable implements Animatable {
24+ public class AnimatedStripedDrawable extends Drawable {
2725
2826 private float stripeWidth ;
2927 private int colorBack ;
3028 private int colorMain ;
3129 private int colorSub ;
3230 private float alpha ;
3331 private float cornerRadius ;
34- private int duration ;
32+ private int stripeDuration ;
3533 private float tilt ;
3634 private boolean stripeRevert ;
3735 private boolean showStripes ;
@@ -59,6 +57,7 @@ protected void onBoundsChange(Rect bounds) {
5957 viewHeight = bounds .height ();
6058 viewWidth = bounds .width ();
6159 adjustStripes ();
60+ initAnimator ();
6261 }
6362
6463 private void adjustStripes () {
@@ -72,6 +71,21 @@ private void adjustStripes() {
7271 }
7372 }
7473
74+ private void initAnimator () {
75+ if (animator == null ) {
76+ animator = ValueAnimator .ofInt (0 , 1 );
77+ animator .setRepeatCount (ValueAnimator .INFINITE );
78+ animator .setDuration (stripeDuration );
79+ animator .addListener (new AnimatorListenerAdapter () {
80+ @ Override
81+ public void onAnimationRepeat (Animator animation ) {
82+ shiftColor (colorMain , colorSub );
83+ invalidateSelf ();
84+ }
85+ });
86+ }
87+ }
88+
7589 @ Override
7690 public int getIntrinsicWidth () {
7791 return viewWidth > 0 ? viewWidth : super .getIntrinsicWidth ();
@@ -96,8 +110,14 @@ public int getOpacity() {
96110 @ Override
97111 public void draw (@ NonNull Canvas canvas ) {
98112 drawStripes (canvas );
99- if (!running ) {
100- startAnimation ();
113+ startStripesAnimation ();
114+ }
115+
116+ private void startStripesAnimation () {
117+ if (running ) {
118+ start ();
119+ } else {
120+ setShowStripes (false );
101121 }
102122 }
103123
@@ -137,29 +157,13 @@ private LinearGradient createGradientShader() {
137157 colorMain , colorSub , Shader .TileMode .REPEAT );
138158 }
139159
140- private void startAnimation () {
141- if (animator == null && !running ) {
142- animator = ValueAnimator .ofInt (0 , 1 );
143- animator .setRepeatCount (ValueAnimator .INFINITE );
144- animator .setDuration (duration );
145- animator .addListener (new AnimatorListenerAdapter () {
146- @ Override
147- public void onAnimationRepeat (Animator animation ) {
148- shiftColor (colorMain , colorSub );
149- invalidateSelf ();
150- }
151- });
152- start ();
153- }
154- }
155-
156160 private void shiftColor (int mainColor , int subColor ) {
157161 colorMain = subColor ;
158162 colorSub = mainColor ;
159163 }
160164
161- @ Override
162- public void start () {
165+ // Start stripes animation
166+ protected void start () {
163167 if (isRunning ()) {
164168 return ;
165169 }
@@ -169,8 +173,8 @@ public void start() {
169173 invalidateSelf ();
170174 }
171175
172- @ Override
173- public void stop () {
176+ // Stop stripes animation
177+ protected void stop () {
174178 if (!isRunning ()) {
175179 return ;
176180 }
@@ -180,9 +184,9 @@ public void stop() {
180184 invalidateSelf ();
181185 }
182186
183- @ Override
184- public boolean isRunning () {
185- return running ;
187+ // Check if stripes animation running
188+ protected boolean isRunning () {
189+ return animator != null && animator . isStarted () ;
186190 }
187191
188192 public float getStripeWidth () {
@@ -245,12 +249,12 @@ public AnimatedStripedDrawable setCornerRadius(float cornerRadius) {
245249 return this ;
246250 }
247251
248- public int getDuration () {
249- return duration ;
252+ public int getStripeDuration () {
253+ return stripeDuration ;
250254 }
251255
252- public AnimatedStripedDrawable setDuration (int duration ) {
253- this .duration = duration ;
256+ public AnimatedStripedDrawable setStripeDuration (int stripeDuration ) {
257+ this .stripeDuration = stripeDuration ;
254258 invalidateSelf ();
255259 return this ;
256260 }
0 commit comments