如何在android中的onDraw方法中存储画布?

时间:2011-11-08 11:29:59

标签: android android-canvas

我正在尝试在android中的onDraw方法中保存和恢复画布,但是使用此代码无法正常工作我发现只有最后一行显示以前没有被淹没 所以请告诉我。我的代码如下。

public void setMData(Float pData, Float cData, int i){


    this.pData = pData;
    this.cData = cData;
    this.i = i;

}

public void onDraw(Canvas canvas)

 {


    super.onDraw(canvas);
    canvas.restore();
    hs = getHeight();
    ws = getWidth();
    paint.setTextSize(15);
    paint.setColor(Color.parseColor("#00FFCC"));
    paint.setStrokeWidth(2);
    ox = 30;
    oy = hs - 30;
    canvas.drawLine(30, 30, 30, hs - 30, paint); // Vertical Line
    canvas.drawLine(30, hs - 30, ws - 30, hs - 30, paint); // Horizontal
    canvas.drawText("(0,0)", 10, hs - 10, paint);
    float secWidth = 20;// (ws-30)/mData.size();
    float pRatio = oy / 100 * pData;
    float cRatio = oy / 100 * cData;
    canvas.drawLine(secWidth * i, hs - pRatio, secWidth * (i + 1), hs - cRatio,paint);
    canvas.drawText("" + cData, secWidth * (i + 1), hs - cRatio, paint);
    canvas.save();
}

1 个答案:

答案 0 :(得分:0)

试试这个:

int saveCount; //declare this as a field

saveCount = canvas.save();
.....
canvas.restoreToCount(saveCount);