如何绘制游戏在Android上帆布与许多基元 [英] How to draw game on Android Canvas with lots of primitives

查看:105
本文介绍了如何绘制游戏在Android上帆布与许多基元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画了很多线矩形在我的游戏每一帧 - 这是一个老同学手持电子游戏的娱乐。这有原油的点阵显示主游戏和自定义图像文本或一些图像的人。

I draw a lot of lines rectangles each frame in my game - it's a recreation of an old school handheld electronic game. The ones that had crude dot matrix display for the main game and custom images for text or some images.

我有一个虚拟的点阵屏上大20×20像素,我也是在屏幕上画出一些7段显示器和其他一些东西。

I have 20x20 big "pixels" on a virtual dot matrix screen, I also draw some 7-segment displays on the screen and some other things.

据TraceView,大部分时间都花在为的drawLine和DrawRectangle的方法,因为那里实在太高了他们。我吸取上述点阵显示的像素,并在7段显示器的每个部分的每个的框架。

According to TraceView, most of time is spent at drawLine and drawRectangle methods, because there is simply too much of them. I draw each 'pixel' of the dot matrix display, and each segment of 7-segment displays in every frame.

这是优化来到我的脑海,我会重新绘制只有那些像素/段,改变了,所以我有救像素/段的previous状态,并试图重新绘制不仅改变的。基本上这是一个无效原油

An optimization came to my mind, that I would repaint only those pixels/segments which changed, so I have saved the previous state of pixels/segments and tried to repaint only changed ones. Basically it's a crude invalidation.

我所期待的行为是画布会留下,因为它是经过最后​​一帧完成绘画,我只需要重绘的东西。

唉,我的画布是闪烁黑色,每过一段时间,当我的屏幕的状态改变,只是点阵显示的像素/段闪烁上一帧。而且我确信,我并不太清楚我的画布。

Alas, my Canvas was flickering to black and every once in a while when state of my screen changed, only pixels of dot matrix display/segments flickered on for a frame. And I made sure that I didn't Clear my canvas.

我借鉴了SurfaceView内的画布,更新线程,因为月球着陆器的Andr​​oid样品中有描述。在月球着陆的样品,他们重新绘制每一帧的一切。

I'm drawing on a Canvas inside a SurfaceView, updating Thread, as is described in Lunar Lander Android sample. In the Lunar Lander sample, they repaint everything in each frame.

也许我可以使一切都在第一帧位图,然后每隔一帧上更新位图?

Maybe I could render everything to a Bitmap on the first frame, then update that Bitmap on every other frame?

我应该使用哪种技术,我的paint()方法?

推荐答案

SurfaceHolder.unlockCanvasAndPost()实际上做翻页表面上,因此即使previous框架没有清理出去,你会得到第二最古老的框架,你刚才提请之一。并在表面上做锁()实例,每个大概是做结算在其构造的时间一个新的画布。

SurfaceHolder.unlockCanvasAndPost() actually does page flipping on the Surface, so even if the previous frame wasn't cleared out you'd get the 2nd oldest frame, not the one you just drew. and doing lock() on a surface instantiates a new canvas each time which probably is doing the clearing in its constructor.

你最好要present的图像显示每次在画布上绘制成位图像有人建议,然后drawBitmap。您可以从位图AQUIRE帆布画进去。

You're better off drawing into a Bitmap like someone suggested and then drawBitmap on the canvas each time you want to present the image to the display. You can aquire a canvas from a bitmap to draw into it

和喜欢别人建议(很暧昧),可以生成用于点阵显示屏精灵,直接从位图缓存那些画,而然后用简单的命令绘制。即对点阵计算器,你可以生成所有0-9成10位图,只是他们的blit画布而不是调用一组的drawLine /矩形的操作上。

and like someone else suggested (very ambiguously), you can generate sprites for the dot matrix display and draw those directly from a cache of bitmaps rather then draw with primitive commands. ie for a dot matrix calculator you could generate all 0-9 into 10 bitmaps and simply blit them on the canvas rather then calling a set of drawLine/Rect operations.

如果上面仍然太慢还有OpenGL的,这需要硬件加速的优势

if the above are still too slow there still is opengl, which takes advantage of hardware acceleration

这篇关于如何绘制游戏在Android上帆布与许多基元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆