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 缓存图片的配置类及使用方法的更多相关文章

  1. LindAgile~缓存拦截器支持类的虚方法了

    写它的原因 之前写过一个缓存拦截器,主要在方法上添加CachingAspect特性之后,它的返回值就可以被缓存下来,下次访问时直接从缓存中返回结果,而它有一个前提,就是你的方法需要是一个接口方法,缓存 ...

  2. 最全的C#图片处理帮助类ImageHelper

    最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...

  3. spring 配置 Java配置类装配bean

    https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...

  4. springboot项目启动之后初始化自定义配置类

    前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...

  5. 【Spring】简述@Configuration配置类注册BeanDefinition到Spring容器的过程

    概述 本文以SpringBoot应用为基础,尝试分析基于注解@Configuration的配置类是如何向Spring容器注册BeanDefinition的过程 其中主要分析了 Configuratio ...

  6. 真懂Spring的@Configuration配置类?你可能自我感觉太良好

    当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...

  7. Android-Universal-Image-Loader的缓存处理机制与使用 LruCache 缓存图片

    讲到缓存,平时流水线上的码农一定觉得这是一个高大上的东西.看过网上各种讲缓存原理的文章,总感觉那些文章讲的就是玩具,能用吗?这次我将带你一起看过UIL这个国内外大牛都追捧的图片缓存类库的缓存处理机制. ...

  8. Android之简单了解Bitmap显示图片及缓存图片

    昨天我们学了如何连接网络,今天我们就学习一下如何从把网上图片显示到项目中 今天主要用到的是Bitmap 类 Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图 ...

  9. Android 使用 LruCache 缓存图片

    在你应用程序的 UI 界面加载一张图片是一件很简单的事情,但是当你需要在界面上加载一大堆图片的时候,情况就变得复杂起来.在很多情况下,(比如使用 ListView, GridView 或者 ViewP ...

随机推荐

  1. AngularJs中,如何在ng-repeat完成之后,执行Js脚本

    //ng-repeat生成4个li,生成后再执行自定义方法fn在每个li后加一根横线 <script> var myapp=angular.module('myapp',[]); myap ...

  2. Python基础之面向对象进阶一

    一.isinstance(obj,cls)和issubclass(sub,super) 1.isinstance(obj,cls)检查obj是否是类 cls 的对象 class A: pass obj ...

  3. tensorflow(4):神经网络框架总结

    #总结神经网络框架 #1,搭建网络设计结构(前向传播) 文件forward.py def forward(x,regularizer): # 输入x 和正则化权重 w= b= y= return y ...

  4. 开放系统的直连式存储(Direct-Attached Storage,简称DAS)

    开放系统的直连式存储(Direct-Attached Storage,简称DAS)已经有近四十年的使用历史,随着用户数据的不断增长,尤其是数百GB以上时,其在备份.恢复.扩展.灾备等方面的问题变得日益 ...

  5. WCF与WebService的区别(转)

    1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...

  6. 原生开发小程序 和 wepy 、 mpvue, Taro 对比

    https://blog.csdn.net/yang450712123/article/details/79623518 mpvue https://www.cnblogs.com/bgwhite/p ...

  7. Eclipse中如何打开Map/Reduce Locations窗口

    Window->Show View->Other->MapReduce Tools,双击打开

  8. WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Exception in thread "main" java.io.IOException: No FileSystem for sc F

    1.执行脚本程序报如下所示的错误: [hadoop@slaver1 script_hadoop]$ hadoop jar web_click_mr_hive.jar com.bie.hive.mr.C ...

  9. eclipse4.2版本下面安装ADT,安装已经完成了,但没有ADT的那个图标显示

    如果安装过程没错,直接在Eclipse ->window->customize Perpective->Command Groups Availability 勾选andorid 选 ...

  10. Python_面向对象_类2

    类的几个装饰器方法: @classmethod (类方法):使之无法访问实例变量 class Animal(object): def __init__(self, name): self.name = ...