安卓:二维。OpenGL 还是 android.graphics?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2755446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:14:19  来源:igfitidea点击:

Android: 2D. OpenGl or android.graphics?

androidanimationopengl-es2d

提问by Bostone

I'm working with my friend on our first Android game. Basic idea is that every frame of the game the whole surface is redrawn (1 large bitmap) in 2 steps:

我正在和我的朋友一起开发我们的第一个 Android 游戏。基本思想是游戏的每一帧整个表面都分两步重绘(1个大位图):

  1. Background with some static image (PNG) wipes out previous frame
  2. Then it is sprinkled all over with large number of particles which produces effect of soapy bubbles where there's a pool of about 20 bitmaps which randomly gets picked to produce illusion that all bubbles (between 200 - 300) are all different. Bubbles positions updated on each frame (~50ms) producing effect of moving bubbles.
  1. 带有一些静态图像 (PNG) 的背景会清除前一帧
  2. 然后撒上大量粒子,产生肥皂泡的效果,其中有大约 20 个位图的池,随机选取这些位图以产生所有气泡(200 - 300 之间)都不同的错觉。气泡位置在每帧(~50 毫秒)上更新,产生移动气泡的效果。

The math engine is in C (JNI) and currently all drawing is done using android.graphics package very similar (since that was the example I was using) to Lunar Lander. It works but animation is somewhat jerky and I can feel by temperature of my phone that it is very busy. Will we benefit from switching to OpenGL? And as a bonus question: what would be a good way to optimize the drawing mechanism (Lunar Lander like) we have now?

数学引擎是在 C (JNI) 中,目前所有的绘图都是使用与 Lunar Lander 非常相似的 android.graphics 包完成的(因为这是我使用的示例)。它可以工作,但动画有点生涩,我可以通过手机的温度感觉到它很忙。我们会从切换到 OpenGL 中受益吗?还有一个额外的问题:优化我们现在拥有的绘图机制(例如 Lunar Lander)的好方法是什么?

回答by Simon

Now I've started to work with OpenGL ES, I would also use it for 2D graphics. This way is the most flexible and it's extremely fast (look at this example code. It's about 2D rendering, and there you can see the power of OpenGL.

现在我已经开始使用 OpenGL ES,我也会将它用于 2D 图形。这种方式是最灵活的,而且速度非常快(看这个示例代码。它是关于 2D 渲染的,在那里你可以看到 OpenGL 的威力。

It's not the easiest thing to start with, but there are some good tutorials out there - for example, thisis a very good one.

这不是最容易开始的事情,但是有一些很好的教程 - 例如,是一个非常好的教程。

回答by Tyler Mercer

Don't redraw the entire screen each time. That's what causes your low framerate. Use the invalidate method to mark the areas that have changed each frame.

不要每次都重绘整个屏幕。这就是导致您的低帧率的原因。使用 invalidate 方法标记每帧发生变化的区域。