Android studio模拟动态心电

Source

效果图:

1.Ecg_View.java

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/2

    private 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();
            }
        }

        ;
    };

    //发送随机数据到refresList
    public Ecg_View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setBackgroundColor(getResources().getColor(R.color.white));
        new Thread(new Runnable() {

            @Override
            public 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

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;

    @Override
    protected 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

<com.example.myapplication.ECG_VIEW.Ecg_View
    android:layout_width="match_parent"
    android:layout_height="265dp"
    android:id="@+id/ecg_data_view"
    tools:ignore="MissingConstraints"
    >

</com.example.myapplication.ECG_VIEW.Ecg_View>

初来咋到新手,代码略有稚嫩,本小程序只是给与新手启发,若有错误还请大牛批评指正

发布了1 篇原创文章 · 获赞 0 · 访问量 85