第一次写博客好紧张,不多说上封装的工具类:
init 方法在Applicatio中调用就好,然后再需要显示图片的地方调用displayImage方法,OK
public class ImageLoaderUtils {
/**
* 初始化、配置
*
* @param context
*/
public static void init(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
// default 线程池内加载的数量
.threadPoolSize(3)
// default 设置当前线程的优先级
.threadPriority(Thread.NORM_PRIORITY - 2)
// default
.tasksProcessingOrder(QueueProcessingType.FIFO)
.denyCacheImageMultipleSizesInMemory()
// 可以通过自己的内存缓存实现
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
// 内存缓存的最大值
.memoryCacheSize(2 * 1024 * 1024)
// default 可以自定义缓存路径,为了方便直接用了外部存储卡
.discCache(
new UnlimitedDiscCache(Environment
.getExternalStorageDirectory()))
// 50 Mb sd卡(本地)缓存的最大值
.discCacheSize(50 * 1024 * 1024)
// 可以缓存的文件数量
.discCacheFileCount(100)
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new
// Md5FileNameGenerator())加密
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(context))
.imageDecoder(new BaseImageDecoder(true))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.enableLogging() // 打印debug log
.build(); // 开始构建
ImageLoader.getInstance().init(config);
}
/**
* 显示加载失败、加载等待、数据为空 图片
*
* @return
*/
public static DisplayImageOptions getAvatarDisplayOptions() {
DisplayImageOptions avatarOptions = new DisplayImageOptions.Builder()
// 加载等待 时显示的图片
.showStubImage(R.drawable.ic_launcher)
// 加载数据为空时显示的图片
.showImageForEmptyUri(R.drawable.ic_launcher)
// 加载失败时显示的图片
.showImageOnFail(R.drawable.ic_launcher)
// 缓存进内存=======缓存进本地
.cacheInMemory().cacheOnDisc()
// 圆角
.displayer(new RoundedBitmapDisplayer(22)).build();
return avatarOptions;
}
/**
* 显示图片
*
* @param url
* @param imageView
* @param options
*/
public static void displayImage(String url, ImageView imageView,
DisplayImageOptions options) {
ImageLoader.getInstance().displayImage(url, imageView, options);
}
}