使用 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 ...
随机推荐
- 利用map和stringstream数据流解题
题目描述 喜闻乐见A+B.读入两个用英文表示的A和B,计算它们的和并输出. 输入 第一行输入一个字符串,表示数字A:第二行输入一个字符串表示数字B.A和B均为正整数. 输出 输出一个正整数n,表示A+ ...
- linux用户
hen we are travelling, we find ourselves in new places and new spaces, physically and internally; it ...
- 【python】gevent协程例子
说在前面:用协程还是多线程需要仔细考量.我在做实验时请求了100w个ip,分别用pool为1000的协程和64个线程来跑,结果是多线程的速度是协程的10倍以上. 一个简单的协程例子 #!/usr/bi ...
- 高斯消元处理无解|多解情况 poj1830
高斯消元结束后,若存在系数为0,常数不为0的行,则方程无解 若系数不为0的行有k个,则说明主元有k个,自由元有n-k个,方程多解 /* 给定n个开关的初始状态si,要求将其变成目标状态di 规定: 每 ...
- Java 获取屏幕的宽、高
import java.awt.Toolkit; public class GetScreenSize { public static void main(String[] args) { int s ...
- 51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- ID3算法下的决策树
网上的内容感觉又多又乱,自己写一篇决策树算法.希望对别人有所启发,对自己也是一种进步. 决策树 须知概念 信息熵 & 信息增益 熵: 熵(entropy)指的是体系的混乱的程度,在不同的学科中 ...
- 一脸懵逼学习HBase的搭建(注意HBase的版本)
1:Hdfs分布式文件系统存的文件,文件存储. 2:Hbase是存储的数据,海量数据存储,作用是缓存的数据,将缓存的数据满后写入到Hdfs中. 3:hbase集群中的角色: ().一个或者多个主节点, ...
- EF Core Migration
//添加migrations dotnet ef migrations add [名称] //根据model更新sql表结构 dotnet ef database update //删除最新的migr ...
- python之requests urllib3 连接池
0.目录 1.参考 2. pool_connections 默认值为10,一个站点主机host对应一个pool (4)分析 host A>>host B>>host A pag ...