2525 */
2626public class AnimatedStripedDrawable extends Drawable implements Animatable {
2727
28- private float stripeWidth = Constants .STRIPE_WIDTH ;
29- private int colorBack = Constants .DEF_BACKGROUND ;
30- private int colorMain = Constants .DEF_MAIN_STRIPE ;
31- private int colorSub = Constants .DEF_SUB_STRIPE ;
32- private float alpha = Constants .STRIPE_ALPHA ;
33- private float cornerRadius = Constants .CORNER ;
34- private int duration = Constants .DURATION ;
35- private float tilt = Constants .STRIPE_TILT ;
36- private boolean stripeRevert = Constants .REVERT ;
37- private boolean showStripes = Constants .SHOW_STRIPES ;
28+ private float stripeWidth ;
29+ private int colorBack ;
30+ private int colorMain ;
31+ private int colorSub ;
32+ private float alpha ;
33+ private float cornerRadius ;
34+ private int duration ;
35+ private float tilt ;
36+ private boolean stripeRevert ;
37+ private boolean showStripes ;
38+ private boolean stripeGradient ;
3839
3940 private float density ;
4041 private int viewHeight ;
@@ -45,6 +46,7 @@ public class AnimatedStripedDrawable extends Drawable implements Animatable {
4546
4647 private Context context ;
4748 private ValueAnimator animator ;
49+ private Shader stripesShader ;
4850
4951 public AnimatedStripedDrawable (Context context ) {
5052 this .context = context ;
@@ -90,11 +92,11 @@ protected void onBoundsChange(Rect bounds) {
9092 density = context .getResources ().getDisplayMetrics ().density ;
9193 viewHeight = bounds .height ();
9294 viewWidth = bounds .width ();
93- stripeWidthToDp ();
95+ adjustStripes ();
9496 defineTilt ();
9597 }
9698
97- private void stripeWidthToDp () {
99+ private void adjustStripes () {
98100 stripeWidth = stripeWidth * density ;
99101 }
100102
@@ -139,25 +141,38 @@ public void draw(@NonNull Canvas canvas) {
139141
140142 private void drawStripes (Canvas canvas ) {
141143 final Paint paintBack = new Paint (Paint .ANTI_ALIAS_FLAG );
142- final Paint paintCorner = new Paint (Paint .ANTI_ALIAS_FLAG );
144+ final Paint paintStripes = new Paint (Paint .ANTI_ALIAS_FLAG );
143145 final Rect rect = new Rect (0 , 0 , viewWidth , viewHeight );
144146 final RectF rectF = new RectF (rect );
147+ final int stripesAlpha = Util .computeAlpha (alpha );
148+
149+ if (stripeGradient ) {
150+ stripesShader = createGradientShader ();
151+ } else {
152+ stripesShader = createShader ();
153+ }
145154
146155 paintBack .setColor (colorBack );
147156 canvas .drawRoundRect (rectF , cornerRadius , cornerRadius , paintBack );
148157
149158 if (showStripes ) {
150- Shader shader = createShader ();
151- paintCorner .setShader (shader );
152- paintCorner .setAlpha (Util .computeAlpha (alpha ));
153- canvas .drawRoundRect (rectF , cornerRadius , cornerRadius , paintCorner );
159+ paintStripes .setAlpha (stripesAlpha );
160+ paintStripes .setShader (stripesShader );
161+ canvas .drawRoundRect (rectF , cornerRadius , cornerRadius , paintStripes );
154162 }
155163 }
156164
157- private Shader createShader () {
165+ @ NonNull
166+ private LinearGradient createShader () {
167+ return new LinearGradient (stripeWidth , tiltLeft , 0 , tiltRight ,
168+ new int [] { colorMain , colorSub }, new float [] { .5f , .5f },
169+ Shader .TileMode .REPEAT );
170+ }
171+
172+ @ NonNull
173+ private LinearGradient createGradientShader () {
158174 return new LinearGradient (stripeWidth , tiltLeft , 0 , tiltRight ,
159- new int [] { colorMain , colorSub },
160- new float [] { 0.5f , 0.5f }, Shader .TileMode .REPEAT );
175+ colorMain , colorSub , Shader .TileMode .REPEAT );
161176 }
162177
163178 private void startAnimation () {
@@ -188,6 +203,7 @@ public void start() {
188203 }
189204 running = true ;
190205 animator .start ();
206+ setShowStripes (true );
191207 invalidateSelf ();
192208 }
193209
@@ -198,6 +214,7 @@ public void stop() {
198214 }
199215 running = false ;
200216 animator .cancel ();
217+ setShowStripes (false );
201218 invalidateSelf ();
202219 }
203220
@@ -305,4 +322,14 @@ public AnimatedStripedDrawable setShowStripes(boolean showStripes) {
305322 invalidateSelf ();
306323 return this ;
307324 }
325+
326+ public boolean isStripeGradient () {
327+ return stripeGradient ;
328+ }
329+
330+ public AnimatedStripedDrawable setStripeGradient (boolean stripeGradient ) {
331+ this .stripeGradient = stripeGradient ;
332+ invalidateSelf ();
333+ return this ;
334+ }
308335}
0 commit comments