Universal-ImageLoader是一個第三方開源庫,主要用來異步加載圖片、緩存圖片、和圖片顯示。(獲取圖片顯示在相應的控件上)java
官網地址:https://github.com/nostra13/Android-Universal-Image-Loadergit
優勢:github
一、支持多線程下載圖片。圖片來源能夠是網絡、本地文件夾、assets和drawable緩存
二、支持隨意配置ImageLoader網絡
三、支持圖片的內存存儲,文件系統存儲或者SD卡存儲多線程
四、支持圖片下載過程的監聽異步
五、較好的控制圖片加載的過程。例如暫停圖片加載、從新開始圖片加載ide
七、提供在較慢的網絡下對圖片進行加載gradle
原理:動畫
相關概念:
ImageLoaderEngine:任務分發器,負責分發LoadAndDisplayImageTask
和ProcessAndDisplayImageTask
給具體的線程池去執行
ImageAware:顯示圖片的對象,能夠是ImageView
等。
ImageDownloader:圖片下載器,負責從圖片的各個來源獲取輸入流, 。
Cache:圖片緩存,分爲MemoryCache
和DiskCache
兩部分。
MemoryCache:內存圖片緩存,可向內存緩存緩存圖片或從內存緩存讀取圖片。
DiskCache:本地圖片緩存,可向本地磁盤緩存保存圖片或從本地磁盤讀取圖片。
ImageDecoder:圖片解碼器,負責將圖片輸入流InputStream
轉換爲Bitmap
對象。
BitmapProcessor:圖片處理器,負責從緩存讀取或寫入前對圖片進行處理。
BitmapDisplayer:將Bitmap
對象顯示在相應的控件ImageAware
上。
LoadAndDisplayImageTask:用於加載並顯示圖片的任務。
ProcessAndDisplayImageTask:用於處理並顯示圖片的任務,。
DisplayBitmapTask:用於顯示圖片的任務。
使用步驟:
a、gradle配置
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
b、初始化ImageLoderConfigulation
在Application的onCreate()方法中進行
/** 使用默認的配置方式 */ ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this); /*** 自定義配置方式 */ //設置緩存的路徑 File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache"); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext()) .memoryCacheExtraOptions(480, 800) //即保存的每一個緩存文件的最大長寬 .threadPoolSize(3) //線程池內加載的數量 .threadPriority(Thread.NORM_PRIORITY - 2)//當同一個Uri獲取不一樣大小的圖片,緩存到內存時,只緩存一 個。默認會緩存多個不一樣的大小的相同圖片 .denyCacheImageMultipleSizesInMemory() //拒絕緩存多個圖片。 .memoryCache(new WeakMemoryCache()) //緩存策略你能夠經過本身的內存緩存實現 ,這裏用弱引用,缺點是 太容易被回收了,不是很好! .memoryCacheSize(2 * 1024 * 1024) //設置內存緩存的大小 .diskCacheSize(50 * 1024 * 1024) //設置磁盤緩存大小 50M .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //將保存的時候的URI名稱用MD5 加密 .tasksProcessingOrder(QueueProcessingType.LIFO) //設置圖片下載和顯示的工做隊列排序 .diskCacheFileCount(100) //緩存的文件數量 .diskCache(new UnlimitedDiskCache(cacheDir)) //自定義緩存路徑 .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) //顯示圖片的參數,默認:Displa yImageOptions.createSimple() .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000)) // c onnectTimeout (5 s), readTimeout (30 s)超時時間 .writeDebugLogs() //打開調試日誌 .build(); ImageLoader.getInstance().init(configuration);
c、圖片顯示
設置顯示的Option
DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.loading) //設置圖片在下載期間顯示的圖片 .showImageForEmptyUri(R.mipmap.ic_launcher)//設置圖片Uri爲空或是錯誤的時候顯示的圖片 .showImageOnFail(R.mipmap.loading) //設置圖片加載/解碼過程當中錯誤時候顯示的圖片 .cacheInMemory(true)//設置下載的圖片是否緩存在內存中 .cacheOnDisk(true)//設置下載的圖片是否緩存在SD卡中 .considerExifParams(true) //是否考慮JPEG圖像EXIF參數(旋轉,翻轉) .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//設置圖片以如何的編碼方式顯示 .bitmapConfig(Bitmap.Config.RGB_565)//設置圖片的解碼類型 //.decodingOptions(BitmapFactory.Options decodingOptions)//設置圖片的解碼配置 .delayBeforeLoading(0)//int delayInMillis爲你設置的下載前的延遲時間 //設置圖片加入緩存前,對bitmap進行設置 //.preProcessor(BitmapProcessor preProcessor) .resetViewBeforeLoading(true)//設置圖片在下載前是否重置,復位 .displayer(new RoundedBitmapDisplayer(20))//不推薦用!!!!是否設置爲圓角,弧度爲多少 .displayer(new FadeInBitmapDisplayer(100))//是否圖片加載好後漸入的動畫時間,可能會出現閃動 .build();//構建完成
顯示
ImageLoader imageLoader = ImageLoader.getInstance(); DisplayImageOptions options = getWholeOptions(); imageLoader.displayImage(url, view, options);
顯示流程