-
Notifications
You must be signed in to change notification settings - Fork 6
/
GuidanceRippleView.java
205 lines (181 loc) · 6.37 KB
/
GuidanceRippleView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package jsc.kit.guidance;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* <br>Email:[email protected]
* <br>QQ:1006368252
* <br><a href="https://github.com/JustinRoom/GuidanceDemo" target="_blank">https://github.com/JustinRoom/GuidanceDemo</a>
*
* @author jiangshicheng
*/
public class GuidanceRippleView extends View {
private Paint paint;
private int count;
private int[] colors;
private float space;
private float speed;
private boolean autoRun;
private int frameCountPerSecond = 0;
private float radius = 0;
private boolean isRunning = false;
private int clipWidth;
private int clipHeight;
public GuidanceRippleView(Context context) {
super(context);
initAttr(context, null, 0);
}
public GuidanceRippleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttr(context, attrs, 0);
}
public GuidanceRippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttr(context, attrs, defStyleAttr);
}
public void initAttr(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GuidanceRippleView, defStyleAttr, 0);
count = a.getInteger(R.styleable.GuidanceRippleView_grvCount, 3);
space = a.getDimension(R.styleable.GuidanceRippleView_grvSpace, 0);
frameCountPerSecond = a.getInteger(R.styleable.GuidanceRippleView_grvSpeed, 48);
autoRun = a.getBoolean(R.styleable.GuidanceRippleView_grvAutoRun, true);
String colors = a.getString(R.styleable.GuidanceRippleView_grvColors);
a.recycle();
if (count < 1)
throw new IllegalArgumentException("count must be more than one.");
if (frameCountPerSecond < 1)
throw new IllegalArgumentException("speed should be one frame per second at least.");
this.colors = new int[count];
if (colors != null) {
colors = colors.trim();
colors = colors.replace("{", "");
colors = colors.replace("}", "");
colors = colors.replace("(", "");
colors = colors.replace(")", "");
String[] colorSplit = colors.split(",");
for (int i = 0; i < count; i++) {
if (colorSplit.length > 0)
this.colors[i] = Color.parseColor(colorSplit[i % colorSplit.length].trim());
else
this.colors[i] = Color.BLUE;
}
} else {
for (int i = 0; i < count; i++) {
this.colors[i] = Color.BLUE;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
if (space <= 0) {
space = getMeasuredWidth() * 1.0f / (count * 2);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
speed = -1;
}
@Override
protected void onDraw(Canvas canvas) {
if (speed <= 0) {
speed = space / frameCountPerSecond;
}
if (clipWidth > 0 && clipHeight > 0) {
int clipLeft = (getWidth() - clipWidth) / 2;
int clipTop = (getHeight() - clipHeight) / 2;
canvas.clipRect(clipLeft, clipTop, clipLeft + clipWidth, clipTop + clipHeight);
}
float maxRadius = getWidth() / 2.0f;
int alpha = (int) (0xFF * (1 - radius / maxRadius) + .5f);
for (int i = 0; i < count; i++) {
paint.setColor(colors[i]);
paint.setAlpha(alpha);
float tempRadius = radius - space * i;
if (tempRadius > 0)
canvas.drawCircle(maxRadius, maxRadius, tempRadius, paint);
}
radius += speed;
if (radius > maxRadius)
radius = 0;
if (isRunning)
invalidate();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (autoRun)
start();
}
@Override
protected void onDetachedFromWindow() {
stop();
super.onDetachedFromWindow();
}
public void start() {
if (isRunning)
return;
radius = 0;
isRunning = true;
invalidate();
}
public void stop() {
radius = 0;
isRunning = false;
invalidate();
}
public void pause() {
isRunning = false;
}
public void resume() {
isRunning = true;
invalidate();
}
/**
* Whether run animation automatically when {@link #onAttachedToWindow()}.
*
* @param autoRun true, run animation automatically when {@link #onAttachedToWindow()}.
*/
public void setAutoRun(boolean autoRun) {
this.autoRun = autoRun;
}
/**
* Set the ripple animation display area.
*
* @param clipWidth clip width
* @param clipHeight clip height
*/
public void setClip(int clipWidth, int clipHeight) {
this.clipWidth = clipWidth;
this.clipHeight = clipHeight;
}
/**
* @param count ripple circle count
* @param colors colors
* @param space the radius offset of two ripple circle
* @param frameCountPerSecond the speed of ripple animation
*/
public void updateAnimation(int count, float space, int frameCountPerSecond, int... colors) {
if (count < 1)
throw new IllegalArgumentException("count must be more than one.");
if (frameCountPerSecond < 1)
throw new IllegalArgumentException("speed should be one frame per second at least.");
this.colors = new int[count];
if (colors == null || colors.length == 0)
colors = new int[]{Color.BLUE};
for (int i = 0; i < count; i++) {
this.colors[i] = colors[i % colors.length];
}
this.count = count;
this.space = space;
this.speed = -1;
}
}