ImageLoader 详解
ImageLoader 詳解
應(yīng)用情景
- 網(wǎng)絡(luò)加載大量圖片
注意事項(xiàng)
- 1.自定義 application 必須在清單文件只能配置 Application android:name 屬性
- 因圖片等資源在網(wǎng)絡(luò)中加載,需配置 internet 權(quán)限
- 因設(shè)置本地緩存,會(huì)在本地寫入內(nèi)容,需要配置 寫SD卡的權(quán)限
- 4.一般我們?cè)谑褂肐mageLoader的時(shí)候,需要在應(yīng)用程序的入口進(jìn)行它的一個(gè)配置,這個(gè)配置一般寫到Application里邊
- 在Application 和 Activity 中都用了 ImageLoader,在Application中通過(guò) ImageLoader.getInstance().init(ImageLoaderConfiguration config),來(lái)初始化 ImageLoader 操作;而在Activity 中通過(guò) ImageLoader.getInstance().displayImage(String url, ImageView iv,DisplayImageOptions options)來(lái)設(shè)置圖片。可以看到共同點(diǎn):ImageLoader.getInstance(),其后為init()和display()分別執(zhí)行初始化和展示操作
注意方法
getInstance()
得到ImageLoader的單例。通過(guò)雙層是否為 null 判斷提高性能
init(ImageLoaderConfiguration configuration)
初始化配置參數(shù),參數(shù)configuration為ImageLoader的配置信息,包括圖片最大尺寸、任務(wù)線程池、磁盤緩存、下載器、解碼器等等。
實(shí)現(xiàn)中會(huì)初始化ImageLoaderEngine engine屬性,該屬性為任務(wù)分發(fā)器。displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener)
加載并顯示圖片或加載并執(zhí)行回調(diào)接口
使用詳解__步驟
- 導(dǎo)入安裝包 universal-image-loader-1.9.3.jar
2.在app 初始化的時(shí)候設(shè)置 ImageLoaderConfiguration,并初始化 ImageLoader(采用自定義的 Application ,在其里面的 onCreate()方法里面進(jìn)行處理)
- 2.1 獲取 ImageLoaderConfiguration 對(duì)象的內(nèi)部類 Builder
ImageLoaderConfiguration.Builder builder = new Builder(
getApplicationContext()); 2.2 設(shè)置相關(guān)參數(shù)
- builder.threadPoolSize(3);// 設(shè)置線程數(shù)量
- builder.threadPriority(Thread.NORM_PRIORITY - 2);// 設(shè)置線程優(yōu)先級(jí)
- builder.memoryCacheSize(2 * 1024 * 1024);// 設(shè)置緩存空間
- builder.memoryCacheExtraOptions(480, 800);// 設(shè)置緩存中的圖片寬高
- builder.diskCacheSize(50 * 1024 * 1024); // 設(shè)置緩存空間 50M (磁盤中)
- builder.diskCache(new UnlimitedDiscCache(new File(path)));// 自定義文件的緩存路徑
- String path=Environment.getExternalStorageDirectory().getAbsolutePath()+”…”;
builder.diskCacheFileNameGenerator(new Md5FileNameGenerator());//磁盤緩存的文件名的命名方式
- 一般使用默認(rèn)值 (獲取文件名稱的hashcode然后轉(zhuǎn)換成字符串)
或MD5 new Md5FileNameGenerator()源文件的名稱同過(guò)md5加密后保存
builder.denyCacheImageMultipleSizesInMemory();
- builder.tasksProcessingOrder(QueueProcessingType.LIFO);
加載同一URL圖片是,imageView 從小變大 ,從內(nèi)存緩存中加載
- 2.1 獲取 ImageLoaderConfiguration 對(duì)象的內(nèi)部類 Builder
- 2.3 獲取ImageLoaderConfiguration 對(duì)象
- ImageLoaderConfiguration config = builder.build();
- 2.4 初始化 ImageLoader
- ImageLoader.getInstance().init(config);
- 3.在活動(dòng)中設(shè)置 DisplayImageOptions
- 3.1獲取 DisplayImageOptions 對(duì)象的內(nèi)部類 Builder
- DisplayImageOptions.Builder builder=new Builder();
- 3.2 設(shè)置使用緩存 在內(nèi)存中
- builder.cacheInMemory(true);
- 3.3 設(shè)置使用緩存在 SD 卡中
- builder.cacheOnDisc(true);
- 3.4 其他設(shè)置 非必須 默認(rèn)圖片 在 drawable中進(jìn)行加載
- 設(shè)置圖片為空時(shí),默認(rèn)圖片
- builder.showImageForEmptyUri(R.drawable.ic_empty);
- 設(shè)置加載失敗的默認(rèn)圖片
- builder.showImageOnFail(R.drawable.ic_error);
- 設(shè)置正在加載的默認(rèn)圖片
- builder.showImageOnLoading(R.drawable.ic_stub);
- builder設(shè)置圓角
- builder.displayer(new RoundedBitmapDisplayer(20));
- 設(shè)置圖片為空時(shí),默認(rèn)圖片
- 3.5 獲取 DisplayImageOptions 對(duì)象
- DisplayImageOptions options = builder.build();
- 3.1獲取 DisplayImageOptions 對(duì)象的內(nèi)部類 Builder
- 4.展示圖片
- 可設(shè)置樣式 ImageLoader.getInstance().displayImage(String url, ImageView iv,DisplayImageOptions options);
- 默認(rèn)樣式 ImageLoader.getInstance().displayImage(String url, ImageView iv)
- 5.相關(guān)配置
- 權(quán)限配置
- 訪問(wèn)Intent _
- 寫內(nèi)存卡 __
- application 配置
- 使用自定義的 Application,將默認(rèn)application設(shè)置改為自定義的application,重新配置name,android:name=”com.example.and4_imageloader_demo1.MyApplication”
- 權(quán)限配置
ImageLoader 配置介紹
- ImageLoaderConfiguration:
是針對(duì)圖片緩存的全局配置,主要有線程類、緩存大小、磁盤大小、圖片下載與解析、日志方面的配置
- DisplayImageOptions:
- ImageLoader:
用于指導(dǎo)每一個(gè)Imageloader根據(jù)網(wǎng)絡(luò)圖片的狀態(tài)(空白、下載錯(cuò)誤、正在下載)顯示對(duì)應(yīng)的圖片,是否將緩存加載到磁盤上,下載完后對(duì)圖片進(jìn)行怎么樣的處理
是具體下載圖片,緩存圖片,顯示圖片的具體執(zhí)行類,它有兩個(gè)具體的方法displayImage(…)、loadImage(…),但是其實(shí)最終他們的實(shí)現(xiàn)都是displayImage(…)
從三者的協(xié)作關(guān)系上看,他們有點(diǎn)像廚房規(guī)定、廚師、客戶個(gè)人口味之間的關(guān)系ImageLoaderConfiguration就像是廚房里面的規(guī)定,每一個(gè)廚師要怎么著裝,要怎么保持廚房的干凈,這是針對(duì)每一個(gè)廚師都適用的規(guī)定,而且不允許個(gè)性化改變。ImageLoader就像是具體做菜的廚師,負(fù)責(zé)具體菜譜的制作。DisplayImageOptions就像每個(gè)客戶的偏好,根據(jù)客戶是重口味還是清淡,每一個(gè)imageLoader根據(jù)DisplayImageOptions的要求具體執(zhí)行
相關(guān)概念
- 為Google 為 Android 所做的開源框架
- 下載地址 https://github.com/nostra13/Android-Universal-Image-Loader
- 安裝包 universal-image-loader-1.9.3.jar
Android Universal Image Loader 是一個(gè)強(qiáng)大的、可高度定制的圖片緩存
多線程異步加載和顯示圖片(圖片來(lái)源于網(wǎng)絡(luò)、sd卡、assets文件夾,drawable文件夾(不能加載9patch),新增加載視頻縮略圖)
特點(diǎn)
- 可配置高。支持任務(wù)線程池、下載器、解碼器、內(nèi)存和磁盤的緩沖、顯示選項(xiàng)等等的配置。 (ImageLoaderConfiguration 里面配置)
- 包含內(nèi)存緩沖和磁盤緩沖兩級(jí)緩沖。(ImageLoaderConfiguration、DisplayImageOptions 里面配置)
- 支持多線程,支持異步和同步加載(ImageLoaderConfiguration 里面配置)
- 支持多種緩沖算法、下載進(jìn)度監(jiān)聽、ListView圖片錯(cuò)亂解決等。???
出現(xiàn)背景
面臨問(wèn)題
- 我們經(jīng)常會(huì)從網(wǎng)絡(luò)(SD卡)中加載大量的圖片,如果處理不好,經(jīng)常會(huì)出現(xiàn)內(nèi)存溢出(OutOfMemoryError),導(dǎo)致app崩潰,還有下載速度慢等問(wèn)題…
常用解決方式:
- 對(duì)圖片進(jìn)行壓縮(BitmapFactory.Options)
- 及時(shí)回收 Bitmap 的內(nèi)存(Bitmap.recyle());
- 緩存圖片(緩存到內(nèi)存、SD卡等[Map
更好的解決方案
- ImageLoader 基本處理了該類問(wèn)題,避免了這些問(wèn)題,下載速度快,還有很好的緩沖管理機(jī)制
示例代碼
布局文件
activity_main.xml
`<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"/> </RelativeLayout>`list_item.xml
`<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv"android:layout_width="72dp"android:layout_height="72dp"android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/tv"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="測(cè)試"android:layout_gravity="center"/> </LinearLayout>`自定義 Application
`import java.io.File; import android.app.Application; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; public class MyApplication extends Application { @Override public void onCreate() {super.onCreate();ImageLoaderConfiguration.Builder builder = new Builder(getApplicationContext());builder.threadPoolSize(3);// 設(shè)置線程數(shù)量builder.threadPriority(Thread.NORM_PRIORITY - 2);// 設(shè)置線程優(yōu)先級(jí)builder.memoryCacheSize(2 * 1024 * 1024);// 設(shè)置緩存空間// 設(shè)置緩存中的圖片寬高builder.memoryCacheExtraOptions(480, 800);// 設(shè)置緩存空間 50M (磁盤中)builder.diskCacheSize(50 * 1024 * 1024);// 自定義文件的緩存路徑builder.diskCache(new UnlimitedDiscCache(new File("")));//磁盤緩存的文件名的命名方式//一般使用默認(rèn)值 (獲取文件名稱的hashcode然后轉(zhuǎn)換成字符串)//或MD5 new Md5FileNameGenerator()源文件的名稱同過(guò)md5加密后保存builder.diskCacheFileNameGenerator(new Md5FileNameGenerator());//加載同一URL圖片是,imageView 從小變大 ,從內(nèi)存緩存中加載builder.denyCacheImageMultipleSizesInMemory();builder.tasksProcessingOrder(QueueProcessingType.LIFO);ImageLoaderConfiguration config = builder.build();//2. 初始化 ImageLoaderImageLoader.getInstance().init(config); }}`圖片網(wǎng)絡(luò)地址
`package com.example.and4_imageloader_demo1;public class Constants {public static final String IMAGES = "CONSTANTS.IMAGES"; public static final String IMAGE_POSITION = "CONSTANTS.IMAGE_POSITION";public static final String[] images = new String[] {"http://cdn.duitang.com/uploads/blog/201308/18/20130818150526_Ru2Bk.thumb.600_0.png","http://www.bkill.com/u/info_img/2012-09/02/2012083116140522302.jpg","http://www.it165.net/uploadfile/2011/1218/20111218070928328.jpg","http://www.3761.com/uploads/pic/42861391998071.jpg","http://www.jhq8.cn/qqtouxiang/UploadPic/2012-9/201291016107737.jpg","http://www.3761.com/uploads/pic/71781391998073.jpg","http://www.qqcan.com/uploads/allimg/c120822/13455c923250-91E45.jpg","http://p1.qq181.com/cms/120503/2012050320291269450.jpg","http://www.qqbody.com/uploads/allimg/201401/20-094917_95.jpg","http://www.qqbody.com/uploads/allimg/201301/15-200525_65.jpg","http://www.qqgqtx.com/uploads/allimg/130117/1-13011F61119.jpg","http://www.xk77.com/uploads/allimg/111115/1_111115124125_6.jpg","http://www.3761.com/uploads/pic/7761379903040.jpg","http://www.qqai.net/fa/UploadPic/2012-8/2012829161638939.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqya.com/userimg/2455/110514234111.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121125/25-032807_173.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121204/04-013352_840.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121204/04-013953_608.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqphotos.com/uploads/allimg/1401/2-140112112226.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-130326094313-52.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.3761.com/uploads/pic/32201377656359.png","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qjis.com/uploads/allimg/121027/130Z635X-3.png","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130928110958_zmzdn.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.jhq8.cn/qqtouxiang/UploadPic/2012-11/2012119154647323.jpg","http://www.qqbody.com/uploads/allimg/201301/12-202555_717.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg" }; } `活動(dòng)代碼
`import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; public class MainActivity extends Activity {private ListView listView; private List<String> list;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);list = new ArrayList<String>();String[] str = Constants.images;for (String string : str) {list.add(string);}//設(shè)置 DisplayImageOptionsDisplayImageOptions.Builder builder=new Builder();//設(shè)置圖片為空時(shí),默認(rèn)圖片builder.showImageForEmptyUri(R.drawable.ic_empty);//設(shè)置加載失敗的默認(rèn)圖片builder.showImageOnFail(R.drawable.ic_error);//設(shè)置正在加載的默認(rèn)圖片builder.showImageOnLoading(R.drawable.ic_stub);//設(shè)置使用緩存 在內(nèi)存中builder.cacheInMemory(true);//設(shè)置使用緩存在 SD 卡中builder.cacheOnDisc(true);//builder設(shè)置圓角builder.displayer(new RoundedBitmapDisplayer(20));DisplayImageOptions options = builder.build();listView = (ListView) findViewById(R.id.listView);listView.setAdapter(new MyAdapter(MainActivity.this, list,options)); }class MyAdapter extends BaseAdapter {private Context context;private List<String> list;private DisplayImageOptions options;public MyAdapter(Context context, List<String> list, DisplayImageOptions options) {this.context = context;this.list = list;this.options=options;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView == null) {holder = new ViewHolder();convertView = View.inflate(context, R.layout.list_item, null);holder.iv = (ImageView) convertView.findViewById(R.id.iv);holder.tv = (TextView) convertView.findViewById(R.id.tv);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}ImageLoader.getInstance().displayImage(list.get(position), holder.iv,options);holder.tv.setText("item" + position);return convertView;}class ViewHolder {private ImageView iv;private TextView tv;}}}`配置清單文件
`<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.and4_imageloader_demo1" android:versionCode="1" android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/><application android:name="com.example.and4_imageloader_demo1.MyApplication"android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activity android:name="com.example.and4_imageloader_demo1.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity> </application> </manifest>`關(guān)聯(lián)源碼 參考v4 包中 ViewPager ,相同的方式
可成功:5.1.刪除 libs-->android-support-v4.jar , 進(jìn)入 JavaBuildPath -->Libraries --->選擇 Android Dependencies -->Remove 5.2.右擊項(xiàng)目 ---> Properties 進(jìn)入 JavaBuildPath -->Libraries-->AddExternal JARs.. 選擇 sdk\extras\android\support\v4 android-support-v4.jar,點(diǎn)擊OK 5.3進(jìn)入 JavaBuildPath -->Order and Export --->勾選 Android 6.0 ,android-support-v4.jar 點(diǎn)擊AttachSource --->External Folder-->選擇sdk\extras\android\support\v4路徑下 android-support-v4.jar,點(diǎn)擊OK.可成功: 5.1 右擊項(xiàng)目 ---> Properties 進(jìn)入 JavaBuildPath -->Libraries-->選擇 Android Dependencies -->Remove 5.2 右擊 libs-->android-support-v4.jar -----》BuildPath 5.3 進(jìn)入 JavaBuildPath -->Order and Export --->勾選 Android 6.0 ,android-support-v4.jar提升 源碼
init方法
`public synchronized void init(ImageLoaderConfiguration configuration) {if (configuration == null) {throw new IllegalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);}if (this.configuration == null) {L.d(LOG_INIT_CONFIG);engine = new ImageLoaderEngine(configuration);this.configuration = configuration;} else {L.w(WARNING_RE_INIT_CONFIG);} } `getInstance(); 如何保證為單例模式???
` public static ImageLoader getInstance() {if (instance == null) {synchronized (ImageLoader.class) {if (instance == null) {instance = new ImageLoader();}}}return instance; }`ImageLoaderEngine()方法
` ImageLoaderEngine(ImageLoaderConfiguration configuration) {this.configuration = configuration;taskExecutor = configuration.taskExecutor;taskExecutorForCachedImages = configuration.taskExecutorForCachedImages;taskDistributor = DefaultConfigurationFactory.createTaskDistributor(); }`displayImage()方法
`public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) {displayImage(uri, new ImageViewAware(imageView), options, null, null); }`displayImage 深入源碼
`public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {checkConfiguration();if (imageAware == null) {throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);}if (listener == null) {listener = emptyListener;}if (options == null) {options = configuration.defaultDisplayImageOptions;}if (TextUtils.isEmpty(uri)) {engine.cancelDisplayTaskFor(imageAware);listener.onLoadingStarted(uri, imageAware.getWrappedView());if (options.shouldShowImageForEmptyUri()) {imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));} else {imageAware.setImageDrawable(null);}listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);return;}ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);listener.onLoadingStarted(uri, imageAware.getWrappedView());Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);if (bmp != null && !bmp.isRecycled()) {L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);if (options.shouldPostProcess()) {ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,options, listener, progressListener, engine.getLockForUri(uri));ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo,defineHandler(options));if (options.isSyncLoading()) {displayTask.run();} else {engine.submit(displayTask);}} else {options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);}} else {if (options.shouldShowImageOnLoading()) {imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));} else if (options.isResetViewBeforeLoading()) {imageAware.setImageDrawable(null);}ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,options, listener, progressListener, engine.getLockForUri(uri));LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,defineHandler(options));if (options.isSyncLoading()) {displayTask.run();} else {engine.submit(displayTask);}} }`
總結(jié)
以上是生活随笔為你收集整理的ImageLoader 详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 项目需求分析和项目可行性
- 下一篇: 云原生入门 第五章:kubernetes