Android的图片异步加载

1、为什么要异步加载图片?

2、缓存的实现(SoftReference)?

      栈内存:存放对象的引用。堆内存:存放对象。引用指向对象,当对象的引用不再指向某个对象,或者对象无引用指向,垃圾回收器就会把他们回收。

      SoftReference :软引用,非完成的引用。当垃圾回收机制启动时,首先判断内存是否已满,如果未满,则不对软引用做处理,但是,当内存已满时,会把软引用回收。

     SoftReference<Object> sr=new SoftReference<Object>(new Object());

     Object obj=sr.get();

3、异步加载图片的方法?

  1. public class AsyncImageLoader {  
  2.       //key:图片的URL    value:SoftReference对象,该对象指向一个Drawable对象
  3.      private HashMap<String, SoftReference<Drawable>> imageCache;  
  4.         
  5.          public AsyncImageLoader() {  
  6.              imageCache = new HashMap<String, SoftReference<Drawable>>();  
  7.          }  
  8.         
  9.          public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {  
  10.              if (imageCache.containsKey(imageUrl)) {                                       //首先判断缓存中是否含有该图片,存在的话获取并返回
  11.                  SoftReference<Drawable> softReference = imageCache.get(imageUrl);  
  12.                  Drawable drawable = softReference.get();  
  13.                  if (drawable != null) {  
  14.                      return drawable;  
  15.                  }  
  16.              }  
  17.              final Handler handler = new Handler() {                                         //不存在的话,在线程中加载,并通过Handler发送至UI线程
  18.                  public void handleMessage(Message message) {  
  19.                      imageCallback.imageLoaded((Drawable) message.obj, imageUrl);  
  20.                  }  
  21.              };  
  22.              new Thread() {  
  23.                  @Override  
  24.                  public void run() {  
  25.                      Drawable drawable = loadImageFromUrl(imageUrl);  
  26.                      imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));  
  27.                      Message message = handler.obtainMessage(0, drawable);  
  28.                      handler.sendMessage(message);  
  29.                  }  
  30.              }.start();  
  31.              return null;  
  32.          }  
  33.         
  34.         public static Drawable loadImageFromUrl(String url) {                                //通过图片url获取图片资源
  35.             URL m;  
  36.             InputStream i = null;  
  37.             try {  
  38.                 m = new URL(url);  
  39.                 i = (InputStream) m.getContent();  
  40.             } catch (MalformedURLException e1) {  
  41.                 e1.printStackTrace();  
  42.             } catch (IOException e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.             Drawable d = Drawable.createFromStream(i, "src");  
  46.             return d;  
  47.         }  
  48.         
  49.          public interface ImageCallback {                                                                   //自定义一个接口,
  50.              public void imageLoaded(Drawable imageDrawable, String imageUrl);  
  51.          }  
  52.   




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值