每次重绘SurfaceView时,先前绘制的内容都会被删除。如何保存状态以便我的循环将新对象添加到屏幕而不删除旧对象?
答案 0 :(得分:2)
使用Bitmap
:
Bitmap mDrawBitmap;
Canvas mBitmapCanvas;
Paint mDrawPaint = new Paint();
@Override
public void onDraw(Canvas canvas) {
if (mDrawBitmap == null) {
mDrawBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
mBitmapCanvas = new Canvas(mDrawBitmap);
}
// draw on the btimapCanvas
//... and more
mBitmapCanvas.drawWhatever(...);
// after drawing with the bitmapcanvas,
//all drawn information is stored in the Bitmap
// draw everything to the screen
canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
}
以这种方式," mDrawBitmap"将始终包含当前绘制的状态。如果您愿意,可以致电mBitmapCanvas.drawColor(Color.WHITE);