日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2024/10/12 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Universal-Image-Loader(UIL)图片载入框架使用简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個也是近期項目中使用到的第三方圖片載入框架。在這里也自己總結一下,簡單的介紹一些使用的方式。

UIL圖片載入框架特點

簡單介紹:
  • 項目地址:https://github.com/nostra13/Android-Universal-Image-Loader
  • 異步載入圖片或者載入大量圖片常常會遇到圖片錯亂或者OOM等相關問題。UIL圖片緩存,眼下使用最廣泛的圖片緩存。支持主流圖片緩存的絕大多數特性。
    我們看下該圖片載入的三級緩存原理
特點:
1.多線程下載圖片。圖片能夠來源于網絡,文件系統,項目文件夾assets中以及drawable中等2.支持任意的配置ImageLoader,比如線程池。圖片下載器,內存緩存策略,硬盤緩存策略,圖片顯示選項以及其它的一些配置3.支持圖片的內存緩存,文件系統緩存或者SD卡緩存4.支持圖片下載過程的監聽5.依據控件(ImageView)的大小對Bitmap進行裁剪,降低Bitmap占用過多的內存6.較好的控制圖片的載入過程,比如暫停圖片載入。又一次開始載入圖片,一般使用在ListView,GridView中。滑動過程中暫停載入圖片。停止滑動的時候去載入圖片7.提供在較慢的網絡下對圖片進行載入
  • 當然了哈,主流的圖片載入還有其它的幾個
    • Picasso
    • Cube ImageLoader
    • Fresco 這個能夠看看我的還有一篇(Fresco圖片載入框架的簡單介紹)
    • Glide

UIL圖片載入框架使用解說

第一步。項目引入

ImageLoader Jar包引入項目中:https://github.com/nostra13/Android-Universal-Image-Loader/raw/master/downloads/universal-image-loader-1.9.5.jar
或者是下載這個項目,然后導入到project中。使用庫依賴的方式進行引用,假設還不太懂怎么導入demo和庫依賴,能夠看下
AndroidStudio導入本地和github項目,以及怎么加入第三方依賴介紹

第二步
配置ImageLoder參數(ImageLoaderConfiguration)ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this);
第三步
初始化ImageLoader ImageLoader.getInstance()
第四步
displayImage(), loadImage(),loadImageSync()

好了,我們開始載入圖片吧。
這個時候,我們須要配置imageloader的參數。也就是在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這里是獲取到他默認的本地緩存文件夾。這StorageUtils是他這個imageloader里面的工具類,默認的緩存文件夾是包名/cache文件夾下(當然自己能夠改變)File cacheDir = StorageUtils.getCacheDirectory(context);ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context).threadPoolSize(5)//線程池.diskCache(new UnlimitedDiskCache(cacheDir))//內存卡.threadPriority(Thread.NORM_PRIORITY -2)//線程優先級.denyCacheImageMultipleSizesInMemory().memoryCache(new LargestLimitedMemoryCache(2 * 1024 * 1024))//內存緩存.memoryCacheSize(2 * 1024 * 1024)//內存緩存大小.diskCacheSize(50 * 1024 * 1024)//存儲卡緩存大小.diskCacheFileCount(100)//存儲卡文件個數.memoryCacheSizePercentage(13) // default.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // default.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default//.writeDebugLogs().tasksProcessingOrder(QueueProcessingType.FIFO) //先進先出.build();ImageLoader.getInstance().init(configuration);}}

接下來,我們就要獲取imageloader的實例。和設置DisplayImageOptions的參數,這里我附上一張DisplayImageOptions配置圖:

mImageLoader = ImageLoader.getInstance();mOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher)//圖片載入的時候顯示的默認圖.showImageForEmptyUri(R.mipmap.ic_launcher)//圖片的地址為空的時候顯示的圖.showImageOnFail(R.mipmap.ic_launcher)//圖片載入失敗的時候顯示.cacheOnDisk(true) //設置保存在sdcard中.cacheInMemory(true) //設置保存在內存其中.build();

最后我們就要載入圖片了:
載入之前:

載入成功后:
是不是非常easy呢。并且配置也是通俗易懂的。只是不幸的是,這個框架。已經停止了更新,只是我相信,這么優秀的開源框架,還是會有非常多人記著的。

當然了哈,我的項目中用到的也就是載入圖片,并沒實用到其它的厲害的方法,比方

// 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);

好了,想要了解很多其它的有關Universal-Image-Loader,能夠去官網下載下來慢慢研究,這里就不多做解釋了哈。

轉載于:https://www.cnblogs.com/blfbuaa/p/7083627.html

總結

以上是生活随笔為你收集整理的Universal-Image-Loader(UIL)图片载入框架使用简介的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。