Universal-Image-Loader(UIL)图片载入框架使用简介
這個(gè)也是近期項(xiàng)目中使用到的第三方圖片載入框架。在這里也自己總結(jié)一下,簡(jiǎn)單的介紹一些使用的方式。
UIL圖片載入框架特點(diǎn)
簡(jiǎn)單介紹:
- 項(xiàng)目地址:https://github.com/nostra13/Android-Universal-Image-Loader
- 異步載入圖片或者載入大量圖片常常會(huì)遇到圖片錯(cuò)亂或者OOM等相關(guān)問(wèn)題。UIL圖片緩存,眼下使用最廣泛的圖片緩存。支持主流圖片緩存的絕大多數(shù)特性。
我們看下該圖片載入的三級(jí)緩存原理
特點(diǎn):
1.多線程下載圖片。圖片能夠來(lái)源于網(wǎng)絡(luò),文件系統(tǒng),項(xiàng)目文件夾assets中以及drawable中等2.支持任意的配置ImageLoader,比如線程池。圖片下載器,內(nèi)存緩存策略,硬盤緩存策略,圖片顯示選項(xiàng)以及其它的一些配置3.支持圖片的內(nèi)存緩存,文件系統(tǒng)緩存或者SD卡緩存4.支持圖片下載過(guò)程的監(jiān)聽5.依據(jù)控件(ImageView)的大小對(duì)Bitmap進(jìn)行裁剪,降低Bitmap占用過(guò)多的內(nèi)存6.較好的控制圖片的載入過(guò)程,比如暫停圖片載入。又一次開始載入圖片,一般使用在ListView,GridView中。滑動(dòng)過(guò)程中暫停載入圖片。停止滑動(dòng)的時(shí)候去載入圖片7.提供在較慢的網(wǎng)絡(luò)下對(duì)圖片進(jìn)行載入- 當(dāng)然了哈,主流的圖片載入還有其它的幾個(gè)
- Picasso
- Cube ImageLoader
- Fresco 這個(gè)能夠看看我的還有一篇(Fresco圖片載入框架的簡(jiǎn)單介紹)
- Glide
UIL圖片載入框架使用解說(shuō)
第一步。項(xiàng)目引入
ImageLoader Jar包引入項(xiàng)目中:https://github.com/nostra13/Android-Universal-Image-Loader/raw/master/downloads/universal-image-loader-1.9.5.jar
或者是下載這個(gè)項(xiàng)目,然后導(dǎo)入到project中。使用庫(kù)依賴的方式進(jìn)行引用,假設(shè)還不太懂怎么導(dǎo)入demo和庫(kù)依賴,能夠看下
AndroidStudio導(dǎo)入本地和github項(xiàng)目,以及怎么加入第三方依賴介紹
第二步
配置ImageLoder參數(shù)(ImageLoaderConfiguration)ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this);第三步
初始化ImageLoader ImageLoader.getInstance()第四步
displayImage(), loadImage(),loadImageSync()好了,我們開始載入圖片吧。
這個(gè)時(shí)候,我們須要配置imageloader的參數(shù)。也就是在application里面配置。這里我們的application使用的是單例模式:
接下來(lái),我們就要獲取imageloader的實(shí)例。和設(shè)置DisplayImageOptions的參數(shù),這里我附上一張DisplayImageOptions配置圖:
mImageLoader = ImageLoader.getInstance();mOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher)//圖片載入的時(shí)候顯示的默認(rèn)圖.showImageForEmptyUri(R.mipmap.ic_launcher)//圖片的地址為空的時(shí)候顯示的圖.showImageOnFail(R.mipmap.ic_launcher)//圖片載入失敗的時(shí)候顯示.cacheOnDisk(true) //設(shè)置保存在sdcard中.cacheInMemory(true) //設(shè)置保存在內(nèi)存其中.build();最后我們就要載入圖片了:
載入之前:
載入成功后:
是不是非常easy呢。并且配置也是通俗易懂的。只是不幸的是,這個(gè)框架。已經(jīng)停止了更新,只是我相信,這么優(yōu)秀的開源框架,還是會(huì)有非常多人記著的。
當(dāng)然了哈,我的項(xiàng)目中用到的也就是載入圖片,并沒實(shí)用到其它的厲害的方法,比方
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view // which implements ImageAware interface) imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {@Overridepublic void onLoadingStarted(String imageUri, View view) {//開始載入...}@Overridepublic void onLoadingFailed(String imageUri, View view, FailReason failReason) {//載入失敗...}@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {//載入完畢...}@Overridepublic void onLoadingCancelled(String imageUri, View view) {//載入取消...} }, new ImageLoadingProgressListener() {@Overridepublic void onProgressUpdate(String imageUri, View view, int current, int total) {//載入百分比...} }); // Load image, decode it to Bitmap and return Bitmap to callback ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {// Do whatever you want with Bitmap} }); // Load image, decode it to Bitmap and return Bitmap synchronously ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);好了,想要了解很多其它的有關(guān)Universal-Image-Loader,能夠去官網(wǎng)下載下來(lái)慢慢研究,這里就不多做解釋了哈。
轉(zhuǎn)載于:https://www.cnblogs.com/blfbuaa/p/7083627.html
總結(jié)
以上是生活随笔為你收集整理的Universal-Image-Loader(UIL)图片载入框架使用简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 老骥伏枥志在千里 烈士暮年壮心不已(ld
- 下一篇: postgresql 排序索引