如何在重绘时保存先前绘制到Canvas的对象?

时间:2014-06-23 23:24:08

标签: java android graphics android-canvas android-bitmap

每次重绘SurfaceView时,先前绘制的内容都会被删除。如何保存状态以便我的循环将新对象添加到屏幕而不删除旧对象?

1 个答案:

答案 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);

清除它