【问题标题】:How to use ImageLoad using Android Universal Image Loader如何使用 Android Universal Image Loader 使用 ImageLoad
【发布时间】:2013-11-05 14:37:50
【问题描述】:

关于我以前的HERE 的主题,我已经设法在我的列表视图中显示图像。原来在显示图片的过程中没有使用async,感觉listview很重。然后我尝试使用库Android Universal Image Loader。在我的库使用中尝试组合和替换我以前使用的 ImageLoader 库时仍然有点困惑。

我在我的活动和适配器中输入了onCreate

File cacheDir = StorageUtils.getCacheDirectory(this.getApplicationContext());
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.getApplicationContext())
            .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
            .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
            .threadPoolSize(3) // default
            .threadPriority(Thread.NORM_PRIORITY - 1) // default
            .tasksProcessingOrder(QueueProcessingType.FIFO) // default
            .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
            .memoryCacheSize(2 * 1024 * 1024)
            .memoryCacheSizePercentage(13) // default
            .discCache(new UnlimitedDiscCache(cacheDir)) // default
            .discCacheSize(50 * 1024 * 1024)
            .discCacheFileCount(100)
            .imageDownloader(new BaseImageDownloader(this.getApplicationContext())) // default
            .build();
        ImageLoader.getInstance().init(config);

在我的循环中(onPostExecute):

for (int i = 0; i < allData.length(); i++) {

    JSONObject c = allData.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(Constants.TAG_MenuID);
    String title = c.getString(Constants.TAG_Title);
    String post = c.getString(Constants.TAG_Post);
    String image = c.getString(Constants.TAG_Image);

    BeritaBean mb = new BeritaBean();
    mb.setID(id);
    mb.setTitle(title);
    mb.setPost(post);
    mb.setImg(image);

    AmbilDataBean.add(mb);
}
adapter.setItem(AmbilDataBean);

然后我也更改了我的 previous 适配器(使用ImageLoader 类):

public View getView(int position, View convertView, ViewGroup parent) {

        View v = inflater.inflate(R.layout.list_row, null);

        ImageView img       = (ImageView) v.findViewById(R.id.image);
        TextView judul      = (TextView) v.findViewById(R.id.judul);
        TextView tanggal    = (TextView) v.findViewById(R.id.tanggal);
        TextView isi        = (TextView) v.findViewById(R.id.isi);

        BeritaBean obj      = (BeritaBean) getItem(position);

        judul.setText(obj.getTitle());
        tanggal.setText(obj.getCreated());
        isi.setText(obj.getPost());

        try {
            String urlImage = obj.getImg();
            if(urlImage.length() > 0){
                img.setTag(urlImage);
                imageLoader.DisplayImage(urlImage, img);
            }
        } catch (Exception e) {

        }
        return v;
    }

成为(使用Universal Image Loader)的一部分:

try {
        String urlImage = obj.getImg();
        if(urlImage.length() > 0){
            img.setTag(urlImage);
            imageLoader.displayImage(urlImage, img);
        }
    } catch (Exception e) {

    }

但是在我的应用程序运行后,静止图像无法显示。这个库中是否缺少设置?请帮忙

【问题讨论】:

    标签: java android android-listview


    【解决方案1】:

    这似乎有点矫枉过正——对于一个使用异步加载并能很好地处理列表视图的优秀库,请查看 koush 的(知名开发者)UrlImageViewHelper lib on github

    【讨论】:

    • 非常好用又容易上手
    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 2013-10-17
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多