日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Universal-Image-Loader(UIL)图片载入框架使用简介

發(fā)布時(shí)間:2024/10/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Universal-Image-Loader(UIL)图片载入框架使用简介 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這個(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使用的是單例模式:

public class MyApplication extends Application{private static MyApplication instance=null;@Overridepublic void onCreate() {super.onCreate();this.instance=this;initImageLoader(getApplicationContext());}public static MyApplication getInstance(){return instance;}private void initImageLoader(Context context){//cacheDir這里是獲取到他默認(rèn)的本地緩存文件夾。這StorageUtils是他這個(gè)imageloader里面的工具類,默認(rèn)的緩存文件夾是包名/cache文件夾下(當(dāng)然自己能夠改變)File cacheDir = StorageUtils.getCacheDirectory(context);ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context).threadPoolSize(5)//線程池.diskCache(new UnlimitedDiskCache(cacheDir))//內(nèi)存卡.threadPriority(Thread.NORM_PRIORITY -2)//線程優(yōu)先級(jí).denyCacheImageMultipleSizesInMemory().memoryCache(new LargestLimitedMemoryCache(2 * 1024 * 1024))//內(nèi)存緩存.memoryCacheSize(2 * 1024 * 1024)//內(nèi)存緩存大小.diskCacheSize(50 * 1024 * 1024)//存儲(chǔ)卡緩存大小.diskCacheFileCount(100)//存儲(chǔ)卡文件個(gè)數(shù).memoryCacheSizePercentage(13) // default.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // default.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default//.writeDebugLogs().tasksProcessingOrder(QueueProcessingType.FIFO) //先進(jìn)先出.build();ImageLoader.getInstance().init(configuration);}}

接下來(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)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。