0

我有一个LinearLayout背景颜色设置为黑色。在此LinearLayout,我有一个我View在其中使用Canvas. 因为onDraw()方法会被多次调用,所以我想在调用onDraw()方法时清除之前绘制的内容,因此我使用Canvas.drawColor(Color.BLACK)清除画布。

但是我得到的是一个没有任何东西的黑屏,即使我画了一些新的东西。Canvas.drawColor(Color.BLACK)在添加onDraw()方法之前,我已经可以绘制一些东西了。

编辑:我的onDraw()方法代码

String value = "";
static Bitmap bitmap;
static Canvas canvas;
public void init(){// this is called by constructor method
  this.setWillNotDraw(false);
  bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  canvas = new Canvas();
  canvas.setBitmap(bitmap);
}
public void onDraw(Canvas canvas){
  canvas.drawBitmap(bitmap, 0, 0, null);
  drawGrid();
}
public void drawGrid(){
  Paint paint = new Paint();
  paint.setColor(Color.GRAY);
  paint.setStrokeWidth(1);
  canvas.drawText(value, somex, somey, paint);
}
public void changeData(String value){
  this.value = value;
  this.postInvalidate();
}

另一个问题,我打电话的正确地点在哪里Canvas.drawColor(Color.BLACK)

4

2 回答 2

0

我使用了下面的代码,它对我有用。清除画布上的绘图。

public void resetBitmapCanvasAndPath() {
    // TODO Auto-generated method stub
    mDrawingUtilities.mBitmap = Bitmap.createBitmap(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT ,
            Bitmap.Config.ARGB_8888);
    mDrawingUtilities.mCanvas = new Canvas(mDrawingUtilities.mBitmap);
    mDrawingUtilities.mPath = new Path();
} 

这是与您的问题类似的链接,并查看已接受的答案。 如何清除手指上的油漆?

于 2012-11-30T14:38:22.767 回答
-2

使用Color.TRANSPARENT而不是使用Color.BLACK

于 2012-11-30T14:38:03.530 回答