如何使用canvas在android中绘制心电图波形

时间:2016-04-21 05:59:27

标签: java android android-studio android-canvas

我尝试使用canvas方法绘制波形。不幸的是,我获得了当前位置,但我需要全波显示在视野中。

MainActivity

public class LineView extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_line_view);

}

XML

 <LinearLayout android:orientation="vertical"
    android:layout_height="350dp"
    android:layout_width="fill_parent">

<com.akasmedical.android.linedrawing.DemoView
    android:layout_height="match_parent"
    android:layout_width="wrap_content"/>

`

我的观点在这里

public class DemoView extends View {
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
int x = 35, j = 35;
int i = 0;
//ECG Wave form
int[] a = {20, 50, 40, 65, 56, 43, 22, 23, 55, 77, 76, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25};

public DemoView(Context context) {
    super(context);
    init();
}

public DemoView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DemoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();

}

public void init() {
    paint.setColor(Color.WHITE);
    paint1.setColor(Color.RED);
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if (i != 0) {
        //draw wave form here
        canvas.drawLine(x, (getHeight() / 4) - a[i], x + 1, (getHeight() / 4) - a[i - 1], paint1);
        System.out.println(x);
    }
    i++;
    if (i == 22) {
        i = 0;
    }
    //Repaint wave form
    if (x > getWidth() - 25) {
        canvas.drawRect(j, 0, j + 20, getHeight(), paint);
        j += 4;
        if (j > getWidth() - 25)
            j = 35;
    } else {
        x += 4;
    }
    invalidate();
}}

此处的预期输出

我不仅需要当前结束位置。我希望以前的位置也在视图活动中绘制。

1 个答案:

答案 0 :(得分:0)

您必须将数据点映射到像素,并在这些像素之间绘制线条。

您应该在onDraw()上绘制所有点,而不只是一个。像这样:

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int ecg_max = 250;
    int pixelx=0;
    int pixely=0;
    int pixelx_prev=0;
    int pixely_prev=0;

    for (int i=0;i<a.length;i++){
        //map point number to X
        pixelx = getWidth()*(i/a.length);
        //map point value to Y (notice that the axis is inverted)
        pixely = getHeight() - getHeight()*(a[i]/ecg_max);
        canvas.drawLine(pixelx, pixely, pixelx_prev, pixely_prev, paint);
        pixelx_prev = pixelx;
        pixely_prev = pixely;
    }

}