0

我正在尝试绘制由细线组成的心电图,但是当我调用该Canvas drawLine方法时,该方法会像形状一样填充线条的顶部

心电图

这是绘制图表的类:

public class ECGView extends View{

    private Bitmap  mBitmap;
    private Paint   mPaint = new Paint();
    private Canvas  mCanvas = new Canvas();
    private final float MAX_MILISECONDS = 5000f;
    private final float INTERVAL = 4;
    private float maxValue = 0;
    private float minValue = 1023;
    private float maxX;
    private float deltaX;
    private float lastX;
    private float maxY;
    private float deltaY;
    private float lastY;
    private int lineColor = Color.argb(192, 0, 0, 128);


    public ECGView(Context context) {
        super(context);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        mPaint.setColor(lineColor);
        mPaint.setStyle(Style.STROKE);
        //mPaint.setStrokeWidth(0.5f);
        lastX = 0;
        lastY = 0;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        mCanvas.setBitmap(mBitmap);
        mCanvas.drawColor(0xFFFFFFFF);
        maxY = h;
        maxX = w;
        deltaX = maxX/(MAX_MILISECONDS/INTERVAL);
        lastX = maxX;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(mBitmap != null){
            final Canvas cavas = mCanvas;
            if(lastX >= maxX){
                lastX = 0;
                cavas.drawColor(0xFFFFFFFF);                    
            }
            canvas.drawBitmap(mBitmap, 0, 0, null);

        }
        super.onDraw(canvas);
    }

    public synchronized void addData(short[] values){
        if(mBitmap != null){
            final Canvas canvas = mCanvas;
            final Paint paint = mPaint;
            for(short value : values){
                if(value > maxValue) maxValue = value;
                if(value < minValue) minValue = value;

                if(minValue == maxValue)
                    deltaY = maxY/1023.0f;
                else{
                    deltaY =maxY/(maxValue-minValue);
                    value -= minValue;
                }

                final float newX = lastX+deltaX;
                final float newY = deltaY*value;

                canvas.drawLine(lastX, lastY, newX, newY, paint);
                lastX += deltaX;
                invalidate();
            }       
        }

    }



}

我错过了什么吗?

4

1 回答 1

1

您没有更新lastY,因此它将每条线从y=0(顶部)绘制到当前位置。

于 2013-10-31T20:54:12.767 回答