Android笔记(二):网络图片加载器

可以根据url地址来加载图片,并缓存。

public class AsyncImageLoader {
    final Handler handler = new Handler(Looper.getMainLooper());
    private HashMap<String, SoftReference<Drawable>> imageCache;
    String title;  //图片对应的标题

    public AsyncImageLoader() {
        imageCache = new HashMap<>();//图片缓存
    }

    // 回调函数
    public interface ImageCallback {
        void onImageLoad(Integer t, String title, Drawable drawable);

        void onError(Integer t);
    }

    public void loadDrawable(final Integer pos, final String imageUrl, String title,
                                 final ImageCallback imageCallback) {
        this.title = title;
        new Thread() {
            @Override
            public void run() {

                LoadImg(pos, imageUrl, imageCallback);

            }
        }.start();
    }

    public void LoadImg(final Integer pos, final String imageUrl,
                        final ImageCallback imageCallback) {
        // 首先判断是否在缓存中
        // 但有个问题是:ImageCache可能会越来越大,以至用户内存用光,所以要用SoftReference(弱引用)来实现
        if (imageCache.containsKey(imageUrl)) {
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            final Drawable drawable = softReference.get();
            if (drawable != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        imageCallback.onImageLoad(pos, title, drawable);
                    }
                });
                return;
            }
        }
        // 尝试从URL中加载
        try {
            final Drawable drawable = loadImageFromUrl(imageUrl);
            if (drawable != null) {
                imageCache.put(imageUrl, new SoftReference<>(drawable));
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageCallback.onImageLoad(pos, title, drawable);
                }
            });
        } catch (IOException e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageCallback.onError(pos);
                }
            });
            e.printStackTrace();
        }

    }

    // 根据URL加载图片,如果出现错误throws IOException式的错误,以便在LoadImg中捕获,执行OnError()函数
    public static Drawable loadImageFromUrl(String url) throws IOException {
        URL m;
        InputStream i;
        m = new URL(url);
        i = (InputStream) m.getContent();
        Drawable d = Drawable.createFromStream(i, "src");
        return d;
    }
}

使用:

new AsyncImageLoader().loadDrawable(i, url,title, new AsyncImageLoader.ImageCallback() {
                    @Override
                    public synchronized void onImageLoad(Integer t,String title,Drawable drawable) {
                        images.add(drawable);
                        titles.add(title);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Integer t) {
                        Toast.makeText(MainActivity.this,"图片加载失败",Toast.LENGTH_SHORT).show();
                    }
                });


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌面小侠Plus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值