ImageLoader的使用
1.ImageLoaderConfiguration 配置参数如下:
File cacheDir = StorageUtils.getCacheDirectory(context); //缓存文件夹路径
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽
.diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default 可以自定义缓存路径
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.diskCacheFileCount(100) // 可以缓存的文件数量
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build(); //开始构建
//需要识记的是以下几个参数:
File cacheFile=getExternalCacheDir();
ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽
.threadPoolSize(2)//线程池的数量
.threadPriority(4)
.memoryCacheSize(2*1024*1024)//设置内存缓存区大小
.diskCacheSize(20*1024*1024)//设置sd卡缓存区大小
.diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录
.writeDebugLogs()//打印日志内容
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理
.build();
2.显示图片配置参数如下:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
.resetViewBeforeLoading(false) // default 设置图片在加载前是否重置、复位
.delayBeforeLoading(1000) // 下载前的延迟时间
.cacheInMemory(false) // default 设置下载的图片是否缓存在内存中
.cacheOnDisk(false) // default 设置下载的图片是否缓存在SD卡中
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.ARGB_8888) // default 设置图片的解码类型
.decodingOptions(...) // 图片的解码设置
.displayer(new SimpleBitmapDisplayer()) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20)
.handler(new Handler()) // default
.build();
//需要识记的是以下几个参数:
DisplayImageOptions options=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存
.showImageOnLoading(R.mipmap.ic_stub)//设置正在下载的图片
.showImageForEmptyUri(R.mipmap.ic_empty)//url为空或请求的资源不存在时
.showImageOnFail(R.mipmap.ic_error)//下载失败时显示的图片
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式
.build();
3.ImageLoader的使用步骤如下:
- 在当前工程中导入ImageLoader的jar包
- 进行初使化操作,需要对ImageLoader进行配置,它的配置全局配置一次就可以,通常在Application中
- 进行显示图片的配置DisplayImageOptions
- 调用ImageLoader.getInstance().displayImage()方法从网络上下载文件
4.ImageLoader使用默认配置,代码步骤如下:
//请求的图片的url
String picUrl="http://img-arch.pconline.com.cn/images/piclib/201306/18/batch/1/179626/1371518448073jl2qblsrem.jpg";
//第一步:进行初使化-缺省配置
ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(config);
//第二步:进行图片缓存及显示图片的配置-默认设置
DisplayImageOptions options=DisplayImageOptions.createSimple();
//第三步:加载显示图片 这里也可以使用 displayImage方法
ImageLoader.getInstance().loadImage(picUrl, options, 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) {
img.setImageBitmap(bitmap);
}
//取消加载时回调
@Override
public void onLoadingCancelled(String s, View view) {
}
});