当前位置: 移动技术网 > 移动技术>移动开发>Android > Android Universal ImageLoader 缓存图片

Android Universal ImageLoader 缓存图片

2019年07月24日  | 移动技术网移动技术  | 我要评论

项目介绍:

android上最让人头疼的莫过于从网络获取图片、显示、回收,任何一个环节有问题都可能直接oom,这个项目或许能帮到你。universal image loader for android的目的是为了实现异步的网络图片加载、缓存及显示,支持多线程异步加载。它最初来源于fedor vlasov的项目,且自此之后,经过大规模的重构和改进。

特性列举:

多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
支持随意的配置imageloader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
支持图片的内存缓存,文件系统缓存或者sd卡缓存
支持图片下载过程的监听
根据控件(imageview)的大小对bitmap进行裁剪,减少bitmap占用过多的内存
较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在listview,gridview中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
提供在较慢的网络下对图片进行加载

使用过程:

创建默认的imageloader,所有的操作都由imageloader控制。该类使用单例设计模式,所以如果要获取该类的实力,需要调用getinstance()方法。在使用imageloader显示图片之前,你首先要初始化它的配置,调用imageloaderconfiguration的init()方法,然后你就可以实现各种的显示了。

//创建默认的imageloader配置参数 
imageloaderconfiguration configuration = imageloaderconfiguration 
.createdefault(this); 
//initialize imageloader with configuration. 
imageloader.getinstance().init(configuration); 

自定义配置imageloader, 就像你已经知道的,首先,你需要使用imageloaderconfiguration对象来初始化imageloader。由于imageloader是单例,所以在程序开始的时候只需要初始化一次就好了。建议你在activity的oncreate()方法中初始化。如果一个imageloader已经初始化过,再次初始化不会有任何效果。下面我们通过imageloaderconfiguration.builder创建一个设置

file cachedir =storageutils.getowncachedirectory(this, "imageloader/cache"); 
imageloaderconfigurationconfig = new imageloaderconfiguration 
.builder(this) 
.memorycacheextraoptions(480, 800) // maxwidth, max height,即保存的每个缓存文件的最大长宽 
.threadpoolsize(3)//线程池内加载的数量 
.threadpriority(thread.norm_priority -2) 
.denycacheimagemultiplesizesinmemory() 
.memorycache(new usingfreqlimitedmemorycache(2* 1024 * 1024)) // you can pass your own memory cache implementation/你可以通过自己的内存缓存实现 
.memorycachesize(2 * 1024 * 1024) 
.disccachesize(50 * 1024 * 1024) 
.disccachefilenamegenerator(newmd5filenamegenerator())//将保存的时候的uri名称用md5 加密 
.tasksprocessingorder(queueprocessingtype.lifo) 
.disccachefilecount(100) //缓存的文件数量 
.disccache(new unlimiteddisccache(cachedir))//自定义缓存路径 
.defaultdisplayimageoptions(displayimageoptions.createsimple()) 
.imagedownloader(new baseimagedownloader(this,5 * 1000, 30 * 1000)) // connecttimeout (5 s), readtimeout (30 s)超时时间 
.writedebuglogs() // remove for releaseapp 
.build();//开始构建 
imageloader.getinstance().init(config); 

得到imageloader

imageloader imageloader imageloader = imageloader.getinstance(); 

使用过程:

(1)图像操作是否参与缓存以及图像效果的配置操作

displayimageoptions options = new displayimageoptions.builder() 
.showimageonloading(r.drawable.ic_stub) //加载图片时的图片 
.showimageforemptyuri(r.drawable.ic_empty) //没有图片资源时的默认图片 
.showimageonfail(r.drawable.ic_error) //加载失败时的图片 
.cacheinmemory(true) //启用内存缓存 
.cacheondisk(true) //启用外存缓存 
.considerexifparams(true) //启用exif和jpeg图像格式 
.displayer(new roundedbitmapdisplayer(20)) //设置显示风格这里是圆角矩形 
.build(); 

displayimageoptions以下是所有默认配置参数根据需求可以自定义配置

private int imageresonloading = 0; 
private int imageresforemptyuri = 0; 
private int imageresonfail = 0; 
private drawable imageonloading = null; 
private drawable imageforemptyuri = null; 
private drawable imageonfail = null; 
private boolean resetviewbeforeloading = false; 
private boolean cacheinmemory = false; 
private boolean cacheondisk = false; 
private imagescaletype imagescaletype = imagescaletype.in_sample_power_of_2; 
private options decodingoptions = new options(); 
private int delaybeforeloading = 0; 
private boolean considerexifparams = false; 
private object extrafordownloader = null; 
private bitmapprocessor preprocessor = null; 
private bitmapprocessor postprocessor = null; 
private bitmapdisplayer displayer = defaultconfigurationfactory.createbitmapdisplayer(); 
private handler handler = null; 
private boolean issyncloading = false; 

(2)图片加载监听器在这里吧可以设置加载时的动画或者进度条之类的东西这里

imageloadinglistener animatefirstlistener = new animatefirstdisplaylistener(); 
private static class animatefirstdisplaylistener extends simpleimageloadinglistener { 
static final list<string> displayedimages = collections.synchronizedlist(new linkedlist<string>()); 
@override 
public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { 
if (loadedimage != null) { 
imageview imageview = (imageview) view; 
boolean firstdisplay = !displayedimages.contains(imageuri); 
if (firstdisplay) { 
fadeinbitmapdisplayer.animate(imageview, 500); 
displayedimages.add(imageuri); 
} 
} 
} 
}

(3)简单设置就可以给imageview添加图片了

imageloader.displayimage(imageurl, imageview, options, animatefirstlistener); 

对于本地的图片 ,在其绝对地址前面要加入"file://"。网络图片就直接写路径了。

由于我的这个是最新的包,可能跟以前老的版本不同,看到有些网友说的是:

string imageuri = "http://site.com/image.png"; // 网络图片 
string imageuri = "file:///mnt/sdcard/image.png"; //sd卡图片 
string imageuri = "content://media/external/audio/albumart/13"; // 媒体文件夹 
string imageuri = "assets://image.png"; // assets 
string imageuri = "drawable://" + r.drawable.image; // drawable文件 

缓存的清理:

缓存的清理可以按需求来定,可以再每个activity的生命周期函数ondestroy中清理也可以单独设置让用户自行清理。

@override 
public void ondestroy() { 
super.ondestroy(); 
imageloader.clearmemorycache(); 
imageloader.cleardiskcache(); 
}

girdview,listview加载图片:

相信大部分人都是使用gridview,listview来显示大量的图片,而当我们快速滑动gridview,listview,我们希望能停止图片的加载,而在gridview,listview停止滑动的时候加载当前界面的图片,这个框架当然也提供这个功能,使用起来也很简单,它提供了pauseonscrolllistener这个类来控制listview,gridview滑动过程中停止去加载图片,该类使用的是代理模式

listview.setonscrolllistener(new pauseonscrolllistener(imageloader, pauseonscroll, pauseonfling)); 
gridview.setonscrolllistener(new pauseonscrolllistener(imageloader, pauseonscroll, pauseonfling)); 

第一个参数就是我们的图片加载对象imageloader, 第二个是控制是否在滑动过程中暂停加载图片,如果需要暂停传true就行了,第三个参数控制猛的滑动界面的时候图片是否加载

outofmemoryerror:

虽然这个框架有很好的缓存机制,有效的避免了oom的产生,一般的情况下产生oom的概率比较小,但是并不能保证outofmemoryerror永远不发生,这个框架对于outofmemoryerror做了简单的catch,保证我们的程序遇到oom而不被crash掉,但是如果我们使用该框架经常发生oom,我们应该怎么去改善呢?

减少线程池中线程的个数,在imageloaderconfiguration中的(.threadpoolsize)中配置,推荐配置1-5

在displayimageoptions选项中配置bitmapconfig为bitmap.config.rgb_565,因为默认是argb_8888, 使用rgb_565会比使用argb_8888少消耗2倍的内存

在imageloaderconfiguration中配置图片的内存缓存为memorycache(newweakmemorycache()) 或者不使用内存缓存

在displayimageoptions选项中设置.imagescaletype(imagescaletype.in_sample_int)或者imagescaletype(imagescaletype.exactly)

通过上面这些,相信大家对universal-image-loader框架的使用已经非常的了解了,我们在使用该框架的时候尽量的使用displayimage()方法去加载图片,loadimage()是将图片对象回调到imageloadinglistener接口的onloadingcomplete()方法中,需要我们手动去设置到imageview上面,displayimage()方法中,对imageview对象使用的是weak references,方便垃圾回收器回收imageview对象,如果我们要加载固定大小的图片的时候,使用loadimage()方法需要传递一个imagesize对象,而displayimage()方法会根据imageview对象的测量值,或者android:layout_width and android:layout_height设定的值,或者android:maxwidth and/or android:maxheight设定的值来裁剪图片

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网