图片的来源:网络,磁盘,assert,drawable
loadimagesync
@Override
public void onClick(View v) {// ImageLoader.getInstance().displayImage(imageUrl, imageView,)
ImageSize imageSize = new ImageSize(100, 100); //设置显示图片的大小
ImageLoader.getInstance().loadImage(imageUrl, imageSize, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
@Override
public void onLoadingCancelled(String s, View view) {
}
});
}
使用displayimageoptions,来设置显示配置:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher_round)
.showImageOnFail(R.mipmap.ic_launcher)
.resetViewBeforeLoading(false)
.delayBeforeLoading(1000) //延迟加载1s
.cacheInMemory(false)//不在内存进行缓存
.cacheOnDisk(false)//不在磁盘进行缓存// .preProcessor()// .postProcessor()// .extraForDownloader() .considerExifParams(false)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)// .decodingOptions()
.displayer(new SimpleBitmapDisplayer())
.handler(new Handler())
.build();
displayImag:
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageSize targetSize, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
this.checkConfiguration(); //檢查ImagloderConfigration
if(imageAware == null) { // 承载iamge的主体,也就是ImamgeView不存在就会报错
throw new IllegalArgumentException("Wrong arguments were passed to displayImage() method (ImageView reference must not be null)");
} else {
if(listener == null) { //加载图片的状态监听器
listener = this.defaultListener;
}
if(options == null) {
//显示配置,如果为空,则选择默认配置
options = this.configuration.defaultDisplayImageOptions;
}
if(TextUtils.isEmpty(uri)) {
//如果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);
}
//在执行另一个callback方法
listener.onLoadingComplete(uri, imageAware.getWrappedView(), (Bitmap)null);
} else {
if(targetSize == null) {
//如果没有设置制定的显示大小,则会根据,定义的缓存中的size和屏幕像素点来决定
targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, this.configuration.getMaxImageSize());
}
//拼接成资格字符串,用作key
String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);
this.engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);
listener.onLoadingStarted(uri, imageAware.getWrappedView());
Bitmap bmp = 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()) {
// 如果设置了PostProcessor,这就执行“善后”工作;
// 并且imageloadingInfo构建了一个结构体,用来传递数据。
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);
}
}
}
}
}
displaytask的步骤:
public void run() {
L.d("PostProcess image before displaying [%s]", new Object[]{this.imageLoadingInfo.memoryCacheKey});
BitmapProcessor processor = this.imageLoadingInfo.options.getPostProcessor();
//挑用善后处理器进行处理,处理完之后,还是返回bitmap对象
Bitmap processedBitmap = processor.process(this.bitmap);
DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(processedBitmap, this.imageLoadingInfo, this.engine, LoadedFrom.MEMORY_CACHE);
//runTask这个方法决定展示工作是同步还是异步,根据isSyncLoading()方法
LoadAndDisplayImageTask.runTask(displayBitmapTask, this.imageLoadingInfo.options.isSyncLoading(), this.handler, this.engine);
}
displaybitmaptask的步骤:
public void run() {
//如果view被回收过
if(this.imageAware.isCollected()) {
L.d("ImageAware was collected by GC. Task is cancelled. [%s]", new Object[]{this.memoryCacheKey});
this.listener.onLoadingCancelled(this.imageUri, this.imageAware.getWrappedView());
// 如果view被复用过
} else if(this.isViewWasReused()) {
L.d("ImageAware is reused for another image. Task is cancelled. [%s]", new Object[]{this.memoryCacheKey});
this.listener.onLoadingCancelled(this.imageUri, this.imageAware.getWrappedView());
} else {
L.d("Display image in ImageAware (loaded from %1$s) [%2$s]", new Object[]{this.loadedFrom, this.memoryCacheKey});
//显示数据
//此处的displayer是指默认的player,将bitmap通过ImageView进行展示
this.displayer.display(this.bitmap, this.imageAware, this.loadedFrom);
//清空记录
this.engine.cancelDisplayTaskFor(this.imageAware);
//完成加载的毁掉。
this.listener.onLoadingComplete(this.imageUri, this.imageAware.getWrappedView(), this.bitmap);
}
}
private boolean isViewWasReused() {
String currentCacheKey = this.engine.getLoadingUriForView(this.imageAware);
return !this.memoryCacheKey.equals(currentCacheKey);
}