7

如何做到这一点?谁能给我一个大纲?

从我在网上找到的内容来看,它似乎在我的 run() 函数中:

  1. 创建位图
  2. 创建一个画布并将其附加到位图
  3. 锁定画布()
  4. 调用 draw(canvas) 并将位图绘制到后台缓冲区(如何??)
  5. 解锁CanvasAndPost()

它是否正确?如果是这样,我能得到一点解释吗?这些步骤是什么意思,我该如何实施?我以前从未为 Android 编程过,所以我是一个真正的菜鸟。如果它不正确,我该怎么做?

4

2 回答 2

17

它已经是双缓冲的,这就是 unlockCanvasAndPost() 调用的作用。无需创建位图。

于 2011-06-30T17:20:31.157 回答
2

Android Developers Group的步骤说您需要一个缓冲区画布,所有的渲染都被绘制到该画布上。

Bitmap buffCanvasBitmap;
Canvas buffCanvas;

// Creating bitmap with attaching it to the buffer-canvas, it means that all the changes // done with the canvas are captured into the attached bitmap
tempCanvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
tempCanvas = new Canvas();
tempCanvas.setBitmap(tempCanvasBitmap);

// and then you lock main canvas
canvas = getHolder().lockCanvas();              
// draw everything you need into the buffer
tempCanvas.drawRect.... // and etc
// then you draw the attached bitmap into the main canvas
canvas.drawBitmap(tempCanvasBitmap, 0, 0, drawView.getPaint());
// then unlocking canvas to let it be drawn with main mechanisms
getHolder().unlockCanvasAndPost(canvas);

您正在获取主缓冲区,您正在绘制该缓冲区,而不会在每个持有人的锁上获得不同的双缓冲画布。

于 2013-03-04T03:36:21.463 回答