-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSiriView.java
More file actions
183 lines (153 loc) · 3.75 KB
/
SiriView.java
File metadata and controls
183 lines (153 loc) · 3.75 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
package com.tao.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
public class SiriView extends View {
// ÄÚ²¿
private int progress;
private Thread thread;
private float targetHeight;
private Paint paint;
private Path path;
// ÊôÐÔ
private float waveHeight;
private float waveWidth;
private int waveColor;
private float waveOffsetX;
private int waveAmount;
private float waveSpeed;
public SiriView(Context context) {
super(context);
init(context);
}
public SiriView(Context context, AttributeSet attr) {
super(context, attr);
init(context);
}
public SiriView(Context context, AttributeSet attr, int style) {
super(context, attr, style);
init(context);
}
private void init(Context context) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
path = new Path();
waveSpeed = 0.1f;
waveOffsetX = 0f;
waveAmount = 4;
waveColor = Color.rgb(39, 188, 136);
waveHeight = 1f;
targetHeight = 1f;
waveWidth = 5f;
progress = 0;
thread = new Thread(runnable);
thread.start();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
// ֪ͨ¿Ø¼þÖØ»æ
invalidate();
}
}
};
private Runnable runnable = new Runnable() {
@Override
public void run() {
try {
while (true) {
handler.sendEmptyMessage(1);
Thread.sleep((int) (1 / waveSpeed));
progress += 1;
if (progress >= (20 * waveAmount)) {
progress = 0;
}
if (waveHeight < targetHeight) {
waveHeight += 0.01f;
} else {
waveHeight -= 0.01f;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float line = canvas.getWidth() / waveAmount;
path.reset();
paint.setColor(waveColor);
paint.setStrokeWidth(waveWidth);
float h;
for (int i = 0; i < (waveAmount * 2); i++) {
float x1 = (-canvas.getWidth() + line * i) + line * waveOffsetX;
float x2 = (-canvas.getWidth() + line * (i + 0.5f)) + line
* waveOffsetX;
float x3 = (-canvas.getWidth() + line * (i + 1f)) + line
* waveOffsetX;
if (i % 2 == 0) {
h = (float) (canvas.getHeight() / 2 + canvas.getHeight() / 2
* waveHeight);
} else {
h = (float) (canvas.getHeight() / 2 - canvas.getHeight() / 2
* waveHeight);
}
path.moveTo(x1 + canvas.getWidth() * progress / (20 * waveAmount),
canvas.getHeight() / 2);
path.quadTo(x2 + canvas.getWidth() * progress / (20 * waveAmount),
h, x3 + canvas.getWidth() * progress / (20 * waveAmount),
canvas.getHeight() / 2);
}
canvas.drawPath(path, paint);
}
public void stop() {
targetHeight = 0;
}
public void setWaveHeight(float height) {
this.targetHeight = height;
}
public float getWaveHeight() {
return this.waveHeight;
}
public void setWaveWidth(float width) {
this.waveWidth = width;
}
public float getWaveWidth() {
return this.waveWidth;
}
public int getWaveColor() {
return waveColor;
}
public void setWaveColor(int waveColor) {
this.waveColor = waveColor;
}
public float getWaveOffsetX() {
return waveOffsetX;
}
public void setWaveOffsetX(float waveOffsetX) {
this.waveOffsetX = waveOffsetX;
}
public int getWaveAmount() {
return waveAmount;
}
public void setWaveAmount(int waveAmount) {
this.waveAmount = waveAmount;
}
public float getWaveSpeed() {
return waveSpeed;
}
public void setWaveSpeed(float waveSpeed) {
this.waveSpeed = waveSpeed;
}
}