Android Universal ImageLoader 缓存图片

Android Universal ImageLoader 缓存图片,第1张

概述项目介绍:Android上最让人头疼的莫过于从网络获取图片、显示、回收,任何一个环节有问题都可能直接OOM,这个项目或许能帮到你。UniversalImageLoaderforAndroid的目的是为了实现异步的网络图片加载、缓存及显示,支

项目介绍:

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,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设定的值来裁剪图片

总结

以上是内存溢出为你收集整理的Android Universal ImageLoader 缓存图片全部内容,希望文章能够帮你解决Android Universal ImageLoader 缓存图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1141795.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存