Loading a large number of images from a spritesheet

我与影子孤独终老i 提交于 2020-01-23 17:17:46

问题


I'm attempting to make a simple game of Pairs for Android.

Program Structure:

Menu.java (Menu activity initially loaded)

Game.java (Game activity, started by Menu)
GameThread.java (Handles gameloop, calls render process in GameView)
GameView.java (Handles all drawing to the screen)

Graphics.java (Stores loaded images)

The Problem:

The game features 15 different types of card, each of which requires around 14 frames for animation (flipping, destroying, etc). I'm currently reading these off a PNG spritesheet, and then chopping them into a Bitmap array (Bitmap[15][14]) using the following code:

for (int i=0; i<15; i++) {
    for (int j=0; j<14; j++) {
        card[i][j] = Bitmap.createBitmap(spriteSheet,
            j*cardWidth, i*cardHeight, cardWidth, cardHeight);
    }
}

The problem arises when I initially load the GameView, the card graphics need to be loaded, which seems to take around 2 seconds to process (resulting in an unresponsive app).

Is there a better way I can do this?

Thanks for your help in advance.


回答1:


If the problem is indeed that your app is unresponsive, I would recommend loading the images via a thread while showing the user an indeterminate dialog box. This is generally done via a Thread and a Handler. There are plenty of examples of how to do this online.

If however the problem is that of memory your going to have to figure out a different way to handle all the images. That could be loading only one animation at a time, resizing your bitmaps, or fixing a memory problem. There is no way to tell without seeing the errors and/or more code.

Finally, I see from your logic that you may have more Bitmaps in memory than necessarily are required. Once again I can't be certain without seeing more logic.



来源:https://stackoverflow.com/questions/5294016/loading-a-large-number-of-images-from-a-spritesheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!