android图片延迟加载,照片墙,listview中图片的延迟加载(国外制造)

今天在csdn的下载头条上看到一个照片墙的实现,说实话,没怎么好好看。想起来我这里存有类似的资源。

国外制造的一个lazyload(延迟加载)的一个源码一个demo和一个开源项目(三份份资源)

周末不上班的无聊的程序猿可以拿来和国产的比较一下

下面是翻译和下载地址


一:

这个就是我创建的用来在我项目中实时显示图片的工程。(ps:我项目中的那个Log是被我自定义了的。)


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();
    }
}



二:

我写了一个 lazy list加载图片的简单的 demo,对于大家来说可能还有些参考价值。这个demo在后台线程中加载图片并缓存到SD卡和内存上。其实我写的这个缓存是一个很简单的足够我这个demo用的东东,大家表怕哈。我用inSampleSize读取图片来减少内存消耗,并做了一下垃圾回收。


下载地址:http://download.csdn.net/detail/wjyyxzzjnws/5968143



三:

我还推荐一个开源项目,他来源于 Fedor Vlasov 的一个LazyList 的项目,然后被众多程序猿改到现在。

简单介绍一下:

  • 多线程的图片加载
  • 可以对ImageLoder做很多调整 (线程执行,下载, 图片解码, 内存和“硬盘”缓存, 显示图片选项等等……)
  • 图片可以在内存、设备的文件系统或者SD卡中缓存
  • 可以监听加载进程
  • 可以在各自独立的的各个图片项中自定义每个图片的显示
  • Widget支持
  • Android 2.0以上支持


下载地址:http://download.csdn.net/detail/wjyyxzzjnws/5968175


第一次做翻译,好累……有什么不对的还请大家批评指正哈,有余力的兄台就帮我多下载点资源哈


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值