[轉]Android圖片異步加載

獲取圖片工具類:緩存

public class ApacheUtility {異步

/**
  * 獲取圖片流
  *
  * @param uri 圖片地址ide

  * @return
  * @throws MalformedURLException
  */
 public static InputStream GetImageByUrl(String uri) throws MalformedURLException {
  URL url = new URL(uri);
  URLConnection conn;
  InputStream is;
  try {
   conn = url.openConnection();
   conn.connect();
   is = conn.getInputStream();函數

   // 或者用以下方法工具

   // is=(InputStream)url.getContent();
   return is;
  } catch (IOException e) {
   e.printStackTrace();
  }post

  return null;
 }this

 

 

/**
  * 獲取Bitmap url

  *
  * @param uri 圖片地址
  * @return
  */
 public static Bitmap GetBitmapByUrl(String uri) {spa

  Bitmap bitmap;  
  InputStream is;
  try {.net

   is =  GetImageByUrl(uri); 

   bitmap = BitmapFactory.decodeStream(is);
   is.close();

   return bitmap;

  } catch (MalformedURLException e) {
   e.printStackTrace();

  } catch (IOException e) {
   e.printStackTrace();
  }

  return null;
 }

 

 

/**
  * 獲取Drawable

  *
  * @param uri  圖片地址

  * @return
  */
 public static Drawable GetDrawableByUrl(String uri) {

  Drawable drawable;  
  InputStream is;
  try {

   is =  GetImageByUrl(uri); 

   drawable= Drawable.createFromStream(is, "src");

   is.close();

   return drawable;

  } catch (MalformedURLException e) {
   e.printStackTrace();

  } catch (IOException e) {
   e.printStackTrace();
  }

  return null;
 }

}

 

異步任務加載圖片類:

/**
  * 異步加載圖片
  * 

  */
 public class LoadImageAsyncTask extends AsyncTask<String, Integer, Bitmap> {
  private ImageView imageView;

  @Override
  protected void onPostExecute(Bitmap bitmap) {
   if (null != imageView) {
    imageView.setImageBitmap(bitmap);
   }
  }
  
  // 設置圖片視圖實例
  public void setImageView(ImageView image) {
   this.imageView = image;
  }

  @Override
  protected Bitmap doInBackground(String... params) {
   Bitmap bitmap = ApacheUtility.GetBitmapByUrl(params[0]); // 調用前面 ApacheUtility 類的方法

   return bitmap;
  }
 }

調用過程以下:

String imgUrl="http://www.streetcar.org/mim/cable/images/cable-01.jpg"; // 圖片路徑

LoadImageAsyncTask task = new LoadImageAsyncTask();

task.setImageView(iv); // iv爲獲取的ImageView對象實例

tast.execute(imgUrl); // 執行任務,參數與 doInBackground(String... params) 方法參數一致

 

異步圖片加載類:

參考文章:http://blog.163.com/guozioo@126/blog/static/64086947201136102838503/

 

public class AsyncImageLoader {

 // SoftReference是軟引用,是爲了更好的爲了系統回收變量
 private HashMap<String, SoftReference<Drawable>> imagesCache;

 public AsyncImageLoader() {
   imagesCache = new HashMap<String, SoftReference<Drawable>>();
 }
 
 public Drawable loadDrawable(final String imageUrl,final ImageView imageView,final ILoadImageCallback callback){
   if(imagesCache.containsKey(imageUrl)){
     // 從緩存中獲取
     SoftReference<Drawable> softReference=imagesCache.get(imageUrl);
     Drawable drawable=softReference.get();
     if(null!=drawable){
       return drawable;
     }
  }
  
  final Handler handler=new Handler(){
    public void handleMessage(Message message){
      callback.onObtainDrawable((Drawable)message.obj, imageView);
    }
  };
  //創建一個新的線程下載圖片
  new Thread(){
    public void run(){
      Drawable drawable=ApacheUtility.GetBitmapByUrl(imageUrl); // 調用前面 ApacheUtility 類的方法

      imagesCache.put(imageUrl, new SoftReference<Drawable>(drawable));
      Message msg=handler.obtainMessage(0, drawable);
      handler.sendMessage(msg);
    }
  }.start();
   
  return null;
 }

 

 /**
  * 異步加載圖片的回調接口

  */
 public interface ILoadImageCallback {
  public void onObtainDrawable (Drawable drawable, ImageView imageView);
 }
}

調用過程以下:

String imgUrl="http://www.streetcar.org/mim/cable/images/cable-01.jpg"; // 圖片路徑

AsyncImageLoader  imagLoader = new AsyncImageLoader();

Drawable cachedImage= imagLoader.loadDrawable(imgUrl,iv,new ILoadImageCallback(){

  public void onObtainDrawable (Drawable drawable, ImageView imageView){

    imageView.setImageDrawable(drawable);

  }

});

if(null!=cachedImage){

  iv.setImageDrawable(cachedImage);

}

 

 

相似於上面的另外一種實現方式:

public class AsyncImageLoader{

  private HashMap<String, Bitmap> imageMap; // 存放圖片列表:key-圖片路徑;value-Bitmap

  private Handler handler; 

  // 構造函數

  public AsyncImageLoader(){

    imageMap = new HashMap<String, Bitmap>();

    handler = new Handler();

  }

  // 異步加載圖片

  public void loadImage(final String imageUrl,final ImageView imageView){

    new Thread(new Runnable() {
      public void run() {

        Bitmap image = imageMap.get(imageUrl); // 從緩存中獲取數據
        if (image == null) {

          image = ApacheUtility.GetBitmapByUrl(imageUrl); // 調用前面 ApacheUtility 類的方法

          imageMap.put(status.getId(), image);

        }

        final Bitmap bitmap = image;
        handler.post(new Runnable() {
          public void run() {
            imgView.setImageBitmap(bitmap);
          }
        });

      }
    }).start();

  }

}

調用過程以下:

String imgUrl="http://www.streetcar.org/mim/cable/images/cable-01.jpg"; // 圖片路徑

AsyncImageLoader  imagLoader = new AsyncImageLoader();

imageLoader.loadImage(imgUrl,iv); // iv爲ImageView實例

相關文章
相關標籤/搜索