public class WaveView extends View {
private byte[] bytes;
private Paint mPaint;
// 整个自定义组件的高度
private int height;
// 整个自定义组件的宽度
private int width;
// x轴的块状间隔宽度
private int x_interval = 2;
// y轴的块状间隔宽度
private int y_interval = 3;
// 颗粒状小图的高度
private int small_height = 6;
// 颗粒状小图的宽度
private int small_width = 6;
// 比例
private float scale = 0;
// 波形图的最大值
private final static int MAX = 128;
// 适配
private GetScreenSize screenSize = null;
// 如果没有数据时使用此数据
private byte[] defautBytes = new byte[] { -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, -128 };
// 没有波形数据时模拟数据
private byte[] simulateBytes1 = { 121, 120, 119, 119, 118, 118, 117, 117,
116, 116, 116, 116, 117, 117, 117, 118, 119, 119, 120, 120, 120,
121, 121, 122, 122, 123, 124, 125, 126, 127, -128, -126, -125,
-123, -122, -120, -119, -118, -116, -115, -113, -112, -110, -109,
-107, -106, -104, -103, -101, -100, -98, -96, -95, -94, -93, -93,
-92, -92, -93, -94, -95, -96, -98, -99, -101, -102, -104, -106,
-107, -108, -110, -112, -113, -115, -117, -119, -121, -123, -125,
-127, 127, 125, 123, 121, 119, 117, 115, 114, 112, 110, 109, 107,
106, 105, 104, 103, 102, 100, 99, 98, 97, 96, 95, 94, 94, 94, 94,
94, 94, 94, 95, 96, 97, 98, 99, 101, 102, 104, 105, 107, 109, 111,
113, 115, 118, 120, 122, 125 };
private byte[] simulateBytes2 = { -116, -117, -118, -119, -121, -122, -123,
-125, -126, -127, 127, 126, 125, 124, 123, 121, 120, 119, 118, 117,
116, 115, 115, 114, 113, 113, 112, 112, 111, 111, 111, 111, 111,
110, 111, 111, 111, 111, 111, 112, 113, 113, 114, 115, 117, 118,
120, 121, 123, 125, 127, -128, -126, -124, -122, -121, -119, -118,
-116, -115, -114, -113, -112, -112, -111, -111, -110, -110, -109,
-109, -109, -109, -110, -110, -111, -111, -112, -113, -114, -115,
-117, -118, -119, -120, -122, -123, -124, -125, -127, -128, 127,
125, 124, 123, 121, 120, 119, 118, 116, 115, 114, 114, 113, 112,
111, 111, 110, 110, 110, 110, 110, 109, 109, 109, 109, 110, 110,
110, 110, 111, 112, 113, 114, 115, 116, 118, 119, 121 };
private byte[] simulateBytes3 = { 117, 117, 116, 116, 116, 116, 116, 117,
117, 117, 118, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
-128, -127, -126, -125, -124, -123, -123, -122, -121, -120, -119,
-118, -117, -116, -116, -115, -115, -114, -114, -114, -114, -114,
-115, -115, -116, -116, -117, -118, -119, -120, -121, -123, -124,
-125, -127, -128, 126, 125, 123, 122, 121, 119, 118, 117, 115, 114,
113, 112, 111, 110, 109, 108, 107, 107, 106, 106, 105, 105, 105,
105, 105, 105, 105, 105, 106, 106, 107, 108, 109, 110, 111, 112,
114, 116, 118, 121, 123, 126, -128, -125, -122, -119, -117, -114,
-111, -109, -106, -103, -101, -99, -97, -94, -93, -91, -89, -88,
-87, -86, -85, -84, -84, -83, -83, -83, -83, -84, -84 };
private byte[] simulateBytes4 = { 124, 124, 124, 123, 123, 123, 123, 123,
123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 124, 124,
124, 124, 125, 125, 125, 126, 126, 126, 127, 127, 127, -128, -128,
-128, -128, -128, -128, -128, -128, 127, 127, 127, 126, 126, 125,
125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 113, 112,
111, 110, 110, 109, 108, 108, 107, 107, 107, 107, 107, 106, 106,
106, 106, 106, 106, 106, 106, 107, 107, 107, 108, 108, 109, 110,
111, 112, 113, 114, 115, 117, 118, 120, 122, 123, 125, 127, -128,
-126, -124, -122, -121, -119, -117, -116, -114, -112, -111, -109,
-108, -107, -105, -104, -102, -101, -100, -99, -98, -97, -96, -95,
-94, -94, -93, -93, -93, -93, -93 };
private byte[] simulateBytes5 = { -86, -87, -88, -89, -90, -91, -92, -93,
-95, -96, -97, -99, -101, -102, -104, -106, -107, -109, -111, -113,
-114, -116, -118, -119, -121, -122, -124, -125, -126, -128, 127,
125, 124, 122, 121, 119, 118, 116, 115, 113, 112, 111, 109, 108,
106, 105, 104, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 87, 86, 84, 83, 81, 80, 78, 77, 75, 74, 72, 71, 69, 68, 66,
65, 63, 62, 61, 59, 58, 57, 56, 55, 54, 54, 54, 53, 53, 53, 52, 52,
52, 52, 52, 52, 52, 52, 53, 53, 54, 55, 56, 57, 59, 60, 62, 63, 65,
67, 70, 72, 74, 76, 79, 81, 84, 86, 89, 91, 94, 96, 99, 102, 104,
107, 109 };
private byte[] simulateBytes6 = { 109, 111, 112, 114, 116, 117, 119, 121,
123, 124, 126, -128, -126, -124, -122, -120, -119, -117, -115,
-114, -112, -111, -110, -108, -107, -106, -105, -104, -103, -102,
-101, -100, -99, -98, -98, -97, -97, -97, -97, -96, -96, -96, -96,
-96, -96, -96, -96, -97, -97, -97, -97, -97, -97, -97, -97, -98,
-98, -98, -98, -98, -99, -99, -99, -99, -99, -98, -98, -98, -97,
-97, -96, -96, -95, -95, -94, -94, -94, -94, -93, -93, -93, -93,
-93, -93, -93, -93, -94, -94, -94, -94, -94, -95, -95, -95, -96,
-96, -97, -97, -98, -99, -100, -100, -101, -102, -104, -105, -106,
-108, -109, -111, -112, -114, -115, -117, -119, -120, -122, -124,
-126, -127, 127, 125, 124, 123, 121, 120, 119, 117 };
private byte[] simulateBytes7 = { -114, -114, -114, -114, -115, -115, -116,
-116, -117, -118, -119, -120, -121, -123, -124, -125, -126, -128,
127, 125, 124, 123, 121, 120, 119, 117, 116, 114, 113, 112, 110,
109, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 98, 97, 96,
95, 94, 93, 93, 92, 91, 90, 90, 89, 88, 88, 87, 87, 86, 86, 85, 85,
85, 85, 85, 85, 85, 85, 86, 86, 86, 87, 88, 89, 89, 90, 91, 92, 94,
95, 96, 97, 99, 100, 101, 103, 104, 106, 107, 108, 110, 111, 112,
113, 115, 116, 117, 118, 119, 121, 122, 123, 124, 126, 127, -127,
-126, -125, -123, -122, -121, -119, -118, -117, -116, -115, -114,
-114, -113, -112, -112, -111, -110, -110, -109, -108, -108, -107 };
private byte[] simulateBytes8 = { 110, 109, 108, 107, 107, 106, 105, 104,
103, 102, 102, 101, 100, 100, 99, 98, 98, 98, 97, 97, 97, 97, 98,
98, 98, 99, 100, 100, 101, 102, 103, 105, 106, 107, 108, 110, 111,
113, 114, 115, 117, 118, 120, 122, 123, 125, 126, -128, -127, -125,
-124, -122, -121, -119, -118, -116, -114, -113, -112, -110, -109,
-108, -107, -106, -106, -105, -104, -104, -103, -103, -103, -103,
-102, -102, -102, -102, -102, -102, -103, -103, -103, -103, -103,
-103, -103, -103, -103, -103, -104, -104, -104, -104, -105, -105,
-106, -107, -107, -108, -109, -109, -110, -111, -112, -112, -113,
-114, -114, -115, -115, -116, -117, -117, -118, -118, -119, -119,
-120, -121, -121, -122, -122, -122, -123, -123, -123, -123, -123,
-124 };
private byte[] simulateBytes9 = { 97, 97, 97, 97, 98, 98, 98, 99, 99, 100,
100, 101, 102, 103, 104, 105, 106, 107, 109, 111, 112, 114, 116,
118, 120, 122, 124, 126, -127, -125, -123, -121, -119, -117, -115,
-113, -111, -109, -107, -105, -103, -102, -100, -99, -98, -97, -96,
-95, -95, -94, -94, -94, -94, -94, -94, -94, -94, -94, -94, -95,
-95, -96, -96, -97, -98, -98, -99, -100, -101, -102, -103, -104,
-105, -106, -107, -108, -109, -110, -111, -112, -113, -114, -115,
-117, -118, -119, -120, -121, -121, -122, -123, -124, -125, -126,
-127, -128, -128, 127, 127, 126, 126, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126,
126, 126, 126, 126, 126, 126, 126, 126 };
// 模拟数据数组
private byte[][] totalBytes = new byte[][] { simulateBytes1,
simulateBytes2, simulateBytes3, simulateBytes4, simulateBytes5,
simulateBytes6, simulateBytes7, simulateBytes8, simulateBytes9 };
public AudioWaveDisplay(Context context, AttributeSet attrs) {
super(context, attrs);
screenSize = new GetScreenSize(context);
x_interval = GetScreenSize.autofitX(2);
y_interval = GetScreenSize.autofitY(3);
small_height = GetScreenSize.autofitX(6);
small_width = GetScreenSize.autofitX(6);
}
public void setSimulateData() {
int index = (int) (Math.random() * 9) % 9;
bytes = totalBytes[index];
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null) {
bytes = defautBytes;
}
getWidthAndHeight();
caculateScale();
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAlpha(80);
mPaint.setStrokeWidth(11);
mPaint.setAntiAlias(true);
int len = bytes.length;
int value = 0;
for (int i = 0; i < len - 1; i++) {
value = i * 3;
if (value >= len - 1) {
break;
}
double top = height - (int) (bytes[value] + 128) * height / 128;
top = top < 0 ? -top : top;
top = scale * top;
// 为了保持跟默认显示的波粒图一样
float x = 2 + i * (small_width + y_interval);
// float x = i * (small_width + y_interval);
for (int j = 1;; j++) {
float y = height - j * (small_height + x_interval);
if (top > y || x + small_width > width) {
if (j != 1) {
break;
}
}
canvas.drawRect(x, y, x + small_width, y + small_height, mPaint);
}
if (x + small_width > width) {
break;
}
}
}
private void caculateScale() {
scale = (float) height / MAX;
}
private void getWidthAndHeight() {
height = getHeight();
width = getWidth();
}
public boolean isFailed(byte[] values) {
boolean failed = true;
if (defautBytes.length == values.length) {
int len = defautBytes.length;
for (int i = 0; i < len; i++) {
if (defautBytes[i] != values[i]) {
failed = false;
break;
}
}
} else {
failed = false;
}
return failed;
}
public void UpdateAudioWaveDisplay(byte[] bytes) {
this.bytes = bytes;
invalidate();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章