Canvas.drawLine像一个形状一样填充

时间:2013-10-31 20:41:27

标签: android canvas

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

ecg graph

这是绘制图形的类:

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

    }



}

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

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