zoukankan      html  css  js  c++  java
  • Android基础篇异步获取网络图片

    很多时候,我们从网络上获取到的图片只是一个Url地址。

    我们必须采用异步加载的方法,将图片显示才来。

    public class AsyncImageLoader {
    
         private HashMap<String, SoftReference<Drawable>> imageCache;
          
             public AsyncImageLoader() {
                 imageCache = new HashMap<String, SoftReference<Drawable>>();
             }
          
             public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
                 if (imageCache.containsKey(imageUrl)) {
                     SoftReference<Drawable> softReference = imageCache.get(imageUrl);
                     Drawable drawable = softReference.get();
                     if (drawable != null) {
                         return drawable;
                     }
                 }
                 final Handler handler = new Handler() {
                     public void handleMessage(Message message) {
                         imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
                     }
                 };
                 new Thread() {
                     @Override
                     public void run() {
                         Drawable drawable = loadImageFromUrl(imageUrl);
                         imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                         Message message = handler.obtainMessage(0, drawable);
                         handler.sendMessage(message);
                     }
                 }.start();
                 return null;
             }
          
            public static Drawable loadImageFromUrl(String url) {
                URL m;
                InputStream i = null;
                try {
                    m = new URL(url);
                    i = (InputStream) m.getContent();
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Drawable d = Drawable.createFromStream(i, "src");
                return d;
            }
          
             public interface ImageCallback {
                 public void imageLoaded(Drawable imageDrawable, String imageUrl);
             }
    
    }

    当我们需要使用的时候

            viewHolder.mWeiboPhoto.setTag(mFriendBeans.get(position).getPhoto());
            Drawable cachedImage = asyncImage.loadDrawable(mFriendBeans.get(position).getPhoto(), new ImageCallback() {
                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) mListView.findViewWithTag(imageUrl);
                    if (imageViewByTag != null) {
                        imageViewByTag.setImageDrawable(imageDrawable);
                    }
                }
            });
            if (cachedImage == null) {
                Log.info("json", "json is null");
                viewHolder.mWeiboPhoto.setImageResource(R.drawable.sinaweibo);
            }else{
                Log.info("json", "json is not null");
                viewHolder.mWeiboPhoto.setImageDrawable(cachedImage);//图标
            }
  • 相关阅读:
    网站页面布局的实现
    谈谈我对MVC的View层实现的理解
    浅谈.htaccess文件--避免滥用.htaccess文件
    magento新增商品属性以及将属性加入Flat table
    MySQL JOIN | 联结
    Linux常用命令
    Laravel Model查询结果的3种存储格式内存占用对比
    Laravel配置Route调用artisan
    研究微信红包分配算法之Golang版
    解决IDEA提示Untrusted Server's certificate 证书不可用( Server's certificate is not trusted )
  • 原文地址:https://www.cnblogs.com/gongcb/p/2601565.html
Copyright © 2011-2022 走看看