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.   




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章