首先在使用ImageLoader的时候我们需要先导入jar包,这里由于用的studio所以直接查找就ok
查找方法:ctrl +alt +shift +s 打开页面:
导入包后需要创建自己的工具类初始化ImageLoader,因为在使用ImageLoader的时候必须初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
ImageLoader.getInstance().init(ImagLoaderConfigs.getImageLoaderConfiguration(this));
}
}
创建这个工具类需要在清单文件中配置个找到它的路径
上面初始化的时候我们还创建了一个ImagLoaderConfigs类,
public class ImagLoaderConfigs {
public static ImageLoaderConfiguration getImageLoaderConfiguration(Context context) {
ImageLoaderConfiguration configuration = 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 设置当前线程的优先级
//任务处理优先级 Fist In Fist Out
//.tasksProcessingOrder(QueueProcessingType.FIFO) // default
//内存中不缓存一张图片的多个尺寸大小
//.denyCacheImageMultipleSizesInMemory()
//内在缓存策略
//.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
//内存缓存大小
//.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
//内在缓存大小:占用百分比
.memoryCacheSizePercentage(13) // default
//磁盘缓存策略
//.diskCache(new LruDiskCache()) // default 可以自定义缓存路径
//磁盘缓存大小
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
//.diskCacheFileCount(100) // 可以缓存的文件数量
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
//.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
//.imageDownloader(new BaseImageDownloader(context)) // default
//(new BaseImageDecoder(false)) // default
//加载具体图片时的一些配置
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build();
return configuration;
}
public static DisplayImageOptions getDefaultDisplayImageOptions(Context context) {
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
//是否缓存
.cacheInMemory(true)
.cacheOnDisk(true)
//RGB 565 r红色占5 g绿色占6 b蓝色占5 -> 2字节
//alpha
//ARGB 4444 4 4 4 4 -> 2字节
//ARGB 8888 -> 4字节
//10 * 10 用rgb565 -> 10*10*2
.bitmapConfig(Bitmap.Config.RGB_565)
//加载时、加载错误时展示什么内容
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
//加载效果
//ctrl + p
.displayer(new CircleBitmapDisplayer())
.build();
//ctrl + h
//BitmapDisplayer;
return displayImageOptions;
}
}
注释的了解即可,下面我们就可以在适配器中设置图片了
//设置图片,只需要在adapter设置内容的时候添加上就行了
ImageLoader.getInstance().displayImage(item.getFirstImageUrl(),imageView,ImagLoaderConfigs.getDusplayImageOptions(imageView.getContext()));