如何调节 Android 应用程序的帧速率?

发布于 2024-09-30 16:04:19 字数 79 浏览 1 评论 0原文

如何调节 Android 应用程序的帧速率?我希望游戏以恒定速度运行。我的应用程序不需要高帧速率,因此我不需要高帧速率,因为这会占用更多的电池。

How can I regulate the framerate in my android app? I would like the game to run at a constant speed. My app doesn't require a high framerate so I don't want one because that would take up more battery then necessary.

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

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

发布评论

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

评论(1

栀子花开つ 2024-10-07 16:04:19

不要使用帧速率来衡量时间。用时间来衡量时间。 GC 传递可能需要 3/10 秒,其他任务可能会在后台启动,等等。

总有一个系统/设置的运行速度比您想象的要慢。

然后,您不以像素为单位指定速度,而是以像素/秒为单位。动画的每一帧都需要一定的时间,等等。在你的游戏引擎中,当计算下一帧时,你的输入之一是“自上一帧以来已经过去了多少时间”。您将该值确定为秒的一小部分,并将每秒的速度乘以该分数。结果是给定事物自上一帧以来移动了多远。

请注意,非常慢的帧速率可能会对碰撞检测造成严重破坏,尤其是对于快速移动的物体。如果物体 1 在帧之间完全穿过物体 2,仅检查 BBox 或半径并不能解决问题。

话虽如此, sleep() 是你的朋友。在帧处理开始时,调用 System.currentTimeMilis()。在帧处理(包括渲染)结束时,再次检查当前时间。如果差异不够长,请睡眠(N)足够的时间以匹配您所需的帧速率。

因此,如果您想要最大 20fps,则每帧应花费 50ms (1000ms / 20 = 50ms)。如果一帧只花了 10 毫秒来模拟和渲染,那么您需要再休眠 (50 毫秒 - 10 毫秒 = ) 40 毫秒,然后才能进入下一帧。

或者,您可以尽可能快地继续运行模拟,并且仅每隔一段时间渲染屏幕。这不会对电池寿命有太大帮助(尽管如果我的 Evo 发出的热量有任何迹象的话,OpenGL 硬件加速会很昂贵),但可以带来非常流畅的体验。哎呀,你可以在那时开始计算运动模糊之类的东西。

Don't use frame rate to measure time. Use time to measure time. A GC pass can take 3/10 of a second, other tasks can fire up in the background, etc etc.

There's always a system/setup that will run slower than you thought possible.

Then you don't specify velocities in pixels, but in pixels/second. Each frame of animation takes a certain amount of time, etc etc. In your game engine, when computing the next frame, one of your inputs is "how much time has passed since the last frame". You determine that value as a fraction of a second, and multiply your velocity/sec by that fraction. The result is how far a Given Thing has moved since the last frame.

Note that really slow frame rates can wreak havoc on collision detection, particularly with fast moving objects. If Thing 1 passes completely through Thing 2 between frames, just checking BBoxes or radii isn't going to cut it.

Having said all that, sleep() is your friend. At the start of a frame's processing, call System.currentTimeMilis(). At the end of a frame's processing (including rendering), check the current time again. If the difference isn't long enough, sleep(N) for enough time to match your desired frame rate.

So if you want 20fps max, then each frame should take 50ms (1000ms / 20 = 50ms). If a frame only took 10ms to simulate and render, then you need to sleep for another (50ms - 10ms = ) 40ms before moving on to the next frame.

Alternatively, you can keep running the simulation as fast as possible, and only render the screen every so often. This won't help battery life much (though OpenGL hardware acceleration is expensive if the heat coming off my Evo is any indication), but can make for a Very Smooth experience. Heck, you can start calculating things like motion blur at that point.

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