绘制新画布是否会删除旧画布上的内容?

时间:2016-08-03 05:59:11

标签: java android image canvas bitmap

见标题

如果我在画布上有drawCircle但是通过下面的代码不断创建一个新画布,那么旧的Circle会消失吗?我知道它可能效率不高但我很好奇。

ImageView background = (ImageView) findViewById(R.id.Background);
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
canvas.drawCircle(point0,point1,10,paint);
background.setImageBitmap(bitmap)

由于

1 个答案:

答案 0 :(得分:2)

每次更改背景图片时,您都不需要创建新的Canvas。 只需使用现有的画布并在其上设置位图

// Have your variables like this
ImageView background;
Bitmap bitmap;
Canvas canvas;

// Associate them to the respective views
background = (ImageView) findViewById(R.id.Background);
bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);

// instead of creating a new canvas, just update the already existing with your bitmap
canvas.setBitmap(bitmap);
canvas.drawCircle(point0,point1,10,paint);
background.setImageBitmap(bitmap)

更新感谢@Doomsknight 的评论 如果你想清除画布,你可以简单地做:

canvas.drawColor(Color.White);

它将删除所有绘制的内容