CommentVoiceLineView.java
4.45 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
package com.wd.comment.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import com.wd.comment.R;
/**
* @author LiuKun
* @date 2023/5/2 16:40
* @Description:语音录入
*/
public class CommentVoiceLineView extends View {
private int voiceLineColor = Color.BLACK;
private Paint paintVoiceLine;
/**
* 灵敏度
*/
private int sensibility = 4;
private float maxVolume = 100;
private float translateX = 0;
private boolean isSet = false;
/**
* 振幅
*/
private float amplitude = 1;
/**
* 音量
*/
private float volume = 10;
private int fineness = 1;
private float targetVolume = 1;
private long lastTime = 0;
private int lineSpeed = 90;
private Path path1;
private Path path2;
private boolean isStart = false;
public CommentVoiceLineView(Context context) {
super(context);
}
public CommentVoiceLineView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CommentVoiceLineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@SuppressLint("ResourceType")
private void init(Context context) {
voiceLineColor = context.getResources().getColor(R.color.res_color_C10);
maxVolume = 50;
sensibility = 4;
lineSpeed = 90;
fineness = 1;
path1 = new Path();
path2 = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
drawVoiceLine(canvas);
run();
}
private void lineChange() {
if (lastTime == 0) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
if (System.currentTimeMillis() - lastTime > lineSpeed) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
return;
}
}
if (volume < targetVolume && isSet) {
volume += getHeight() / 30;
} else {
isSet = false;
if (volume <= 10) {
volume = 10;
} else {
if (volume < getHeight() / 30) {
volume -= getHeight() / 60;
} else {
volume -= getHeight() / 30;
}
}
}
}
private void drawVoiceLine(Canvas canvas) {
lineChange();
if (paintVoiceLine == null) {
paintVoiceLine = new Paint();
paintVoiceLine.setColor(voiceLineColor);
paintVoiceLine.setAntiAlias(true);
paintVoiceLine.setStyle(Paint.Style.STROKE);
paintVoiceLine.setStrokeWidth(2);
}
canvas.save();
int moveY = getHeight() / 2;
path1.reset();
path1.moveTo(getWidth(), getHeight() / 2);
if (!isStart) {
path1.lineTo(0, getHeight() / 2);
canvas.drawPath(path1, paintVoiceLine);
return;
}
for (float i = getWidth() - 1; i >= 0; i -= fineness) {
amplitude = 4 * volume * i / getWidth() - 4 * volume * i * i / getWidth() / getWidth();
float sin = amplitude * (float) Math.sin((i - 53.36) * Math.PI / 180 - translateX);
path1.lineTo(i, (2 * sin - 3 * sin / 4 + moveY));
}
canvas.drawPath(path1, paintVoiceLine);
path2.reset();
path2.moveTo(getWidth(), getHeight() / 2);
for (float i = getWidth() - 1; i >= 0; i -= fineness) {
amplitude = 4 * volume * i / getWidth() - 4 * volume * i * i / getWidth() / getWidth();
float sin = amplitude * (float) Math.sin((i - 53.36) * Math.PI / 180 - translateX);
path2.lineTo(i, getHeight() - (2 * sin - 3 * sin / 4 + moveY));
}
canvas.drawPath(path2, paintVoiceLine);
canvas.restore();
}
public void setVolume(int volume) {
isStart = true;
if (volume > maxVolume * sensibility / 25) {
isSet = true;
this.targetVolume = getHeight() * volume / 2 / maxVolume;
}
}
public void stop() {
isStart = false;
run();
}
public void run() {
invalidate();
}
}