**ImageLoader簡單介紹 **
ImageLoader 是最早開源的 Android 圖片緩存庫, 強(qiáng)大的緩存機(jī)制, 早期使用這個(gè)圖片加載框架的Android應(yīng)用非常多句喜, 至今仍然有不少 Android 開發(fā)者在使用贝次。使用第一步岖赋,配置一些參數(shù)
DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) // 設(shè)置圖片下載期間顯示的圖片
.showImageOnLoading(R.drawable.ic_empty) //設(shè)置下載過程中圖片顯示
.showImageForEmptyUri(R.drawable.ic_empty) // 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageOnFail(R.drawable.ic_error) // 設(shè)置圖片加載或解碼過程中發(fā)生錯(cuò)誤顯示的圖片
.cacheInMemory(true) // 設(shè)置下載的圖片是否緩存在內(nèi)存中
.cacheOnDisc(true) // 設(shè)置下載的圖片是否緩存在SD卡中
.build(); // 創(chuàng)建配置過得DisplayImageOption對象
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(
context.getApplicationContext())
.defaultDisplayImageOptions(options)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
因?yàn)镮mageLoader使用的是單例模式谒主。所以上面的這些配置(還有一些其他配置選項(xiàng)英妓,這里就不一一寫出來了)赔嚎,在項(xiàng)目中只需要設(shè)置一次就可以了。
- 第二步转锈,獲取ImageLoader圖片加載框架實(shí)例對象
ImageLoader imageLoader = ImageLoader.getInstance();
到這里就可以使用獲取到的imagerLoader來進(jìn)行圖片加載了盘寡,使用imagerLoader加載圖片有很多種方式,簡單說三種最常用的撮慨,然后再把全部加在方法源碼貼在后面竿痰,根據(jù)自己的不同需求,大家再具體使用相應(yīng)的方法砌溺。
- 根據(jù)url把圖片展示到imageView控件上(異步加載)
imageLoader.displayImage(imageUri, imageView);
- 根據(jù)url把圖片展示到imageView控件上影涉,并監(jiān)聽圖片加載是否完畢(異步加載)
imageLoader.displayImage(imageUrl, imageView, null , new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//在這里執(zhí)行你想要做的事情
}
}, new ImageLoadingProgressListener(){
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
//在這里執(zhí)行你想要做的事情,total總進(jìn)度规伐,current當(dāng)前加載進(jìn)度
}
});
上面代碼中僅僅重寫了監(jiān)聽對象SimpleImageLoadingListener的onLoadingComplete方法蟹倾,其實(shí)還有其他三個(gè)方法:onLoadingStarted(加載開始),onLoadingFailed(加載失敗)鲜棠,onLoadingCancelled(加載取消)肌厨,可以根據(jù)自己的具體需要去考慮是否進(jìn)行重寫。
- 根據(jù)url獲取到圖片文件并返回為位圖格式對象(同步加載)
Bitmap bmp = imageLoader.loadImageSync(imageUri);
-
以下代碼為ImageLoader對象加載圖片的三大類方法及其重載
方法loadImage是異步加載豁陆,一般需要對加載過程設(shè)置監(jiān)聽柑爸,待圖片資源加載完畢,再手動(dòng)操作把圖片展示到控件上盒音。
方法displayImage是異步加載表鳍,可以不設(shè)置監(jiān)聽,待圖片資源加載完畢祥诽,會自動(dòng)把圖片展示到控件上譬圣。
方法loadImageSync是同步加載,待圖片資源加載完畢雄坪,直接返回位圖資源對象
//ImageViewAware這個(gè)類主要是將ImageView進(jìn)行一個(gè)包裝胁镐,將 ImageView的強(qiáng)引用變成弱引用,
//當(dāng)內(nèi)存不足的時(shí)候诸衔,可以更好的回收ImageView對象盯漂,還有就是獲取ImageView的寬度和高度。
//這使得我們可以根據(jù)ImageView的寬高去對圖片進(jìn)行一個(gè)裁剪笨农,減少內(nèi)存的使用就缆。
public void displayImage(String uri, ImageAware imageAware) {
this.displayImage(uri, (ImageAware)imageAware, (DisplayImageOptions)null, (ImageLoadingListener)null, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageAware imageAware, ImageLoadingListener listener) {
this.displayImage(uri, (ImageAware)imageAware, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options) {
this.displayImage(uri, (ImageAware)imageAware, options, (ImageLoadingListener)null, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener) {
this.displayImage(uri, (ImageAware)imageAware, options, listener, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
this.checkConfiguration();
if(imageAware == null) {
throw new IllegalArgumentException("Wrong arguments were passed to displayImage() method (ImageView reference must not be null)");
} else {
if(listener == null) {
listener = this.emptyListener;
}
if(options == null) {
options = this.configuration.defaultDisplayImageOptions;
}
if(TextUtils.isEmpty(uri)) {
this.engine.cancelDisplayTaskFor(imageAware);
listener.onLoadingStarted(uri, imageAware.getWrappedView());
if(options.shouldShowImageForEmptyUri()) {
imageAware.setImageDrawable(options.getImageForEmptyUri(this.configuration.resources));
} else {
imageAware.setImageDrawable((Drawable)null);
}
listener.onLoadingComplete(uri, imageAware.getWrappedView(), (Bitmap)null);
} else {
ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, this.configuration.getMaxImageSize());
String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);
this.engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);
listener.onLoadingStarted(uri, imageAware.getWrappedView());
Bitmap bmp = (Bitmap)this.configuration.memoryCache.get(memoryCacheKey);
ImageLoadingInfo imageLoadingInfo;
if(bmp != null && !bmp.isRecycled()) {
L.d("Load image from memory cache [%s]", new Object[]{memoryCacheKey});
if(options.shouldPostProcess()) {
imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, this.engine.getLockForUri(uri));
ProcessAndDisplayImageTask displayTask1 = new ProcessAndDisplayImageTask(this.engine, bmp, imageLoadingInfo, defineHandler(options));
if(options.isSyncLoading()) {
displayTask1.run();
} else {
this.engine.submit(displayTask1);
}
} else {
options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);
listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
}
} else {
if(options.shouldShowImageOnLoading()) {
imageAware.setImageDrawable(options.getImageOnLoading(this.configuration.resources));
} else if(options.isResetViewBeforeLoading()) {
imageAware.setImageDrawable((Drawable)null);
}
imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, this.engine.getLockForUri(uri));
LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(this.engine, imageLoadingInfo, defineHandler(options));
if(options.isSyncLoading()) {
displayTask.run();
} else {
this.engine.submit(displayTask);
}
}
}
}
}
public void displayImage(String uri, ImageView imageView) {
this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), (DisplayImageOptions)null, (ImageLoadingListener)null, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) {
this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), options, (ImageLoadingListener)null, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageView imageView, ImageLoadingListener listener) {
this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener) {
this.displayImage(uri, (ImageView)imageView, options, listener, (ImageLoadingProgressListener)null);
}
public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), options, listener, progressListener);
}
public void loadImage(String uri, ImageLoadingListener listener) {
this.loadImage(uri, (ImageSize)null, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null);
}
public void loadImage(String uri, ImageSize targetImageSize, ImageLoadingListener listener) {
this.loadImage(uri, targetImageSize, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null);
}
public void loadImage(String uri, DisplayImageOptions options, ImageLoadingListener listener) {
this.loadImage(uri, (ImageSize)null, options, listener, (ImageLoadingProgressListener)null);
}
public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener) {
this.loadImage(uri, targetImageSize, options, listener, (ImageLoadingProgressListener)null);
}
public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
this.checkConfiguration();
if(targetImageSize == null) {
targetImageSize = this.configuration.getMaxImageSize();
}
if(options == null) {
options = this.configuration.defaultDisplayImageOptions;
}
NonViewAware imageAware = new NonViewAware(uri, targetImageSize, ViewScaleType.CROP);
this.displayImage(uri, (ImageAware)imageAware, options, listener, progressListener);
}
public Bitmap loadImageSync(String uri) {
return this.loadImageSync(uri, (ImageSize)null, (DisplayImageOptions)null);
}
public Bitmap loadImageSync(String uri, DisplayImageOptions options) {
return this.loadImageSync(uri, (ImageSize)null, options);
}
public Bitmap loadImageSync(String uri, ImageSize targetImageSize) {
return this.loadImageSync(uri, targetImageSize, (DisplayImageOptions)null);
}
public Bitmap loadImageSync(String uri, ImageSize targetImageSize, DisplayImageOptions options) {
if(options == null) {
options = this.configuration.defaultDisplayImageOptions;
}
options = (new Builder()).cloneFrom(options).syncLoading(true).build();
ImageLoader.SyncImageLoadingListener listener = new ImageLoader.SyncImageLoadingListener();
this.loadImage(uri, targetImageSize, options, listener);
return listener.getLoadedBitmap();
}
-
對ImageLoader進(jìn)行封裝
建議大家在使用框架的時(shí)候,自己再進(jìn)行一下二次封裝谒亦,這樣如果以后換框架竭宰,整個(gè)項(xiàng)目改動(dòng)的地方會非常少,而且自己封裝的東西份招,用起來肯定會更加得心應(yīng)手切揭。下面是我自己對ImageLoader進(jìn)行簡單封裝生成的一個(gè)類,大家可以參考下然后自己封裝一個(gè)最好锁摔。
public class ImagerLoaderUtil {
private ImageLoader imageLoader;
private DisplayImageOptions options;
private Context context;
private static ImagerLoaderUtil imagerLoaderUtil;
private ImagerLoaderUtil(Context context) {
super();
this.context = context;
}
public synchronized static ImagerLoaderUtil getInstance(Context context){
if(imagerLoaderUtil == null){
imagerLoaderUtil = new ImagerLoaderUtil(context);
imagerLoaderUtil.initImageLoader();
}
return imagerLoaderUtil;
}
public void displayMyImage(String imageUrl, ImageView imageView ){
imageLoader.displayImage(imageUrl, imageView);
}
public void displayMyImage(String imageUrl, ImageView imageView,SimpleImageLoadingListener listener ){
imageLoader.displayImage(imageUrl, imageView, listener);
}
public void displayMyImage(String imageUrl, ImageView imageView, int resourceId){
DisplayImageOptions tempOptions = new DisplayImageOptions.Builder()
.cacheInMemory(false)//設(shè)置下載的圖片是否緩存在內(nèi)存
.cacheOnDisk(true)//設(shè)置下載的圖片是否緩存在SD卡中
.showImageOnLoading(resourceId) // 設(shè)置圖片下載期間顯示的圖片
.showImageForEmptyUri(resourceId) // 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageOnFail(resourceId) // 設(shè)置圖片加載或解碼過程中發(fā)生錯(cuò)誤顯示的圖片
.build();
imageLoader.displayImage(imageUrl, imageView, tempOptions);
}
public void displayMyImage(String imageUrl, ImageView imageView, int resourceId, SimpleImageLoadingListener listener) {
DisplayImageOptions tempOptions = new DisplayImageOptions.Builder()
.cacheInMemory(false)//設(shè)置下載的圖片是否緩存在內(nèi)存
.cacheOnDisk(true)//設(shè)置下載的圖片是否緩存在SD卡中
.showImageOnLoading(resourceId) // 設(shè)置圖片下載期間顯示的圖片
.showImageForEmptyUri(resourceId) // 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageOnFail(resourceId) // 設(shè)置圖片加載或解碼過程中發(fā)生錯(cuò)誤顯示的圖片
.build();
imageLoader.displayImage(imageUrl, imageView, tempOptions, listener);
}
public void initImageLoader() {
DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) // 設(shè)置圖片下載期間顯示的圖片
.showImageOnLoading(R.drawable.ic_empty) //設(shè)置下載過程中圖片顯示
.showImageForEmptyUri(R.drawable.ic_empty) // 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageOnFail(R.drawable.ic_error) // 設(shè)置圖片加載或解碼過程中發(fā)生錯(cuò)誤顯示的圖片
.cacheInMemory(true) // 設(shè)置下載的圖片是否緩存在內(nèi)存中
.cacheOnDisc(true) // 設(shè)置下載的圖片是否緩存在SD卡中
.build(); // 創(chuàng)建配置過得DisplayImageOption對象
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(
context.getApplicationContext())
.defaultDisplayImageOptions(options)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
}
}
- 最后附上ImagerLoader在gitHub上面的項(xiàng)目地址:點(diǎn)擊打開