多線程下載圖片,圖片可以來源於網路,檔案系統,專案檔夾assets中以及drawable中等支援隨意的配置ImageLoader,例如線程池,圖片下載器,記憶體緩衝策略,硬碟緩衝策略,圖片顯示選項以及其他的一些配置支援圖片的記憶體緩衝,檔案系統快取或者SD卡緩衝支援圖片下載過程的監聽根據控制項(ImageView)的大小對Bitmap進行裁剪,減少Bitmap佔用過多的記憶體較好的控製圖片的載入過程,例如暫停圖片載入,重新開始載入圖片,一般使用在ListView,GridView中,滑動過程中暫停載入圖片,停止滑動的時候去載入圖片提供在較慢的網路下對圖片進行載入
簡單使用:
1.建立一個Android appication.名字叫IApplicaiton.
2.把下載下來的項目裡的sample裡的libs下的jar拷貝到此項目中。
3.建立一個類MyApplicaiton繼承application:
package com.example.iapplication;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import android.app.Application;public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //建立預設的ImageLoader配置參數 ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this); //Initialize ImageLoader with configuration. ImageLoader.getInstance().init(configuration); } }
4.配置mainfest.xml檔案:
注意application節點和許可權的加入。
接下來,載入圖片。
5.定義一個layout布局:
<framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> </framelayout>
6.使用displayImage()載入圖片:
//使用displayImage方法載入public void advanceLoad2(){final ImageView mImageView = (ImageView) findViewById(R.id.image); String imageUrl = http://smartcost.com.cn/global/upload/img/20150206/14223432211081942.jpg; //顯示圖片的配置 DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisk(true) .bitmapConfig(Bitmap.Config.RGB_565) .build(); ImageLoader.getInstance().displayImage(imageUrl, mImageView, options); }
搞定。如果有需要顯示載入圖片進度的話,只需要在displayImage()方法中傳入ImageLoadingProgressListener介面就行了:
imageLoader.displayImage(imageUrl, mImageView, options, new SimpleImageLoadingListener(), new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { } });
載入其他資源的圖片:
String imagePath = /mnt/sdcard/image.png; String imageUrl = Scheme.FILE.wrap(imagePath); // String imageUrl = http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg; imageLoader.displayImage(imageUrl, mImageView, options);
//圖片來源於Content provider String contentprividerUrl = content://media/external/audio/albumart/13; //圖片來源於assets String assetsUrl = Scheme.ASSETS.wrap(image.png); //圖片來源於 String drawableUrl = Scheme.DRAWABLE.wrap(R.drawable.image);
當我們快速滑動GridView,ListView,我們希望能停止圖片的載入,而在GridView,ListView停止滑動的時候載入當前介面的圖片,這個架構當然也提供這個功能,使用起來也很簡單,它提供了PauseOnScrollListener這個類來控制ListView,GridView滑動過程中停止去載入圖片
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling)); gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
第一個參數就是我們的圖片載入對象ImageLoader, 第二個是控制是否在滑動過程中暫停載入圖片,如果需要暫停傳true就行了,第三個參數控制猛的滑動介面的時候圖片是否載入
OutOfMemoryError
雖然這個架構有很好的緩衝機制,有效避免了OOM的產生,一般的情況下產生OOM的機率比較小,但是並不能保證OutOfMemoryError永遠不發生,這個架構對於OutOfMemoryError做了簡單的catch,保證我們的程式遇到OOM而不被crash掉,但是如果我們使用該架構經常發生OOM,我們可以使用一下方式去改善。