Android 画布重绘速度为 30fps
我目前正在尝试在我的 Android 设备上实现简单的弹跳球(二维)(基于设备旋转和稍后进入的多个球的物理原理)。
我创建了一个 CustomDrawView
类,它使用 onDraw
函数扩展了 View
,该函数在屏幕中心绘制一个圆。让它正确移动的数学是无关紧要的,因为我的问题是我应该在 CustomDrawView 上调用 invalidate
。
- 我应该创建一个新线程来根据时间处理这个问题吗?如 Thread.sleep(1000/30); view.invalidate (对于 30fps)
- 我应该让它基于其他方法在同一个线程中运行吗?
当然电池也是一个重要因素,哪种方法最能节省电池呢?
Im currently trying to implement the simple bouncing ball (in 2d) on my android device (physics based on device rotation and multiple balls coming in at a later point).
I have created a CustomDrawView
class that extends View
with a onDraw
function that draws a circle in the centre of the screen. The math to get it moving correctly around is irrelevent, as my question is where i should call invalidate
on the CustomDrawView.
- Should i make a new thread to handle this based on time? as in
Thread.sleep(1000/30); view.invalidate
(for 30fps) - Should i make it run in the same thread based on some other method?
Ofcourse battery is a important factor aswell, which method is best for saving battery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我所看到的 Android 视图来看,如果您计划制作任何帧率非常重要的东西,它们并不是最好的。它们滚雪球的速度非常快,并且用于绘制它们的消息传递管道通常是程序在该帧中执行的最慢的事情。我现在只需将渲染移至 OpenGL 即可。使用 SurfaceView,您仍然可以非常快速地进行设置(好吧,SurfaceView 也不是很好,但比所有视图都要好得多)。
如果我要使用视图,我会使用你的第一个方法并设置一个计时线程,使用它有点像标准游戏循环,执行更新然后触发绘制然后等待以确保完整的 1/30 秒通过并循环。
From what I have seen with Android views they aren't the best if you are planning on making anything with framerate is very important. They snowball very quickly and the message passing pipeline used draw them is often the slowest thing your program will do that frame. I would just move the rendering now into OpenGL. Using the SurfaceView you can still get setup very quickly (ok SurfaceView also isn't great but much better than all Views).
If I were to use views, I would use your first method and setup a timing thread, use it sort of a like a standard Game loop that executes an update then triggers a draw then waits to ensure a full 1/30th of a second has passed and loops.