-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWaveView.java
More file actions
171 lines (151 loc) · 5.19 KB
/
WaveView.java
File metadata and controls
171 lines (151 loc) · 5.19 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
package cn.chenyuantao.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Tao on 2016/7/17.
*/
public class WaveView extends View {
private int mViewWidth;
private int mViewHeight;
private int mWaveHeight;
private int mFirstProgress;
private int mSecondProgress;
private Paint mPaint;
private Path mPath;
private int mColor;
private Boolean canRun;
private Point[] mStartPoints;
private Point[] mControlPoints;
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
try {
while (canRun) {
postInvalidate();
mFirstProgress++;
mSecondProgress++;
if (mFirstProgress == 25) {
mSecondProgress = 0;
} else if (mFirstProgress == 100) {
mFirstProgress = 0;
}
Thread.sleep(75);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
public WaveView(Context context) {
super(context);
init();
}
public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
canRun = true;
mFirstProgress = 0;
mSecondProgress = -25;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mColor = Color.rgb(63, 81, 181);
mPaint.setColor(mColor);
mPaint.setStyle(Paint.Style.FILL);
mPath = new Path();
Thread thread = new Thread(mRunnable);
thread.start();
}
private void initMeasure() {
mWaveHeight = mViewWidth / 30;
mStartPoints = new Point[4];
mControlPoints = new Point[4];
for (int i = 0; i < 4; i++) {
Point startPoint = new Point();
Point controlPoint = new Point();
startPoint.x = -3 * mViewWidth + mViewWidth * i;
startPoint.y = mViewHeight - mWaveHeight;
controlPoint.x = -5 * mViewWidth / 2 + mViewWidth * i;
if (i % 2 == 0) {
controlPoint.y = startPoint.y + mWaveHeight;
} else {
controlPoint.y = startPoint.y - mWaveHeight;
}
mStartPoints[i] = startPoint;
mControlPoints[i] = controlPoint;
;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mViewWidth = getMeasuredWidth();
mViewHeight = getMeasuredHeight();
initMeasure();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 新建图层
canvas.saveLayer(-mViewWidth, 0, mViewWidth, mViewHeight, null, Canvas.ALL_SAVE_FLAG);
int increment = 2 * mViewWidth * mFirstProgress / 100;
mPath.reset();
for (int i = 0; i < 4; i++) {
mPath.moveTo(mStartPoints[i].x + increment, mStartPoints[i].y);
mPath.quadTo(mControlPoints[i].x + increment, mControlPoints[i].y, mStartPoints[i].x + mViewWidth + increment, mStartPoints[i].y);
}
canvas.drawPath(mPath, mPaint);
//设置混合模式 (交叉部分不绘制)
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawRect(-mViewWidth, 0, mViewWidth, mViewHeight - mWaveHeight, mPaint);
// 还原混合模式
mPaint.setXfermode(null);
// 还原图层
canvas.restore();
// 新建图层绘制虚波浪线
canvas.saveLayerAlpha(-mViewWidth, 0, mViewWidth, mViewHeight, 60, Canvas.ALL_SAVE_FLAG);
increment = 2 * mViewWidth * mSecondProgress / 100;
mPath.reset();
for (int i = 0; i < 4; i++) {
mPath.moveTo(mStartPoints[i].x + increment, mStartPoints[i].y);
mPath.quadTo(mControlPoints[i].x + increment, mControlPoints[i].y, mStartPoints[i].x + mViewWidth + increment, mStartPoints[i].y);
}
canvas.drawPath(mPath, mPaint);
//设置混合模式 (交叉部分不绘制)
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawRect(-mViewWidth, 0, mViewWidth, mViewHeight - mWaveHeight, mPaint);
// 还原混合模式
mPaint.setXfermode(null);
// 还原图层
canvas.restore();
}
private class Point {
public int x;
public int y;
}
public void setColor(int color) {
this.mColor = color;
mPaint.setColor(mColor);
}
public int getColor() {
return this.mColor;
}
public void start() {
canRun = true;
}
public void stop() {
canRun = false;
}
}