WaveView.java
8.4 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.hh.xuetubao.Utils;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.hh.xuetubao.R;
public class WaveView extends View {
//View的宽高
private int width;
private int height;
//View的画笔
private Paint wavePaint;
private Paint textPaint;
private Paint circlePaint;
//波浪的路径
private Path path;
//sin曲线的长度:一个周期的长度
private int cycle = 160;
//每次平移的长度,为四分之一个周期
private int translateX = cycle / 4;
//sin曲线振幅的高度
private int waveHeight = 80;
//sin曲线的起点坐标
private Point startPoint;
//当前波浪的进度
private int progress = 0;
//当前波浪的速度
private int waveSpeech = 150;
//是否启用了自动增长进度
public boolean isAutoIncrease = false;
public WaveView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint(context);
}
public WaveView(Context context) {
super(context);
initPaint(context);
}
private void initPaint(Context context) {
path = new Path();
wavePaint = new Paint();
wavePaint.setAntiAlias(true);
wavePaint.setStyle(Paint.Style.FILL);
wavePaint.setColor(getResources().getColor(R.color.color_ff5400));//Color.parseColor("#1998FA")
circlePaint = new Paint();
circlePaint.setStrokeWidth(5);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setAntiAlias(true);
circlePaint.setColor(getResources().getColor(R.color.color_ff5400)); // "#1998FA"
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(50);
textPaint.setColor(Color.parseColor("#ffffff"));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置内间距
setPadding(20, 20, 20, 20);
//裁剪画布为圆形
clipCicle(canvas);
//绘制圆形边框
drawCicleBorder(canvas);
//绘制波浪区域
drawWavePath(canvas);
//绘制进度文字
drawProcessText(canvas);
//自动增长进度
if (isAutoIncrease) {
if (progress >= 100) {
progress = 0;
} else {
progress++;
}
}
//更新UI
// postInvalidateDelayed(waveSpeech);
}
/**
* 绘制进度文字
*
* @param canvas
*/
private void drawProcessText(Canvas canvas) {
//画布的大小
Rect targetRect = new Rect(0, 0, width, height);
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
// 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX()
textPaint.setTextAlign(Paint.Align.CENTER);
if(!TextUtils.isEmpty(text)){
canvas.drawText(progress + text, targetRect.centerX(), baseline, textPaint);
}else{
canvas.drawText(progress + "分", targetRect.centerX(), baseline, textPaint);
}
}
/**
* 绘制波浪区域
*
* @param canvas
*/
private void drawWavePath(Canvas canvas) {
//根据进度改变起点坐标的y值
startPoint.y = (int) ((1 - (progress / 100.0)) * (height / 2 + width / 2));
Log.e("TAG", "startPoint.y:" + startPoint.y);
//移动区域起点
path.moveTo(startPoint.x, startPoint.y);
int j = 1;
//循环绘制正弦曲线区域,循环两个周期
for (int i = 1; i <= 8; i++) {
if (i % 2 == 0) {
//波峰
path.quadTo(startPoint.x + (cycle * j), startPoint.y + waveHeight,
startPoint.x + (cycle * 2) * i, startPoint.y);
} else {
//波谷
path.quadTo(startPoint.x + (cycle * j), startPoint.y - waveHeight,
startPoint.x + (cycle * 2) * i, startPoint.y);
}
j += 2;
}
//绘制封闭的区域
path.lineTo(width, height);//右下角
path.lineTo(startPoint.x, height);//左下角
path.lineTo(startPoint.x, startPoint.y);//起点
path.close();
//绘制区域
canvas.drawPath(path, wavePaint);
path.reset();
//一开始的起点是在-160,160 = 40 + 40 + 40 + 40,走完一个周期则回到原点
if (startPoint.x + translateX >= 0) {
startPoint.x = -cycle * 4;
} else {
startPoint.x += translateX;
}
}
/**
* 绘制圆形边框
*
* @param canvas
*/
private void drawCicleBorder(Canvas canvas) {
canvas.drawPaint(circlePaint);
canvas.drawCircle(width / 2, height / 2, width / 2, circlePaint);
}
/**
* 裁剪画布为圆形
*
* @param canvas
*/
private void clipCicle(Canvas canvas) {
Path circlePath = new Path();
circlePath.addCircle(width / 2, height / 2, width / 2, Path.Direction.CW);
canvas.clipPath(circlePath);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//由于是一个圆形,所以取的值是宽高的最小值
width = measureSize(400, widthMeasureSpec);
height = measureSize(400, heightMeasureSpec);
width = Math.min(width, height);
height = Math.min(width, height);
setMeasuredDimension(width, height);
//初始化起点,为屏幕外的一个周期
startPoint = new Point(-cycle * 4, 0);
}
/**
* 测量宽高
*
* @param defaultSize
* @param measureSpec
* @return
*/
private int measureSize(int defaultSize, int measureSpec) {
int result = defaultSize;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch (mode) {
case MeasureSpec.UNSPECIFIED: //如果没有指定大小,就设置为默认大小
result = defaultSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = size;
break;
}
return result;
}
/**
* 开启自动增长
*/
public void startIncrease() {
isAutoIncrease = true;
invalidate();
}
/**
* 设置当前进度
*
* @param progress 进度
*/
public void setProgress(int progress) {
if (progress > 100 || progress < 0)
throw new RuntimeException(getClass().getName() + "请设置[0,100]之间的值");
this.progress = progress;
invalidate();
}
private String text;
public void setText(String text) {
if (!TextUtils.isEmpty(text)) {
this.text = text;
}
invalidate();
}
/**
* 通过动画设置当前进度
*
* @param targetProcess 进度 <=100
* @param duration 动画时长
*/
public void setProgress(final int targetProcess, int duration) {
if (progress > 100 || progress < 0)
throw new RuntimeException(getClass().getName() + "请设置[0,100]之间的值");
ValueAnimator progressAnimator = ValueAnimator.ofInt(progress, targetProcess);
progressAnimator.setDuration(duration);
progressAnimator.setTarget(progress);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
setProgress((Integer) animation.getAnimatedValue());
}
});
progressAnimator.start();
}
/**
* 获取当前进度
*
* @return
*/
public int getProgress() {
return progress;
}
}