Skip to content

Commit dccc4ce

Browse files
committed
Fixed striped button,
Fixed animated drawable
1 parent afcf8a0 commit dccc4ce

7 files changed

Lines changed: 69 additions & 207 deletions

File tree

app/src/main/java/com/github/nikartm/strippedprogressbutton/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.nikartm.strippedprogressbutton;
22

3-
import android.support.v7.app.AppCompatActivity;
43
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
55
import android.widget.Button;
66

77
import com.github.nikartm.support.StripedProgressButton;

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
android:layout_marginEnd="16dp"
2525
android:layout_marginStart="16dp"
2626
android:layout_marginTop="16dp"
27-
android:background="@drawable/btn_rounded"
2827
android:text="Example"
2928
android:textColor="#ffffff"
30-
app:spb_active="true"
29+
app:spb_cornerRadius="50"
3130
app:spb_duration="250" />
3231

3332
<Button

support/src/main/java/com/github/nikartm/support/AnimatedStripedDrawable.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@
2525
*/
2626
public 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
}

support/src/main/java/com/github/nikartm/support/AttributeController.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
import android.util.AttributeSet;
66

77
import com.github.nikartm.support.constant.Constants;
8-
import com.github.nikartm.support.model.StrippedButton;
98

109
/**
1110
* @author Ivan V on 29.03.2018.
1211
* @version 1.0
1312
*/
1413
public class AttributeController {
1514

16-
private StrippedButton button;
15+
private AnimatedStripedDrawable drawable;
1716

1817
public AttributeController(Context context, AttributeSet attrs) {
19-
button = new StrippedButton();
18+
drawable = new AnimatedStripedDrawable(context);
2019
initAttrs(context, attrs);
2120
}
2221

@@ -33,26 +32,26 @@ private void initAttrs(Context context, AttributeSet attrs) {
3332
float cornerRadius = typedArray.getFloat(R.styleable.StripedProgressButton_spb_cornerRadius, Constants.CORNER);
3433
boolean stripeRevert = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_stripeRevert, Constants.REVERT);
3534
boolean showStripes = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_showStripes, Constants.SHOW_STRIPES);
36-
boolean active = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_active, Constants.ACTIVE);
35+
boolean stripeGradient = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_stripeGradient, Constants.GRADIENT);
3736

38-
button.setStripeWidth(stripeWidth)
37+
drawable.setStripeWidth(stripeWidth)
3938
.setStripeAlpha(stripeAlpha)
40-
.setStripeTilt(stripeTilt)
39+
.setTilt(stripeTilt)
4140
.setDuration(duration)
42-
.setBackground(background)
43-
.setMainStripeColor(mainStripeColor)
44-
.setSubStripeColor(subStripeColor)
41+
.setColorBack(background)
42+
.setColorMain(mainStripeColor)
43+
.setColorSub(subStripeColor)
4544
.setCornerRadius(cornerRadius)
4645
.setStripeRevert(stripeRevert)
4746
.setShowStripes(showStripes)
48-
.setActive(active);
47+
.setStripeGradient(stripeGradient);
4948
} finally {
5049
typedArray.recycle();
5150
}
5251
}
5352

54-
public StrippedButton getButton() {
55-
return button;
53+
public AnimatedStripedDrawable getStripedDrawable() {
54+
return drawable;
5655
}
5756

5857
}

0 commit comments

Comments
 (0)