universal-image-loader的配置

来源:互联网 发布:网络远程教育 编辑:程序博客网 时间:2024/05/16 08:04

一般在application中设置基本的信息,在每个activity中根据需求配置具体的信息

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800)//设置每个缓存文件的最大长宽
.discCacheExtraOptions(480, 800, null)//保存到硬盘的每个文件的最大长宽
.threadPoolSize(3)//设置线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))//设置内存缓存的最大大小(2M)
.discCacheSize(50 * 1024 * 1024)//硬盘缓存的最大大小(50M)
.discCacheFileNameGenerator(new Md5FileNameGenerator())//设置缓存的文件的名字以md5加密的方式保存到SD中
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)//设置缓存文件的数量
.discCache(new UnlimitedDiskCache(new File(Environment.getExternalStorageDirectory() + “myapp/imgCache”)))//设置缓存文件的路径
.defaultDisplayImageOptions(getDisplayOptions())//加载图片的配置信息(也可以在每个页面根据不同的需求单独写)
.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000))//设置链接,加载超时的时间
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);

private DisplayImageOptions getDisplayOptions() {
DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)//设置图片下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//设置Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher)//设置图片加载/解码工程中错误的时候显示的图片
.cacheInMemory(true)//设置下载的图片是否缓存在内存中
.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
.considerExifParams(true)//是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型
.resetViewBeforeLoading(true)//设置图片下载前是否重置,复位
.displayer(new RoundedBitmapDisplayer(20))//是否设置圆角,弧度为多少
.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
.build();
return options;

}

getDisplayOptions()是在applicaiton中设置了具体的配置信息,在application中配置好的在使用的时候用 ImageLoader imageLoader = ImageLoader.getInstance()获取,用imageLoader .displayImage(url, imageview, Options)来展示;开发中一般很少这么做,因为每个界面的图片加载的需求不同,应该在每个界面找那个根据需求单独的配置,此时要在每个页面中DisplayImageOptions imageOptions = new DisplayImageOptions.Builder().showImageOnLoading(0)
.showImageForEmptyUri(0)
.showImageOnFail(0)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new RoundedBitmapDisplayer(0)).build();这么做重新new一个DisplayImageOptions ,可以根据不同的界面需求展示的图片也不同

0 0
原创粉丝点击