从 spritesheet 加载大量图像

发布于 2024-10-21 22:13:10 字数 689 浏览 10 评论 0原文

我正在尝试为 Android 制作一个简单的配对游戏。

程序结构:

Menu.java(最初加载的菜单活动)

Game.java(游戏活动,由菜单启动)
GameThread.java(处理游戏循环,在 GameView 中调用渲染进程)
GameView.java(处理屏幕上的所有绘图)

Graphics.java(存储加载的图像)

问题:

游戏具有 15 种不同类型的卡片,每种卡片需要大约 14 帧的动画(翻转、毁坏等)。我目前正在从 PNG spritesheet 中读取这些内容,然后使用以下代码将它们切入位图数组(Bitmap[15][14]):

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);
    }
}

当我最初加载 GameView 时出现问题,需要加载卡片图形,这似乎需要大约 2 秒的时间来处理(导致应用程序无响应)。

我有更好的方法可以做到这一点吗?

提前感谢您的帮助。

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.

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

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

发布评论

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

评论(1

与之呼应 2024-10-28 22:13:10

如果问题确实是您的应用程序没有响应,我建议通过线程加载图像,同时向用户显示不确定的对话框。这通常是通过 Thread处理程序。网上有很多关于如何执行此操作的示例。

然而,如果问题是内存问题,您将不得不找出一种不同的方法来处理所有图像。这可能是一次仅加载一个动画、调整位图大小或修复内存问题。如果没有看到错误和/或更多代码,就无法判断。

最后,我从你的逻辑中看出,内存中的位图可能多于所需的位图。如果没有看到更多的逻辑,我再次无法确定。

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.

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