android lazy load image from the Internet

/*
延迟加载的特性
*Lazy load of images in ListView

*I am using a ListView to display some images and captions associated with those images.
*I am getting the images from the Internet. Is there a way to lazy load the images so while the text displays,
*the UI is not locked up and images are displayed as they are downloaded?
*
*/

package com.wilson.android.library;

import java.io.IOException;

public class DrawableManager {
    private final Map<String, Drawable> drawableMap;

    public DrawableManager() {
        drawableMap = new HashMap<String, Drawable>();
    }

    public Drawable fetchDrawable(String urlString) {
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        }

        Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");

            if (drawable != null) {
                drawableMap.put(urlString, drawable);
                Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
                        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                        + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
            } else {
              Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
            }

            return drawable;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        } catch (IOException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
        if (drawableMap.containsKey(urlString)) {
            imageView.setImageDrawable(drawableMap.get(urlString));
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageView.setImageDrawable((Drawable) message.obj);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                //TODO : set imageView to a "pending" image
                Drawable drawable = fetchDrawable(urlString);
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            }
        };
        thread.start();
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }
}

来自 http://stackoverflow.com/questions/541966/lazy-load-of-images-in-listview

时间: 2024-08-16 08:46:22

android lazy load image from the Internet的相关文章

Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

Entity中Lazy Load的属性序列化JSON时报错

The server encountered an internal error that prevented it from fulfilling this request.org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.party.dinner.entit

Lazy Load, 延迟加载图片的 jQuery 插件

本文翻译自 Lazy Load Plugin for jQuery, 介绍一个 jQuery 插件, 它提供懒汉式加载页面图片的功能. Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 在包含很多大图片长页面中延迟加载图片可以加快页面加载速度. 浏览器将会在加载可见图片之后即进入就绪状态. 在某些情况下还可以帮助降低服

Need a code of lazy load for div--reference

1. For all DIVs of a page $(function() {  $("div").lazyload({effect: 'fadeIn'});}); 2. For a particular DIV having some ID like: <div id="lazyload"> some content </div> $(function() {  $("div#lazyload").lazyload({

Lazy Load延迟加载图片效果

前些日子自己想搞个延时加载的玩玩,但js自己是不会写的,只有上网找插件了.在网上找了好多,都比较坑爹!为什么呢?大部分文章都是他妹的一篇不停的转载,这地方省一点那地方省一点.你说你转载就算了,保留原出处链接也行啊,这样也方便大家可以有更大的收获.但事实上是什么样子,我也就不多说了,我相信上网查过资料的亲都有过感受.在网上找了好久,最终还是有收获的.自己弄了几个小时后,终于折腾出来了.下面献上自己整理的源码,大神喷时还请手留情.先附一张效果图: css样式: .lazy{width:400px;h

jQuery Lazy Load 图片延迟加载

基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery Lazy Load v1.7.2 注意事项: 需要真正实现图片延迟加载,必须将真实图片地址写在 data-original 属性中.若 src 与 data-original 相同,则只是一个特效而已,并不达到延迟加载的功能. 载入 JavaScript 文件 <script src="jqu

jQuery Lazy Load图片懒加载

传送门:官网地址,jQuery Lazy Load v1.7.2下载,Github 使用方法: 1.引用js文件 <script src="jquery.js"></script> <script src="jquery.lazyload.js"></script> 2.修改HTML代码中需要修改的IMG标签 <!-- 将真实图片地址写在 data-original 属性中,而 src 属性中的图片换成占位符的图

MyBatis 之 使用五 延迟加载(Lazy Load)

1. LazyLoad 的作用: 在数据与对象进行 mapping 操作时,只有在真正使用到 该对象时,才进行 mapping 操作,以减少数据库查询开销,从而提升系统性能. 但是Lazy Load也有缺点,在 按需加载时会多次连接数据库,同时会增加数据库的压力.所以在实际使用时,会衡量是否使用 延迟加载. 2. 实现: 1)在 sqlMapConfig.xml 文件中配置 Lazy Load 的开关. <settings> <!-- 开启全局性设置懒加载 --> <sett

Lazy Load, 延迟加载图片的 jQuery 插件(转)

Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 在包含很多大图片长页面中延迟加载图片可以加快页面加载速度. 浏览器将会在加载可见图片之后即进入就绪状态. 在某些情况下还可以帮助降低服务器负担. Lazy Load 灵感来自 Matt Mlinac 制作的 YUI ImageLoader 工具箱. 这是演示页面. 这里