Skip to content

Commit 5189b98

Browse files
committed
Added button animation,
Fixed stripes animation, Fixed attrs
1 parent 04ec898 commit 5189b98

7 files changed

Lines changed: 81 additions & 43 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:color="?attr/colorControlHighlight">
4+
<item android:id="@android:id/mask">
5+
<shape android:shape="rectangle">
6+
<solid android:color="?android:colorAccent" />
7+
<corners android:radius="50dp" />
8+
</shape>
9+
</item>
10+
</ripple>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
android:layout_marginStart="16dp"
2626
android:layout_marginTop="16dp"
2727
android:text="Example"
28+
android:clickable="true"
29+
android:focusable="true"
2830
android:textColor="#ffffff"
29-
app:spb_cornerRadius="50"
30-
app:spb_duration="250" />
31+
app:spb_cornerRadius="50" />
3132

3233
<Button
3334
android:id="@+id/btn_stop"

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

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@
1212
import android.graphics.Rect;
1313
import android.graphics.RectF;
1414
import android.graphics.Shader;
15-
import android.graphics.drawable.Animatable;
1615
import android.graphics.drawable.Drawable;
1716
import android.support.annotation.NonNull;
1817
import 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private void initAttrs(Context context, AttributeSet attrs) {
2525
float stripeWidth = typedArray.getDimension(R.styleable.StripedProgressButton_spb_stripeWidth, Constants.STRIPE_WIDTH);
2626
float stripeAlpha = typedArray.getFloat(R.styleable.StripedProgressButton_spb_stripeAlpha, Constants.STRIPE_ALPHA);
2727
int stripeTilt = typedArray.getInt(R.styleable.StripedProgressButton_spb_stripeTilt, Constants.STRIPE_TILT);
28-
int duration = typedArray.getInt(R.styleable.StripedProgressButton_spb_duration, Constants.DURATION);
28+
int stripeDuration = typedArray.getInt(R.styleable.StripedProgressButton_spb_stripeDuration, Constants.DURATION);
2929
int background = typedArray.getColor(R.styleable.StripedProgressButton_spb_background, Constants.DEF_BACKGROUND);
3030
int mainStripeColor = typedArray.getColor(R.styleable.StripedProgressButton_spb_mainStripColor, Constants.DEF_MAIN_STRIPE);
3131
int subStripeColor = typedArray.getColor(R.styleable.StripedProgressButton_spb_subStripeColor, Constants.DEF_SUB_STRIPE);
@@ -37,7 +37,7 @@ private void initAttrs(Context context, AttributeSet attrs) {
3737
drawable.setStripeWidth(stripeWidth)
3838
.setStripeAlpha(stripeAlpha)
3939
.setTilt(stripeTilt)
40-
.setDuration(duration)
40+
.setStripeDuration(stripeDuration)
4141
.setColorBack(background)
4242
.setColorMain(mainStripeColor)
4343
.setColorSub(subStripeColor)

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,31 @@ private void initAttrs(AttributeSet attrs) {
3434
stripedDrawable = attrController.getStripedDrawable();
3535
}
3636

37-
@Override
38-
protected void onAttachedToWindow() {
39-
super.onAttachedToWindow();
40-
}
41-
4237
@Override
4338
protected void onDraw(Canvas canvas) {
4439
setBackground(stripedDrawable);
40+
setEnabled(!isRunning());
4541
super.onDraw(canvas);
4642
}
4743

4844
@Override
4945
public void start() {
46+
if (isRunning()) {
47+
return;
48+
}
49+
setEnabled(isRunning());
5050
stripedDrawable.start();
51+
Util.Animation.animateView(this, true, 800);
5152
}
5253

5354
@Override
5455
public void stop() {
56+
if (!isRunning()) {
57+
return;
58+
}
59+
setEnabled(isRunning());
5560
stripedDrawable.stop();
61+
Util.Animation.animateView(this, false, 300);
5662
}
5763

5864
@Override

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.github.nikartm.support;
22

3+
import android.animation.Animator;
4+
import android.view.View;
5+
import android.view.ViewAnimationUtils;
6+
37
/**
48
* @author Ivan V on 29.03.2018.
59
* @version 1.0
@@ -24,4 +28,17 @@ public static int computeAlpha(float value) {
2428
private static float valueAlpha(float value) {
2529
return MAX_ALPHA * (value * 100f) / 100f;
2630
}
31+
32+
static class Animation {
33+
34+
public static void animateView(View view, boolean start, long duration) {
35+
int cx = start ? view.getWidth() / 2 : 0;
36+
int cy = view.getHeight() / 2;
37+
int finalRadius = Math.max(view.getWidth(), view.getHeight());
38+
39+
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
40+
anim.setDuration(duration);
41+
anim.start();
42+
}
43+
}
2744
}

support/src/main/res/values/attr.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<attr name="spb_stripeWidth" format="dimension" />
66
<attr name="spb_stripeAlpha" format="float" />
77
<attr name="spb_stripeTilt" format="integer" />
8-
<attr name="spb_duration" format="integer" />
8+
<attr name="spb_stripeDuration" format="integer" />
99
<attr name="spb_background" format="color" />
1010
<attr name="spb_mainStripColor" format="color" />
1111
<attr name="spb_subStripeColor" format="color" />

0 commit comments

Comments
 (0)