我惊讶地发现,View.onDraw()在画画之前把画布擦干净了。这不是我想要的。有什么好方法来保留以前的图纸,这样我就只需要在每次调用时在以前的图纸上绘制更改?
发布于 2014-01-27 03:33:20
我实现了一个SurfaceView。然后,在我创建自己的位图和画布,我画进入,绘制到屏幕上,我画位图到屏幕。
例如:
private Canvas myCanvas = null;
private Bitmap myCanvasBitmap = null;
private Matrix identityMatrix;
public mySurfaceCreated(canvasWidth, canvasHeight){
myCanvasBitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
myCanvas = new Canvas();
myCanvas.setBitmap(myCanvasBitmap);
identityMatrix = new Matrix();
}
public void myDraw(Canvas canvas) {
// Draw stuff onto myCanvas, NOT the canvas given by the android OS.
drawStuff(myCanvas);
// Draw onto the canvas given by the android OS.
canvas.drawBitmap(myCanvasBitmap, identityMatrix, null);
}这样,我不需要每次绘制整个画布,而是只需要进行所需的更改。请注意,操作系统创建的画布仍然每次都被完全重新绘制。
发布于 2013-12-16 06:17:31
有几个API可以为视图定义脏的rect以使其失效:
public void invalidate(Rect dirty)
public void invalidate(int l, int t, int r, int b)然而,更有可能的情况是,View会一直被重新绘制,例如,当您滑动它时,或者在它上面的另一个View以Z顺序get的失效。
您可以尝试使用setDrawingCacheEnabled,如果您自己正在绘图,请确保缓存正在绘制的Bitmaps。
https://stackoverflow.com/questions/20604486
复制相似问题