通過藍牙獲取到心電數(shù)據(jù),使用canvas繪制心電圖及背景網(wǎng)格,代碼如下
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//創(chuàng)建畫筆
Paint p = new Paint();
//設(shè)置綠色
p.setColor(Color.GREEN);
//獲得當前屏幕寬度
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
//離上方的高度
int top = 100;
//------------------畫背景表格-----------------
//畫豎線
for (int x = 0;x < width;x+=15) {
//心電圖網(wǎng)格5小格構(gòu)成一大格,大格加粗,需要判斷當前是否繪制到第5的倍數(shù)條線段
if (x%75==0) {
p.setStrokeWidth((float)2);
} else {
p.setStrokeWidth((float)1);
}
canvas.drawLine(x, top, x, 555, p);
}
//畫橫線
for (int y = top;y < 555;y+=15) {
//同理加粗
if ((y-top)%75==0) {
p.setStrokeWidth((float)2);
} else {
p.setStrokeWidth((float)1);
}
canvas.drawLine(0, y, width, y, p);
}
//-----------------------------------------
//畫數(shù)據(jù)
for (int i = 0;i < pointList.size();i++) {
//設(shè)置紅色
p.setColor(Color.RED);
p.setStrokeWidth((float)2);
int currentData = (int) pointList.get(i);
// if (currentPt.y != 325) {
// System.out.println("錯誤數(shù)據(jù):"+currentPt);
// }
if (i==0) {
//從表格(0,y/2)開始畫
canvas.drawLine(0, 325, i, currentData, p);
} else {
//取出上一個點
int beyongData = (int) pointList.get(i-1);
//連接上一個和當前點
canvas.drawLine(i-1, beyongData, i
, currentData, p);
//判斷是否畫到最右邊
if (i%width == 0) {
//清除數(shù)據(jù)
pointList.clear();
}
}
}
}
public void setLineData(int data) {
//添加數(shù)據(jù)
pointList.add(data);
// System.out.println("size:"+pointList.size());
//重繪
invalidate();
}
至此,canvas繪制心電圖的代碼已完成
//藍牙通知回調(diào)
public void setNotify(UUID serviceUUID, UUID characterUUID) {
mClient.notify(MAC, serviceUUID, characterUUID, new BleNotifyResponse() {
@Override
public void onNotify(UUID service, UUID character, byte[] value) {
//解析value,提取出value中的心電數(shù)據(jù),調(diào)用
view.setLineData(ECGData);
}
@Override
public void onResponse(int code) {
}
});
}