Android - 拥有流畅的精灵动画而不浪费堆的最佳方式
我是一名 Java 程序员,现在正在开发 Android 应用程序。这将是一款动作游戏,所以我需要使用很多精灵。我的动画效果很好,但我在内存管理与性能方面遇到了困难。我在网上看了很多,似乎没有什么可以回答我的问题。 我希望我的精灵能够顺利运行,但又不想浪费太多堆。到目前为止,我尝试过以下操作:
- 将一个大文件,其中包含所有动画(步行、跑步、跳跃等)加载到堆中:从一个动画切换到另一个动画以及从一帧切换到另一帧的速度非常快,但是最大的问题是堆大小被使用。我们必须将整个图像文件以位图形式加载到堆中,这根本没有效率(单个文件很容易达到 10Mb)。如果我们只在屏幕上绘制一个小框,显然不需要将整个图像加载到 RAM 中?
-为每个动画提供单独的文件(一个用于行走,一个用于跑步等),并在需要时一次加载一个文件:它在大小上相对有效,因此我们不会浪费堆空间。加载给定动画也非常流畅,因为整个动画都加载在堆中。最大的问题是从一种动画更改为另一种动画时(例如从跑步到跳跃)。存在很大的滞后,因为每次需要更改动画时,我们都需要解码(bitmapfactory.decoderesources)一个新文件。即使只有 500 毫秒,对于游戏来说也太慢了。
问题:在不将巨大文件加载到堆中的情况下,获得流畅动画的最佳方法是什么?
谢谢
I am a java programmer working on an Android app right now. It will be an action game, so I need to use a lot of sprites. My animations work great, but I am struggling with memory management vs performance. I read a lot online, and nothing seems to answer my issue. I want my sprites to run smoothly, but don't want to waste too much heap. Here's what I tried so far:
-loading one big file, with all animations in it (walk, run, jump, etc.) into the heap: It is very fast to switch from one animation to another and from frame to frame, but the big problem is the heap size is uses. We have to load the whole image file into the heap, in bitmap, which is not efficient at all (a single file can easily go up to 10Mb). We clearly don't need to load the whole image into the RAM if we only draw one small frame on the screen?
-having separate files for each animation (one for walking, one for running, etc), and loading one file at a time, when required: It is relatively efficient in size, so we don't waste heap space. It is also very fluid in loading the given animation, since the whole animation is loaded in the heap. The big issue is when changing from one animation to another (from run to jump for instance). There is a big lag, since we need to decode (bitmapfactory.decoderesources) a new file every time we need to change animation. even if it's only 500ms, it is way too slow for a game.
Question: What would be the best way for having fluent animations, without loading huge files into the heap?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论