http://stackoverflow.com/questions/5729377/android-canvas-how-do-i-clear-delete-contents-of-a-canvas-bitmaps-living的answer2中提到的测试代码,运行的结果,表明有两块buffer被交错使用,n和n+2的canvas绘制是持续的,而n和n+1之间却不是持续的。这是一个很奇怪的特性。
看了http://groups.google.com/group/android-developers/msg/8d1243c33f9b7b6e?pli=1的解释,就明白了每一个surface的确是交错使用两块buffer的。所以,每次绘制canvas的时候,都要重绘每一个pixel。
提到的解决方法是:
- create a bitmap
- attach a canvas to it
- do the rendering into that canvas
- lockCanvas
- draw your bitmap into the backbuffer
- unlockAndPost
具体的代码是:
mBitmap = Bitmap.createBitmap(mSv.getWidth(), mSv.getHeight(), Bitmap.Config.RGB_565);
mWorkingCanvas = new Canvas(mBitmap);
...
mWorkingCanvas.drawCircle(mX, mY, RADIUS, mPaint);
Canvas canvas = mSh.lockCanvas();
canvas.drawBitmap(mBitmap, 0, 0, null);
mSh.unlockCanvasAndPost(canvas);