使用 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 ...
随机推荐
- yum安装软件内容
linux yum源改为阿里yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...
- 攻击WordPress和其他程序
1.SAAS服务(Software as a Service )管理其他作为内容管理的一个人框架的软件,http://www.turnkeylinx.org 上发布了很多测试应用的程序.WordPre ...
- github文档
Video Guides GitHub Help GitHub.com Hello World 10 minute read Intro What is GitHub? Create a Repo ...
- Android Studio 配置虚拟设备的镜像文件的存放路径
操作系统:Windows 10 x64 IDE:Android Studio 3.3 Android Studio创建的虚拟设备的默认存放路径是位于C盘,这导致C盘的可用容量变小. 所以,我决定要将虚 ...
- OrCAD Capture CIS 16.6 导出BOM
OrCAD Capture CIS 16.6 一.选择设计文件:菜单:Tools > Bill of Materials... 二.Bill of Materials > Open in ...
- linux上安装redis并使用
1.下载:curl -O http://download.redis.io/releases/redis-4.0.6.tar.gz 2.在/usr/local/redis上解压:tar -zxvf r ...
- JAVA 程序编译过程;编辑器,编译器和解释器
最基本的软件工具包括,编辑器,编译器,解释器; 编译器:编译器就是将一种编程语言代码翻译成另一种语言的等效代码程序. 解释器:解释器将编译和执行交织在一起,即编译一部分代码后执行该部分代码,然后再编译 ...
- python---实现多个有序列表的合并
我觉得不用抄书上的代码. 遇到实现问题,应该结合python本身的功能去解决. 比如,当合并有序列表时,为什么一定要一项一项比较,而不是使用list的sort函数呢? # coding = utf-8 ...
- 无法连接ssh,fatal: daemon() failed: No such device
今天发现一个服务器的sshd无法启动,查看/var/log/secure里发现:fatal: daemon() failed: No such device 解决办法: rm /dev/null /d ...
- 【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】
差分约束系统,求最小值,跑最长路. 转自:https://www.cnblogs.com/ehanla/p/9134012.html 题解:设sum[x]为前x个咕咕中至少需要赶走的咕咕数,则sum[ ...