LWUIT:在后台线程中加载图像 [英] LWUIT: Load images in background thread

查看:76
本文介绍了LWUIT:在后台线程中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含约20个图像URL和其他一些东西.

I have a list that contains about 20 image URLs and some other things.

我想显示其他内容(描述),并允许用户在加载20张图像的同时与该应用进行交互.

I want to display the other things (description) and allow the user to interact with the app while I load the 20 images.

我注意到的是,无论我尝试了什么,即使我正在另一个线程中进行加载,在图像完成加载之前,我都无法与表单进行交互.

What I noticed is that no matter what I tried, I can't interact with the form until the images finished loading even though I am doing the loading in another thread.

这是我现在正在使用的解决方案.

This is my solution I am using now.

private Container createServerItems() throws Exception {
    Container list = new Container(new BoxLayout(BoxLayout.Y_AXIS));

    final int size = mediaList.size();

    final Button buttons[] = new Button[size];

    System.out.println("In here: " + size);
    for (int i = 0; i < size; i++) {
        Container mainContainer = new Container(new BorderLayout());
        Media m = new Media();
        m.fromJSONString(mediaList.elementAt(i).toString());

        buttons[i] = new Button("please wait");

        final int whichButton = i;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                try {
                    System.out.println(MStrings.replaceAll(m.getImgURL(), "\"", ""));
                    final StreamConnection streamConnection = (StreamConnection) Connector.open(MStrings.replaceAll(m.getImgURL(), "\"", ""));                        
                    Image image = Image.createImage(streamConnection.openInputStream());
                    streamConnection.close();

                    buttons[whichButton].setText("");
                    buttons[whichButton].setIcon(image.scaled(32, 32));

                } catch (Exception e) {
                }
            }
        });
        TextArea t = new TextArea(m.getDesc());
        t.setEditable(false);
        t.setFocusable(false);
        t.setGrowByContent(true);

        mainContainer.addComponent(BorderLayout.WEST, buttons[i]);
        mainContainer.addComponent(BorderLayout.CENTER, t);

        list.addComponent(mainContainer);
    }
    return list;
}

推荐答案

APPROACH I: LWUIT 1.5 有一个功能强大的 LWUIT4IO 库可以解决你的问题.

APPROACH I : LWUIT 1.5 has a powerful LWUIT4IO library to address your problem.

摘录自 Shai的博客链接

An excerpt from Shai's Blog link

LWUIT4IO 中我没有给予足够关注的一项功能是 缓存映射,其有效地是精益哈希表,用于存储其数据 使用弱/软引用(取决于平台)并回退 当没有足够的可用内存时将其存储.这是一个很棒的方法 高速缓存数据而不会落水.关于它的一件很酷的事情是 我们无缝地将其用于存储抽象的事实( 隐藏RMS或等效服务)实际上可以提供对以下内容的更快访问 RMS存储通常在设备上很慢.

A feature in LWUIT4IO to which I didn't give enough spotlight is the cache map, its effectively a lean hashtable which stores its data using weak/soft references (depending on the platform) and falls back to storage when not enough memory is available. Its a great way to cache data without going overboard. One of the cool things about it is the fact that we use it seamlessly for our storage abstraction (which hides RMS or equivalent services) in effect providing faster access to RMS storage which is often slow on devices.

另一个有用的链接是此处

这个想法是将网络IO功能委托给一个单例,以避免任何UI死锁,就像您面临的那样.

The idea is to delegate the Network IO functionality to a singleton to avoid any UI deadlocks, like the one you are facing.

vprise的一个非常好的视频演示此处,说明了如何将GUI功能绑定到您的netbeans.在此视频的7:00分钟左右,它说明了ImageDownloadService类的用法,该类将组件绑定到其缩略图URL,该URL将无缝地从网络获取并填充图像.

A very good video demo here by vprise, explains how to bind GUI functionality to your netbeans. In this video at around 7:00 mins it explains the use of ImageDownloadService class which binds the component to its thumbnail url which will seamlessly fetch from the network and populate the Image.

APPROACH II:难以创建自定义逻辑

  1. 创建一个将与网络交互的单例,以获取 数据
  2. 使用队列来处理顺序图像下载服务
  3. 为此单例创建新线程并等待队列.
  4. 对于每个图像下载服务,调用都将侦听器绑定 组件,以便更轻松地更新正确的组件.
  1. Create a singleton that will interface with the network to fetch the data
  2. Use a queue to handle the sequential image download services
  3. Create a new thread for this singleton and wait on the queue.
  4. With each image download service bind a listener with the invoking component so that it easier to update the right component.

这篇关于LWUIT:在后台线程中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆