package com.zhjy.mycommonprogect.ecg;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Administrator on 2017/8/23.
*/
public class EcgView extends View {
//画笔
protected Paint mPaint;
protected Paint mPath;
//网格颜色
protected int mGridColor = Color.parseColor("#1b4200");
//小网格颜色
protected int mSGridColor = Color.parseColor("#092100");
//背景颜色
protected int mBackgroundColor = Color.BLACK;
//自身的大小
protected int mWidth, mHeight;
//网格宽度
protected int mGridWidth = 50;
//小网格的宽度
protected int mSGridWidth = 10;
Context context;
public EcgView(Context context) {
super(context);
this.context = context;
}
public EcgView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public EcgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public EcgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = w;
mHeight = h;
xing();
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initBackground(canvas);
}
private void initBackground(Canvas canvas) {
mPaint = new Paint();
mPath = new Paint();
canvas.drawColor(mBackgroundColor);
//画小网格
//竖线个数
int vSNum = mWidth / mSGridWidth;
//横线个数
int hSNum = mHeight / mSGridWidth;
mPaint.setColor(mSGridColor);
mPaint.setStrokeWidth(2);
//画竖线
for (int i = 0; i < vSNum + 1; i++) {
canvas.drawLine(i * mSGridWidth, 0, i * mSGridWidth, mHeight, mPaint);
}
//画横线
for (int i = 0; i < hSNum + 1; i++) {
canvas.drawLine(0, i * mSGridWidth, mWidth, i * mSGridWidth, mPaint);
}
//竖线个数
int vNum = mWidth / mGridWidth;
//横线个数
int hNum = mHeight / mGridWidth;
mPaint.setColor(mGridColor);
mPaint.setStrokeWidth(2);
//画竖线
for (int i = 0; i < vNum + 1; i++) {
canvas.drawLine(i * mGridWidth, 0, i * mGridWidth, mHeight, mPaint);
}
//画横线
for (int i = 0; i < hNum + 1; i++) {
canvas.drawLine(0, i * mGridWidth, mWidth, i * mGridWidth, mPaint);
}
mPath.setColor(Color.RED);
mPath.setStrokeWidth(2.0F);
final float scale = context.getResources().getDisplayMetrics().density;
if (data.size() > 0) {
// 画搏动折线
for (int k = 1; k < data.size(); k++) {
if (data.get(k) == xx) {
float b = (k) * 5 * scale;
float x = 0;
for (int i = 1; i < xlist.size(); i++) {
HashMap<Integer, Integer> map1 = xlist.get(i - 1);
int x1 = map1.get(0);
int y1 = map1.get(1);
HashMap<Integer, Integer> map2 = xlist.get(i);
int x2 = map2.get(0);
int y2 = map2.get(1);
if (i == 1) {
x = x1 - b;
}
canvas.drawLine(x1 - x, y1, x2 - x, y2, mPath);
}
} else {
canvas.drawLine((k - 1) * 5 * scale, data.get(k - 1) * scale, k * 5 * scale, data.get(k) * scale, mPath);
}
}
}
if (data.size() > 60) {
data.remove(0);//移除左边图形 让图形移动起来
}
}
ArrayList<Integer> data = new ArrayList<>();
public void setData(int i) {
data.add(getMobileHeight(context) + i);
postInvalidate();
}
int xx;
public int getMobileHeight(Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
xx = (int) (mHeight / scale / 2);
return (int) (mHeight / scale / 2);
}
ArrayList<HashMap<Integer, Integer>> xlist = new ArrayList<>();
private void xing() {
// Paint paint = new Paint();// 创建画笔
// paint.setStyle(Paint.Style.STROKE);
// paint.setStrokeWidth(2);
// paint.setColor(Color.BLUE);
Canvas canvas = null;
// Path path = new Path();
xlist.clear();
final float scale = context.getResources().getDisplayMetrics().density*2;//调整心 大小
for (int i = 0 + 180; i < 361 + 180; i = i + 10) {
double sit = i * 2 * Math.PI / 360d;
double x = scale* 16 * Math.pow(Math.sin(sit), 3);
double y = scale* (13 * Math.cos(sit) - 5 * Math.cos(2 * sit) - 2 * Math.cos(3 * sit) - Math.cos(4 * sit));
HashMap<Integer, Integer> map = new HashMap();
map.put(0, (int) (mWidth / 2 + x));
map.put(1, (int) (mHeight / 2 - y-(scale*15)));//调整心 高度未知
xlist.add(map);
// if (i == 180) {
// path.moveTo(mWidth / 2 + (float) (x), mHeight / 2 - (float) (y));
// }
// path.lineTo(mWidth / 2 + (float) (x), mHeight / 2 - (float) (y));
//
// if (canvas != null)
// canvas.drawPath(path, paint);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zhjy.mycommonprogect.ecg.EcgActivity">
<com.zhjy.mycommonprogect.ecg.EcgView
android:id="@+id/ecg_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity里使用kotilin写的
class EcgActivity : AppCompatActivity() {
var list = ArrayList<Int>()
val timer = Timer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ecg)
init()
}
fun init() {
timer.schedule(object : TimerTask() {
override fun run() {
handler.sendEmptyMessage(0)
}
}, 100, 200)
}
var i = 1
var handler: Handler = object : Handler() {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
var r = 1+Random().nextInt(10)
i++
if (i == 10) {
r -= 200
}
if (i == 20) {
r = 55
}
if (i == 21) {
r = 45
}
if (i == 22) {
r = 50
}
if (i == 30) {
r = 0//心形位置
}
if (i == 40) {
i = 0
}
println(r)
ecg_view.setData(r)
}
}
override fun onDestroy() {
super.onDestroy()
timer.cancel()
}
}