> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/android/4.2-zi-ding-yi-imageloader.md).

# 4.2自定义ImageLoader

## 自定义下载设置

```bash
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(context)
.threadPoolSize(THREAD_COUNT)
.threadPriority(Thread.NORM_PJRIORITY-PRIORITY)
.denyCacheImageMultipleSizesInMemory() //防止缓存多套尺寸的图片到我们内存中
.memoryCathe(new WeakMemoryCache())//弱因公
.diskCacheSize(DISK_CACHE_SIZE);
.diskCacheFileNameGenerator(new Nd5FileNameGenerator())//使用MD5命名缓存文件
.tasksProcessionOrder(QueueProcessingType.LIFO)//图片下载顺序
.defaultDisplayImageOptions(getDefultOptions())//默认的图片加载options
.imageDownloader(new BaseImageDownloader(context,CONNECTION_TIME_OUT,READ_TIME_OUT))//设置图片下载器
.writeDebugLogs()//debug环境下会输出日志
.build();

ImageLoader.getInstance().init(configeration);
ImageLoader.getInstance();

private DisplayImageOptions getDefaultOptions(){
DisplayImageOptions options=new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.xadsdk_ima_error)//图片地址为空时,默认图片
.showImageOnFail(R.drawable.xadsdk_ima_error) //图片下载失败时显示的图片
.cacheInMemory(true)//设置图片可以缓存在内存
.cacheOnDisk(true)//设置图片可以缓存在硬盘
.bitmapConfig(Bitmap.Config.RGB_565)//使用的图片解码类型
.decodingOptions(new BitmapFacrory.Optins())//图片解码配置


.build();
}
```

```java
线程数
4
图片加载优先级
2
缓存大小
50*1024
连接超时时间（毫秒）
5*1000
读取超时时间（毫秒）
30*1000
```

正常的线程优先级Thread.NORM\_PJRIORITY

## 自定义显示设置

```java
DisplayImageOptions options=new DisplayImageOptions.Builder(),build();
imageLoader.displayImage("url",imageView,options,new SimpleImageLoadingListener(){})
```

监听

```java
new SimpleImageLoadingListener(){

}
```

## imageloader的流程控制

内存

硬盘

网络
