使用 universalimageloader 缓存图片的配置类及使用方法
0、gradle 配置
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.squareup.okhttp3:okhttp:3.3.0' //okttp依赖
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}
一、配置类
/**
* @author: qndroid
* @function: 初始化UniverImageLoader, 并用来加载网络图片
* @date: 16/6/27
*/
public class ImageLoaderUtil { private static final int THREAD_COUNT = 2;
private static final int PRIORITY = 2;
private static final int MEMORY_CACHE_SIZE = 2 * 1024 * 1024;
private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;
private static final int CONNECTION_TIME_OUT = 5 * 1000;
private static final int READ_TIME_OUT = 30 * 1000; private static ImageLoaderUtil mInstance = null;
private static ImageLoader mLoader = null; public static ImageLoaderUtil getInstance(Context context) {
if (mInstance == null) {
synchronized (ImageLoaderUtil.class) {
if (mInstance == null) {
mInstance = new ImageLoaderUtil(context);
}
}
}
return mInstance;
} /**
* 私有构造方法完成初始化工作
*
* @param context
*/
private ImageLoaderUtil(Context context) { ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(context)
.threadPoolSize(THREAD_COUNT)
.threadPriority(Thread.NORM_PRIORITY - PRIORITY)
.denyCacheImageMultipleSizesInMemory()
//.memoryCache(new UsingFreqLimitedMemoryCache(MEMORY_CACHE_SIZE))
.memoryCache(new WeakMemoryCache())
.diskCacheSize(DISK_CACHE_SIZE)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.defaultDisplayImageOptions(getDefaultOptions())
.imageDownloader(new BaseImageDownloader(context, CONNECTION_TIME_OUT, READ_TIME_OUT))
.writeDebugLogs()
.build(); ImageLoader.getInstance().init(config);
mLoader = ImageLoader.getInstance();
} //load the image
public void displayImage(ImageView imageView, String path,
ImageLoadingListener listener, DisplayImageOptions options) {
if (mLoader != null) {
mLoader.displayImage(path, imageView, options, listener);
}
} //load the image
public void displayImage(ImageView imageView, String path, ImageLoadingListener listener) {
if (mLoader != null) {
mLoader.displayImage(path, imageView, listener);
}
} public void displayImage(ImageView imageView, String path) {
displayImage(imageView, path, null);
} /**
* 默认的图片显示Options,可设置图片的缓存策略,编解码方式等,非常重要
*
* @return
*/
private DisplayImageOptions getDefaultOptions() {
DisplayImageOptions options = new
DisplayImageOptions.Builder()
.cacheInMemory(true)//设置下载的图片是否缓存在内存中, 重要,否则图片不会缓存到内存中
.cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中, 重要,否则图片不会缓存到硬盘中
.considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
.decodingOptions(new BitmapFactory.Options())//设置图片的解码配置
.resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位
.build();
return options;
} public DisplayImageOptions getOptionsWithNoCache() { DisplayImageOptions options = new
DisplayImageOptions.Builder()
//.cacheInMemory(true)//设置下载的图片是否缓存在内存中, 重要,否则图片不会缓存到内存中
//.cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中, 重要,否则图片不会缓存到硬盘中
.considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
.decodingOptions(new BitmapFactory.Options())//设置图片的解码配置
.resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位
.displayer(new FadeInBitmapDisplayer(400))
.build();
return options;
}
}
二、使用方法
// 声明
private ImageLoaderUtil mImageLoader; //传入 context 实例化
mImageLoader = ImageLoaderUtil.getInstance(mContext); //显示图片 第一个参数为 imageview 第二个参数为远程的url mImageLoader.displayImage(mImageViews[i], mHeaderValue.middle.get(i));
使用 universalimageloader 缓存图片的配置类及使用方法的更多相关文章
- LindAgile~缓存拦截器支持类的虚方法了
写它的原因 之前写过一个缓存拦截器,主要在方法上添加CachingAspect特性之后,它的返回值就可以被缓存下来,下次访问时直接从缓存中返回结果,而它有一个前提,就是你的方法需要是一个接口方法,缓存 ...
- 最全的C#图片处理帮助类ImageHelper
最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...
- spring 配置 Java配置类装配bean
https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...
- springboot项目启动之后初始化自定义配置类
前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...
- 【Spring】简述@Configuration配置类注册BeanDefinition到Spring容器的过程
概述 本文以SpringBoot应用为基础,尝试分析基于注解@Configuration的配置类是如何向Spring容器注册BeanDefinition的过程 其中主要分析了 Configuratio ...
- 真懂Spring的@Configuration配置类?你可能自我感觉太良好
当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...
- Android-Universal-Image-Loader的缓存处理机制与使用 LruCache 缓存图片
讲到缓存,平时流水线上的码农一定觉得这是一个高大上的东西.看过网上各种讲缓存原理的文章,总感觉那些文章讲的就是玩具,能用吗?这次我将带你一起看过UIL这个国内外大牛都追捧的图片缓存类库的缓存处理机制. ...
- Android之简单了解Bitmap显示图片及缓存图片
昨天我们学了如何连接网络,今天我们就学习一下如何从把网上图片显示到项目中 今天主要用到的是Bitmap 类 Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图 ...
- Android 使用 LruCache 缓存图片
在你应用程序的 UI 界面加载一张图片是一件很简单的事情,但是当你需要在界面上加载一大堆图片的时候,情况就变得复杂起来.在很多情况下,(比如使用 ListView, GridView 或者 ViewP ...
随机推荐
- phpstorm2017.2.1破解
今天安装phpstorm时看了网上很多破解方法,基本上都是用http://idea.lanyus.com/ 提供的注册码或者直接在license server上粘贴譬如http://idea.lany ...
- poj2817状态压缩 升维
/* 两两求出字符串之间最大可以匹配的值 由已知状态推导出位置状态 状态s表示已经加入到集合中的字符串,0表示串i不存在,1存在 由于字符串的加入顺序会影响结果,所以增加一维来表示 dp[S][i]表 ...
- You are my brother
问题 : You are my brother 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Little A gets to know a new friend, Little B, ...
- 10进制 VS 2进制
10进制 VS 2进制 时间限制: 1 Sec 内存限制: 32 MB 题目描述 样例输出 623 #include<stdio.h> #include<string.h> ...
- 解决beego中同时开启http和https时,https端口占用问题
在beego的app.go文件中, 找到 // run normal mode if BConfig.Listen.EnableHTTPS { go func() { time.Sleep( * ti ...
- hdfs数据到hbase过程
需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...
- python内置的魔术命令(builtin magic commands)
在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...
- C++ 复制构造函数 与 赋值运算符
在C++中,将一个对象赋给另外一个对象,编译器将提供赋值运算符的定义. 有两种情况,下面假设catman是Monster的一个实例 第一种:初始化 Monster golblen= catman; 第 ...
- (6).NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- centos7 yum install redis
直接yum 安装的redis 不是最新版本 yum install redis 如果要安装最新的redis,需要安装Remi的软件源,官网地址:http://rpms.famillecollet.co ...