效果图:
1.Ecg_View.java
[code]package com.example.myapplication.ECG_VIEW;import android.content.Context;import android.graphics.Canvas;import android.graphics.DashPathEffect;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PathEffect;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.View;import androidx.annotation.Nullable;import com.example.myapplication.R;import java.util.ArrayList;import java.util.Random;import java.util.logging.LogRecord;public class Ecg_View extends View {private int horizontalBigGridLine = 8, verticalBigGridLine = 8;//设置大网格实线个数private int width;//背景页面宽度private int height;//背景页面高度private int widthOfSmallGrid;//小网格的宽度private int baseline;//中心y轴线y=height/2private ArrayList<Float> refreshList = new ArrayList<Float>();//模拟心电数据private float nowX, nowY;//当前的X,Y坐标值private float max_Value = 30;//最大幅度值/*开启线程模拟心电数据产生*/private android.os.Handler handler = new Handler() {public void handleMessage(Message msg) {if (msg.what == 0x1234) {Ecg_View.this.invalidate();}};};//发送随机数据到refresListpublic Ecg_View(Context context, @Nullable AttributeSet attrs) {super(context, attrs);this.setBackgroundColor(getResources().getColor(R.color.white));new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(1000);//每几秒执行一次} catch (InterruptedException e) {e.printStackTrace();}if (refreshList.size() >= horizontalBigGridLine * 5) {refreshList.remove(0);}refreshList.add(new Random().nextFloat() * (40f) - 20f);handler.sendEmptyMessage(0x1234);}}}).start();}//获取View的页面宽度高度以及小网格的宽度、基线位置y坐标值protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);width = w;height = h;widthOfSmallGrid = w / (verticalBigGridLine * 5);baseline = h / 2;// System.out.println(\"基线\"+baseline+\"高\"+height+\"宽\"+width);}//执行画图操作protected void onDraw(Canvas canvas) {super.onDraw(canvas);drawGrid(canvas);drawElectrocardiogram(canvas);}//描绘背景网格private void drawGrid(Canvas canvas) {//大网格横向线for (int i = 0; i <= verticalBigGridLine * 5; i++) {Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setColor(getResources().getColor(R.color.darkred));paint.setStrokeWidth(2.0f);Path mPath = new Path();mPath.moveTo(0, i * widthOfSmallGrid);mPath.lineTo(width, i * widthOfSmallGrid);if (i % 5 != 0) {PathEffect effect = new DashPathEffect(new float[]{1, 5}, 1);paint.setPathEffect(effect);}canvas.drawPath(mPath, paint);// canvas.drawLine(i*widthOfSmallGrid,0,i*widthOfSmallGrid,height,paint);}//大网格纵向线for (int i = 0; i <= horizontalBigGridLine * 5; i++) {Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setColor(getResources().getColor(R.color.darkred));paint.setStrokeWidth(2.0f);Path mPath = new Path();mPath.moveTo(i * widthOfSmallGrid, 0);//将画笔移至某点起笔位置mPath.lineTo(i * widthOfSmallGrid, 675);//落笔位置实现一条直线绘画if (i % 5 != 0) {//画小网格虚线PathEffect effect = new DashPathEffect(new float[]{1, 5}, 1);paint.setPathEffect(effect);}canvas.drawPath(mPath, paint);// canvas.drawLine(0,i*widthOfSmallGrid,width,i*widthOfSmallGrid,paint);}}//画心电private void drawElectrocardiogram(Canvas canvas) {Path electrocarPath = new Path();Paint electrocarPaint = new Paint();electrocarPaint.setColor(getResources().getColor(R.color.red));electrocarPaint.setStyle(Paint.Style.STROKE);electrocarPaint.setAntiAlias(true);electrocarPaint.setStrokeWidth(2);electrocarPaint.setAntiAlias(true);electrocarPath.moveTo(0, baseline);for (int i = 0; i < this.refreshList.size(); i++) {nowX = i * widthOfSmallGrid;float dataValue = (float) refreshList.get(i);nowY = baseline - dataValue * (baseline / (max_Value * 2));electrocarPath.lineTo(nowX, nowY);}canvas.drawPath(electrocarPath, electrocarPaint);}}
2.MainActivity
[code]package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.example.myapplication.ECG_VIEW.Ecg_Util;import com.example.myapplication.ECG_VIEW.Ecg_View;import com.example.myapplication.ECG_VIEW.MyView;public class MainActivity extends AppCompatActivity {private Ecg_View ecg_view;private Ecg_Util ecg_util;private MyView myView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ecg_view =(Ecg_View) findViewById(R.id.ecg_data_view);}}
3.activity_main.xml
[code]<com.example.myapplication.ECG_VIEW.Ecg_Viewandroid:layout_width=\"match_parent\"android:layout_height=\"265dp\"android:id=\"@+id/ecg_data_view\"tools:ignore=\"MissingConstraints\"></com.example.myapplication.ECG_VIEW.Ecg_View>
初来咋到新手,代码略有稚嫩,本小程序只是给与新手启发,若有错误还请大牛批评指正