如何保存重绘时先前绘制到画布上的对象? [英] How to save objects previously drawn to the Canvas on a redraw?
本文介绍了如何保存重绘时先前绘制到画布上的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次重新绘制SurfaceView时,都会删除以前绘制的内容.如何保存它们的状态,以便我的循环将新对象添加到屏幕上而不删除旧对象?
Every time a SurfaceView is redrawn, things that were previously drawn are erased. How do I save their state so that my loop will add new objects to the screen without erasing the old ones?
推荐答案
使用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);
In that way, "mDrawBitmap" will always contain the currently drawn state. If you want to, you can clear it by calling mBitmapCanvas.drawColor(Color.WHITE);
这篇关于如何保存重绘时先前绘制到画布上的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文