* 是应用启动的时候先执行的类
* <p>
* 一般初始化的操作在这里执行。
* <p>
* 三级缓存:
* 1.网络缓存
* 2.内存卡 SDCard
* 3.内存缓存
* <p>
* 当我们加载图片时候,首先去内存找,如果有,直接加载。如果没有,会去sdcard里面找,如果有,加载,向内存中存一份。如果sdcard也没有,
* 去网络下载,下载完毕之后,sdcard,内存各存一份,加载到控件上。如果网络也没有。
* <p>
* 程序在内存中运行时最快的。
*/
添加依赖org.webjars.npm:react-imageloader:2.1.0
//创建默认的imageLoader配置参数 ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this); //Initialize ImageLoader with configuration. ImageLoader.getInstance().init(configuration);
代码实现:
private void initImageLoader() {
//Configuration 配置
//Builder 建造者模式
//存到images
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/images";
File file = new File(sdcardPath);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
//内存配置
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
//磁盘缓存
// .diskCacheExtraOptions(480, 800, Bitmap.CompressFormat.JPEG, 75, null)
.threadPoolSize(3) // 线程池里面线程的数量
.threadPriority(Thread.NORM_PRIORITY - 1) // 线程优先级
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存的大小
.memoryCacheSize(2 * 1024 * 1024)
.diskCache(new UnlimitedDiskCache(file)) // 指sdcard的缓存路径
.diskCacheSize(50 * 1024 * 1024)//指sdcard的缓存大小
// .diskCacheFileCount(100)//指sdcard的缓存文件数量
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
// .imageDecoder(new BaseImageDecoder()) // 图片解码
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
//记得配置到ImageLoader
ImageLoader.getInstance().init(config);
}
//加载图片时候的配置
public static DisplayImageOptions getOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 加载过程中显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) //当地址为空的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) // 加载错误时显示的图片
.resetViewBeforeLoading(true) // 加载前重置视图
.cacheInMemory(true) // 启动内存缓存
.cacheOnDisk(true) // 启动磁盘缓存
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.displayer(new SimpleBitmapDisplayer()) // default
.displayer(new FadeInBitmapDisplayer(100))
.handler(new Handler()) // default
.build();
return options;
}
最后在Adapter里面 设置上
在AndroidManifest里设置
<application
android:name=".MyApp"
</application>
String s = list.get(position).getIcon(); ImageLoader.getInstance().displayImage(s, myHanlder.imageView, MyApp.getOptions());
精简版:
继承 Application
private void initImageLoader() { //创建默认的ImageLoader配置参数 ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this); //Initialize ImageLoader with configuration. ImageLoader.getInstance().init(configuration); }
public static DisplayImageOptions getOptions() { DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) // default .cacheOnDisk(true) // default .bitmapConfig(Bitmap.Config.RGB_565) // default .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build(); return options;