Android 上的 Java 中使用画布和 SurfaceView 的双缓冲

发布于 2024-11-18 04:53:36 字数 312 浏览 5 评论 0原文

如何去做这件事呢?有人能给我一个大纲吗?

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

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

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

How does one go about doing this? Could somebody give me an outline?

From what I've found online, it seems like in my run() function:

  1. create a bitmap
  2. create a canvas and attach it to the bitmap
  3. lockCanvas()
  4. call draw(canvas) and draw bitmap into back buffer (how??)
  5. unlockCanvasAndPost()

Is this correct? If so, could I get a bit of an explanation; what do these steps mean and how do I implement them? I've never programmed for Android before so I'm a real noob. And if it isn't correct, how DO I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-11-25 04:53:36

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

It's already double buffered, that's what the unlockCanvasAndPost() call does. There is no need to create a bitmap.

温馨耳语 2024-11-25 04:53:36

Android 开发者小组中的步骤表明您需要一个缓冲区 -画布,所有渲染都绘制到上面。

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

您将获得主缓冲区,您正在绘制该缓冲区,而无需在每个持有者的锁上获得不同的双缓冲区画布。

The steps from Android Developers Group say that you need a buffer-canvas, to which all the renders are drawn onto.

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

You are getting the main buffer, which you are drawing into without getting different double-buffer canvas' on each holder's lock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文