Android-Universal-Image-Loader三大組件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration詳解...

一、介紹

?Android-Universal-Image-Loader是 一個開源的UI組件程序,該項目的目的是提供一個可重復使用的儀器為異步圖像加載,緩存和顯示。所以,如果你的程序里需要這個功能的話,那么不妨試試它。 因為已經封裝好了一些類和方法。我們 可以直接拿來用了。而不用重復去寫了。其實,寫一個這方面的程序還是比較麻煩的,要考慮多線程緩存,內存溢出等很多方面。

二、具體使用

一個好的類庫的重要特征就是可配置性強。我們先簡單使用Android-Universal-Image-Loader,一般情況下使用默認配置就可以了。

下面的實例利用Android-Universal-Image-Loader將網絡圖片加載到圖片墻中。

復制代碼
 1 public class BaseActivity extends Activity {
 2  ImageLoader imageLoader;  3  @Override  4 protected void onCreate(Bundle savedInstanceState) {  5 // Create global configuration and initialize ImageLoader with this configuration  6 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())  7  .build();  8  ImageLoader.getInstance().init(config);  9 super.onCreate(savedInstanceState); 10  } 11 } 
復制代碼
復制代碼
  1 public class MainActivity extends BaseActivity {
 2  3  @Override  4 protected void onCreate(Bundle savedInstanceState) {  5 super.onCreate(savedInstanceState);  6  setContentView(R.layout.activity_main);  7  8 ImageLoader imageLoader = ImageLoader.getInstance();  9  10 GridView gridView = (GridView) this.findViewById(R.id.grdvImageWall);  11 gridView.setAdapter(new PhotoWallAdapter(Constants.IMAGES));  12  }  13  14 static class ViewHolder {  15  ImageView imageView;  16  ProgressBar progressBar;  17  }  18  19 public class PhotoWallAdapter extends BaseAdapter {  20  String[] imageUrls;  21  ImageLoader imageLoad;  22  DisplayImageOptions options;  23  LinearLayout gridViewItem;  24  25 public PhotoWallAdapter(String[] imageUrls) {  26 assert imageUrls != null;  27 this.imageUrls = imageUrls;  28  29 options = new DisplayImageOptions.Builder()  30 .showImageOnLoading(R.drawable.ic_stub) // resource or  31 // drawable  32 .showImageForEmptyUri(R.drawable.ic_empty) // resource or  33 // drawable  34 .showImageOnFail(R.drawable.ic_error) // resource or  35 // drawable  36 .resetViewBeforeLoading(false) // default  37 .delayBeforeLoading(1000).cacheInMemory(false) // default  38 .cacheOnDisk(false) // default  39 .considerExifParams(false) // default  40 .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default  41 .bitmapConfig(Bitmap.Config.ARGB_8888) // default  42 .displayer(new SimpleBitmapDisplayer()) // default  43 .handler(new Handler()) // default  44  .build();  45 this.imageLoad = ImageLoader.getInstance();  46  47  }  48  49  @Override  50 public int getCount() {  51 return this.imageUrls.length;  52  }  53  54  @Override  55 public Object getItem(int position) {  56 if (position <= 0 || position >= this.imageUrls.length) {  57 throw new IllegalArgumentException(  58 "position<=0||position>=this.imageUrls.length");  59  }  60 return this.imageUrls[position];  61  }  62  63  @Override  64 public long getItemId(int position) {  65 return position;  66  }  67  68  @Override  69 public View getView(int position, View convertView, ViewGroup parent) {  70 // 判斷這個image是否已經在緩存當中,如果沒有就下載  71 final ViewHolder holder;  72 if (convertView == null) {  73 holder = new ViewHolder(); 74 gridViewItem = (LinearLayout) getLayoutInflater().inflate( 75 R.layout.image_wall_item, null); 76 holder.imageView = (ImageView) gridViewItem 77 .findViewById(R.id.item_image); 78 holder.progressBar = (ProgressBar) gridViewItem 79 .findViewById(R.id.item_process); 80 gridViewItem.setTag(holder); 81 convertView = gridViewItem; 82 } else { 83 holder = (ViewHolder) gridViewItem.getTag(); 84 } 85 this.imageLoad.displayImage(this.imageUrls[position], 86 holder.imageView, options, 87 new SimpleImageLoadingListener() { 88 89 @Override 90 public void onLoadingStarted(String imageUri, View view) { 91 holder.progressBar.setProgress(0); 92 holder.progressBar.setVisibility(View.VISIBLE); 93 } 94 95 @Override 96 public void onLoadingFailed(String imageUri, View view, 97 FailReason failReason) { 98 holder.progressBar.setVisibility(View.GONE); 99 } 100 101 @Override 102 public void onLoadingComplete(String imageUri, 103 View view, Bitmap loadedImage) { 104 holder.progressBar.setVisibility(View.GONE); 105 } 106 107 }, new ImageLoadingProgressListener() { 108 109 @Override 110 public void onProgressUpdate(String imageUri, 111 View view, int current, int total) { 112 holder.progressBar.setProgress(Math.round(100.0f 113 * current / total)); 114 } 115 }); // 通過URL判斷圖片是否已經下載 116 return convertView; 117 } 118 119 } 120 }
復制代碼

里面主要的對象都用? ? ? ??突出顯示了。

三者的關系

ImageLoaderConfiguration是針對圖片緩存的全局配置,主要有線程類、緩存大小、磁盤大小、圖片下載與解析、日志方面的配置。

ImageLoader是具體下載圖片,緩存圖片,顯示圖片的具體執行類,它有兩個具體的方法displayImage(...)、loadImage(...),但是其實最終他們的實現都是displayImage(...)。

DisplayImageOptions用于指導每一個Imageloader根據網絡圖片的狀態(空白、下載錯誤、正在下載)顯示對應的圖片,是否將緩存加載到磁盤上,下載完后對圖片進行怎么樣的處理。

從三者的協作關系上看,他們有點像廚房規定、廚師、客戶個人口味之間的關系。ImageLoaderConfiguration就像是廚房里面的規定,每一個廚師要怎么著裝,要怎么保持廚房的干凈,這是針對每一個廚師都適用的規定,而且不允許個性化改變。ImageLoader就像是具體做菜的廚師,負責具體菜譜的制作。DisplayImageOptions就像每個客戶的偏好,根據客戶是重口味還是清淡,每一個imageLoader根據DisplayImageOptions的要求具體執行。

?

ImageLoaderConfiguration

在上面的示例代碼中,我們使用ImageLoaderConfiguration的默認配置,下面給出 ImageLoaderConfiguration比較詳盡的配置,從下面的配置中,可以看出ImageLoaderConfiguration的配置主 要是全局性的配置,主要有線程類、緩存大小、磁盤大小、圖片下載與解析、日志方面的配置。

復制代碼
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800) // default = device screen dimensions.diskCacheExtraOptions(480, 800, null) .taskExecutor(...) .taskExecutorForCachedImages(...) .threadPoolSize(3) // default .threadPriority(Thread.NORM_PRIORITY - 1) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default  .denyCacheImageMultipleSizesInMemory() .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) // default .diskCache(new UnlimitedDiscCache(cacheDir)) // default .diskCacheSize(50 * 1024 * 1024) .diskCacheFileCount(100) .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default .imageDownloader(new BaseImageDownloader(context)) // default .imageDecoder(new BaseImageDecoder()) // default .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default  .writeDebugLogs() .build();
復制代碼

ImageLoaderConfiguration的 主要職責就是記錄相關的配置,它的內部其實就是一些字段的集合(如下面的源代碼)。它有一個builder的內部類,這個類中的字段跟 ImageLoaderConfiguration中的字段完全一致,它有一些默認值,通過修改builder可以配置 ImageLoaderConfiguration。

View Code

?

?Display Options

每一個ImageLoader.displayImage(...)都可以使用Display Options

復制代碼
DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub) // resource or drawable.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable .showImageOnFail(R.drawable.ic_error) // resource or drawable .resetViewBeforeLoading(false) // default .delayBeforeLoading(1000) .cacheInMemory(false) // default .cacheOnDisk(false) // default  .preProcessor(...) .postProcessor(...) .extraForDownloader(...) .considerExifParams(false) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default  .decodingOptions(...) .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build();
復制代碼

?Display Options的主要職責就是記錄相關的配置,它的內部其實就是一些字段的集合(如下面的源代碼)。它有一個builder的內部類,這個類中的字段跟DisplayOption中的字段完全一致,它有一些默認值,通過修改builder可以配置DisplayOptions。

View Code

?

?

參考鏈接

http://blog.csdn.net/wangjinyu501/article/details/8091623

https://github.com/nostra13/Android-Universal-Image-Loader

http://www.intexsoft.com/blog/item/74-universal-image-loader-part-3.html


作者:kissazi2
出處:http://www.cnblogs.com/kissazi2/
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

轉載于:https://www.cnblogs.com/1995hxt/p/4908145.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375444.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375444.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375444.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

營銷類文章 SEO

如何有效的推廣網站 適合沒錢的中小站長 唐世軍 a5總經理 博客 門戶網站廣告報價—以新浪為例 貴的一天30多萬 碧藍天營銷學院 網絡營銷&#xff0c;你真的了解嗎&#xff1f; SEO工具mozBar介紹、友情鏈接新參考mozRank 談談網絡推廣團隊每天工作流程、工作標準、考核 請問安卓…

顯示 grep 結果的指定行

用grep查找特定關鍵字&#xff0c;結果很多&#xff0c;但是有用的在中間的某幾行&#xff0c;即grep得到結果之后再次過濾出指定幾行。 首先查找指定行 grep -a "X" filename | grep -an "X"記下指定行&#xff0c;然后用awk打印指定行 grep -a "…

Java小知識

內部類分為: 成員內部類、靜態嵌套類、方法內部類、匿名內部類。(1)、內部類仍然是一個獨立的類&#xff0c;在編譯之后內部類會被編譯成獨立的.class文件&#xff0c;但是前面冠以外部類的類名和$符號。(2)、內部類不能用普通的方式訪問。成員變量成員變量靜態成員變量。List遍…

C++ 設置線程名字

使用 std::thread #include <thread> #include <pthread.h>std::thread t(funs, args); pthread_setname_np(t.native_handle(), threadName);通過 pthread_create 創建 #define _GNU_SOURCE #include <pthread.h>pthread_t tid; pthread_create(&ti…

java學習_File屬性處理

// TODO Auto-generated method stub File filenew File("newhello.txt"); //文件是否存在 System.out.println("文件是否存在&#xff1a;"file.exists()); //讀取文件名稱 System.out.println("讀取文件名&#xff1a;"file.getName()); //讀取…

pytest 基礎講解

文章目錄 一、前置說明二、操作步驟1. 安裝 pytest2. python 編寫測試用例3. 在 pycharm 中使用 pytest 運行測試用例1)執行單條用例:點擊用例前面的三角形執行,或在用例內部點擊右鍵2)執行多條用例:在測試用例的外部區域,點擊右鍵,批量執行所有用例4. 命令行中使用 pyt…

Myeclipse8.6中安裝SVN插件

方法一&#xff1a; 1.打開HELP->MyEclipse Configuration Center&#xff0c;切換到SoftWare標簽頁。   2.點擊Add Site 打開對話框&#xff0c;在對話框Name輸入Svn&#xff0c;URL中輸入&#xff1a;http://subclipse.tigris.org/update_1.6.x   3.在左邊欄中找到Per…

初識EL

一、EL函數庫介紹 由于在JSP頁面中顯示數據時&#xff0c;經常需要對顯示的字符串進行處理&#xff0c;SUN公司針對于一些常見處理定義了一套EL函數庫供開發者使用。  這些EL函數在JSTL開發包中進行描述&#xff0c;因此在JSP頁面中使用SUN公司的EL函數庫&#xff0c;需要導入…

ffmpeg 合并 flv 文件

// 轉ts char cmd[1024] {\0}; sprintf(cmd, "ffmpeg -i %s -loglevel quiet -c copy -bsf:v h264_mp4toannexb -f mpegts %s", lastFlvFile.c_str(), lastTsFile.c_str()); system(cmd);// 合并ts char cmd[1024] {\0}; sprintf(cmd, "ffmpeg -i concat:\&qu…

怎么樣的理解才是完全理解SQL(二)

如果我們從集合論&#xff08;關系代數&#xff09;的角度來看&#xff0c;一張數據庫的表就是一組數據元的關系&#xff0c;而每個 SQL 語句會改變一種或數種關系&#xff0c;從而產生出新的數據元的關系&#xff08;即產生新的表&#xff09;。我們學到了什么&#xff1f;思考…

Scala學習筆記-環境搭建以及簡單語法

關于環境的搭建&#xff0c;去官網下載JDK8和Scala的IDE就可以了&#xff0c;Scala的IDE是基于Eclipse的。 下面直接上代碼&#xff1a; 這是項目目錄&#xff1a; A是scala寫的&#xff1a; package first import scala.collection.mutable.ListBufferobject A {def main(args…

UVa 12169 (枚舉+擴展歐幾里得) Disgruntled Judge

題意&#xff1a; 給出四個數T, a, b, x1,按公式生成序列 xi (a*xi-1 b) % 10001 (2 ≤ i ≤ 2T) 給出T和奇數項xi&#xff0c;輸出偶數項xi 分析&#xff1a; 最簡單的辦法就是直接枚舉a、b&#xff0c;看看與輸入是否相符。 1 #include <cstdio>2 3 const int maxn …

使用Beautifulsoup爬取藥智網數據

使用Beautifulsoup模塊爬取藥智網數據 Tips&#xff1a;1.爬取多頁時&#xff0c;先用一頁的做測試&#xff0c;要不然ip容易被封 2.自己常用的處理數據的方法&#xff1a; regre.compile(正則表達式) datareg.sub(要替換的字符串,data) 代碼&#xff08;其實沒多少&#xff09…

冪集 返回某集合的所有子集

冪集。編寫一種方法&#xff0c;返回某集合的所有子集。集合中不包含重復的元素。 說明&#xff1a;解集不能包含重復的子集。 示例: 輸入&#xff1a; nums [1,2,3]輸出&#xff1a; [[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[] ]來源&#xff1a;力扣&#xff08;LeetCode…

iOS標準時間與時間戳相互轉換

本文轉載至 http://blog.sina.com.cn/s/blog_a843a8850101dzqd.html [cpp] view plaincopy 設置時間顯示格式: NSString* timeStr "2011-01-26 17:40:50"; NSDateFormatter *formatter [[[NSDateFormatter alloc] init] autorelease]; [formatter s…

JavaScript設計模式 Item 3 --封裝

在JavaScript 中&#xff0c;并沒有對抽象類和接口的支持。JavaScript 本身也是一門弱類型語言。在封裝類型方面&#xff0c;JavaScript 沒有能力&#xff0c;也沒有必要做得更多。對于JavaScript 的設計模式實現來說&#xff0c;不區分類型是一種失色&#xff0c;也可以說是一…

【WCF安全】WCF 自定義授權[用戶名+密碼+x509證書]

1.x509證書制作(略) 2.直接貼代碼 ----------------------------------------------------------------------服務端------------------------------------------------------------------------------------------- WCF服務 1 using System;2 using System.Collections.Generi…

openMVS-編譯

opencv4 編譯 會有問題&#xff0c;可以重新下載 opencv3 編譯并指定好路徑。 OpenCV_DIRyour opencv3 build install path cmake -DCMAKE_BUILD_TYPERelease -DVCG_ROOT"$main_path/vcglib" ..

ASP.NET Web API 數據提供系統相關類型及其關系

轉載于:https://www.cnblogs.com/frankyou/p/4932651.html

openMVG跑自定義數據出錯

使用自己拍攝的圖片跑 openMVG 的 turtor_demo.py 時&#xff0c;出現錯誤&#xff0c;沒有生成 sfm_data.bin DSC01988" model "DSC-RX100M6" doesnt exist in the database Please consider add your camera model and sensor width in the database.原因時數…