universal-image-loader 配置

简介:

 

	/**
	 * 初始化ImageLoader
	 */
	public static void initImageLoader(Context context) {
		File cacheDir = StorageUtils.getOwnCacheDirectory(context,
				"bee_k77/Cache");// 获取到缓存的目录地址
		Log.e("cacheDir", cacheDir.getPath());
		// 创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				// max width, max height,即保存的每个缓存文件的最大长宽
				.memoryCacheExtraOptions(480, 800)
				// Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息,最好不要设置这个
//				 .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) 
				// 线程池内加载的数量
				.threadPoolSize(3)
				// 线程优先级
				.threadPriority(Thread.NORM_PRIORITY - 2)
				/*
				 * When you display an image in a small ImageView
				 *  and later you try to display this image (from identical URI) in a larger ImageView 
				 *  so decoded image of bigger size will be cached in memory as a previous decoded image of smaller size.
				 *  So the default behavior is to allow to cache multiple sizes of one image in memory. 
				 *  You can deny it by calling this method: 
				 *  so when some image will be cached in memory then previous cached size of this image (if it exists)
				 *   will be removed from memory cache before.
				 */
//				.denyCacheImageMultipleSizesInMemory()
				
				// You can pass your own memory cache implementation你可以通过自己的内存缓存实现
				// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) 
				// .memoryCacheSize(2 * 1024 * 1024)
				//硬盘缓存50MB
				.diskCacheSize(50 * 1024 * 1024)
				 //将保存的时候的URI名称用MD5
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				// 加密
				 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				 .diskCacheFileCount(100) //缓存的File数量
				.diskCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径
				// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				// .imageDownloader(new BaseImageDownloader(context, 5 * 1000,
				// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);// 全局初始化此配置
	}


Option类

package com.topnews.config;

import android.graphics.Bitmap;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.topnews.R;

public class Options {
	/**
	 * 新闻列表中用到的图片加载配置
	 */
	public static DisplayImageOptions getListOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		// 设置图片在下载期间显示的图片
				.showImageOnLoading(R.drawable.ic_stub)
				// 设置图片Uri为空或是错误的时候显示的图片
				.showImageForEmptyUri(R.drawable.ic_stub)
				// 设置图片加载/解码过程中错误时候显示的图片
				.showImageOnFail(R.drawable.ic_error)
				// 设置下载的图片是否缓存在内存中
				.cacheInMemory(false)
				// 设置下载的图片是否缓存在SD卡中
				.cacheOnDisc(true)
				// 保留Exif信息
				.considerExifParams(true)
				// 设置图片以如何的编码方式显示
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				// 设置图片的解码类型
				.bitmapConfig(Bitmap.Config.RGB_565)
				// .decodingOptions(android.graphics.BitmapFactory.Options
				// decodingOptions)//设置图片的解码配置
				.considerExifParams(true)
				// 设置图片下载前的延迟
				.delayBeforeLoading(100)// int
				// delayInMillis为你设置的延迟时间
				// 设置图片加入缓存前,对bitmap进行设置
				// .preProcessor(BitmapProcessor preProcessor)
				.resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位
				// .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少
				.displayer(new FadeInBitmapDisplayer(100))// 淡入
				.build();
		return options;
	}
}


 

相关文章
|
6月前
|
分布式计算 IDE Hadoop
Big Data Tools插件使用
Big Data Tools插件使用
194 0
|
存储 缓存 Java
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(二)
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(二)
209 0
【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )(二)
Android-Universal-Image-Loader 框架使用
1、Android-Universal-Image-Loader      github下载地址    https://github.com/nostra13/Android-Universal-Image-Loader   2、使用方法   http://www.cnblogs.com/Mz-Chris/p/4603409.html   3、ListView、GridView加载图片 我们一般情况下要展示大量的图片时都是采用GridView、ListView组件,而我们在这些组件上滚动时,我们可以停止图片的加载,而当停止滑动时,我们就可以加载当前界面上的图片。
852 0
|
缓存 Java
Universal-Image-Loader源码分析,及常用的缓存策略
讲到图片请求,主要涉及到网络请求,内存缓存,硬盘缓存等原理和4大引用的问题,概括起来主要有以下几个内容: 原理示意图     主体有三个,分别是UI,缓存模块和数据源(网络)。它们之间的关系如下: ① UI:请求数据,使用唯一的Key值索引Memory Cache中的Bitmap。 ② 内存缓存:缓存搜索,如果能找到Key值对应的Bitmap,则返回数据
1113 0

热门文章

最新文章