在 J2ME 中加载图像?

发布于 2024-07-22 01:48:48 字数 338 浏览 6 评论 0原文

我对 J2ME 上的概念并不陌生,但我有点懒惰,但我不应该这样做: 最近,我的应用程序一直将图像加载到内存中,因为它们是糖果......

Sprite example = new Sprite(Image.createImage("/images/example.png"), w, h);

我不太确定这是最好的方法,但它在我的摩托罗拉 Z6 上运行良好,直到昨晚,当我在旧三星中测试该应用程序时手机和图像甚至无法加载,需要多次尝试启动线程才能显示。 屏幕保持白色,所以我意识到这一定是关于图像加载的问题,我做得不太好......有谁可以告诉我如何在我的应用程序中正确地创建加载例程?

I'm not so new to the concepts present on J2ME, but I'm sort of lazy in ways I shouldn't:
Lately my app has been loading images into memory as they were candy...

Sprite example = new Sprite(Image.createImage("/images/example.png"), w, h);

and I'm not really sure it's the best way, but it worked fine in my Motorola Z6, until last night, when I tested the app in a old Samsung cellphone and the images wont even load and require several attemps of starting the thread to show up. The screen was left on white, so I realized that it has to be something about Image loading that I'm not doing quite well... Is there anyone who can tell me how to properly make a loading routine in my app?.

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

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

发布评论

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

评论(3

誰認得朕 2024-07-29 01:48:48

我不确定您到底在寻找什么,但您描述的行为听起来很像您正在经历 OutOfMemory 异常。 尝试减小图像的尺寸(堆使用情况基于尺寸)并查看该行为是否停止。 这会让您知道这是否确实是内存不足问题或其他问题。

其他提示:

  1. 从大到小加载图像。 这有助于处理堆碎片,并为最大的图像提供最大的堆空间。
  2. 按照与加载方式相反的顺序卸载(设置为 null),然后进行垃圾收集。 确保在调用 GC 之后调用 Thread.yield()。
  3. 确保只加载您需要的图像。 从应用程序不再处于的状态卸载图像。
  4. 由于您正在创建精灵,因此一张图像可能有多个精灵。 考虑创建一个图像池以确保仅加载图像一次。 然后只需将每个 Sprite 对象指向它所需的池中的图像即可。 您问题中的示例似乎您很可能会多次将同一图像加载到内存中。 这很浪费,并且可能是内存不足问题的一部分。

I'm not sure exactly what you are looking for, but the behavior you describe very much sounds like you are experiencing an OutOfMemory exception. Try reducing the dimensions of your images (heap usage is based on dimension) and see if the behavior ceases. This will let you know if it is truly an OutOfMemory issue or something else.

Other tips:

  1. Load images largest to smallest. This helps with heap fragmentation and allows the largest heap space for the largest images.
  2. Unload (set to null) in reverse order of how you loaded and garbage collect after doing so. Make sure to Thread.yield() after you call the GC.
  3. Make sure you only load the images that you need. Unload images from a state that the application is no longer in.
  4. Since you are creating sprites you may have multiple sprites for one image. Consider creating an image pool to make sure you only load the image once. Then just point each Sprite object to the image within the pool that it requires. Your example in your question seems like you would more than likely load the same image into memory more than once. That's wasteful and could be part of the OutOfMemory issue.
寄人书 2024-07-29 01:48:48

使用电影图像(在一个图像中按定义尺寸排列的一组图像)并使用逻辑一次将它们拉出来。

因为它们组合成一个图像,所以您可以节省每个图像的标头空间,从而减少使用的内存。

该技术首先用于 MIDP 1.0 内存受限设备。

Using a film image(a set of images by a defined dimension in one image) and use logic to pull them out one at a time.

Because they a grouped into one image, you are saving header space per image and thus can reduce the memory used.

This techniques was first used in MIDP 1.0 memory constrained devices.

浮光之海 2024-07-29 01:48:48

使用 Fostah 不反复加载图像的方法,我创建了以下类:

public class ImageLoader {
    private static Hashtable pool = new Hashtable();

    public static Image getSprite(String source){
        if(pool.get(source) != null) return (Image) pool.get(source);
        try {
            Image temp = Image.createImage(source);
            pool.put(source, temp);
            return temp;
        } catch (IOException e){
            System.err.println("Error al cargar la imagen en "+source+": "+e.getMessage());
        }
        return null;
    }
}

因此,每当我需要图像时,我首先向池请求它,或者只是将其加载到池中。

Using the Fostah approach of not loading images over and over, I made the following class:

public class ImageLoader {
    private static Hashtable pool = new Hashtable();

    public static Image getSprite(String source){
        if(pool.get(source) != null) return (Image) pool.get(source);
        try {
            Image temp = Image.createImage(source);
            pool.put(source, temp);
            return temp;
        } catch (IOException e){
            System.err.println("Error al cargar la imagen en "+source+": "+e.getMessage());
        }
        return null;
    }
}

So, whenever I need an image I first ask the pool for it, or just load it into the pool.

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