如何从url Codenameone创建一个Image [英] How to create an Image from a url Codenameone

查看:93
本文介绍了如何从url Codenameone创建一个Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从包含图像网址的字符串中创建一个新的Image实例。

例如 http://maps.gstatic.com/mapfiles/place_api/icons/restaurant -71.png

I need to make a new Image instance from a string that has the url of the image.
E.g. http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png

这种类型的网址来自JSONParser,我想用它们来填充多按钮列表的图标。

This type of urls come from a JSONParser and I want to use them to populate the icons of a multibutton list.

我使用此Model类的以下 getItemAt 方法将数据放在多按钮列表中。每个多按钮都有一个图标和一些线条。我将Line1命名为 name ,将Line2命名为 rating 。我希望从字符串url创建一个Image,这样我就可以在h.put(icon,defaultIcon)行中更改多重按钮图标来代替defaultIcon。

I use the following getItemAt method of this Model class put data on a multibutton list. Each multibutton has an icon and some lines. I have named Line1 as name and Line2 as rating. I want from the string url to make an Image so I can change the multibutton icon in place of defaultIcon in h.put("icon", defaultIcon) line.

static class Model implements ListModel {
    public Object getItemAt(int index) {
        Hashtable h      = new Hashtable();
        Hashtable entry  = (Hashtable) results.elementAt(index);
        String    name   = (String) entry.get("name");
        Double    rating = (Double) entry.get("rating");
        String    url    = (String) entry.get("icon");

        h.put("name", name);
        h.put("icon", defaultIcon); // change defaultIcon with an Image from url String

        if (rating == null) {
            h.put("rating", "Not rated");
        } else {
            h.put("rating", "Rating: " + rating.toString());
        }

        return h;
    }


推荐答案

使用 ImageDownloadService.createImageToStorage 将异步下载图像,将其放在Hashtable中的列表中的正确位置。刷新列表并将图像缓存在存储中以备再次请求。

Use ImageDownloadService.createImageToStorage which will asynchronously download the image, place it in the Hashtable for the list in the proper location. Refresh the list and cache the image in storage in case you ask for it again.

它还可以将图像缩放到固定尺寸作为服务的一部分。

It can also scale your image to a fixed dimension as part of the service.

这篇关于如何从url Codenameone创建一个Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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