編輯:關於 Android 程式設計
在 Android 開發裡,Universal-Image-Loader 就像一位盡職的圖片加載守護者,為我們提供了強大而靈活的能力:

下面我們一起動手,看看如何配置並使用 Universal-Image-Loader 來加載網絡圖片。
GitHub 下載地址:https://github.com/nostra13/Android-Universal-Image-Loader
Maven 下載地址:https://central.maven.org/maven2/com/nostra13/universalimageloader/universal-image-loader/
(1)先關聯 Universal-Image-Loader 的庫文件,然後在 Application 的 onCreate 中完成 ImageLoader 的初始化:
public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); initImageLoader(this); } public static void initImageLoader(Context context) { // This configuration tuning is custom. You can tune every option, you may tune some of them, // or you can create default configuration by ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context); config.threadPriority(Thread.NORM_PRIORITY - 2); config.denyCacheImageMultipleSizesInMemory(); // 不會在內存中緩存多個大小的圖片 config.diskCacheFileNameGenerator(new Md5FileNameGenerator()); // 為了保證圖片名稱唯一 config.diskCacheSize(50 * 1024 * 1024); // 50 MiB // 內存緩存大小默認是:app可用內存的1/8 config.tasksProcessingOrder(QueueProcessingType.LIFO); config.writeDebugLogs(); // Remove for release app // Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config.build()); //ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this)); } }
(2)在 AndroidManifest 中配置 Application。
(3)在 MyImageAdapter 中加載圖片,並初始化 DisplayImageOptions。為了方便複用,我們把它抽取成一個獨立類:
/** * Universal-Image-Loader-master options參數 */ public interface ImageLoaderOptions { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_default)// 加載圖片過程中顯示哪張圖片 .showImageForEmptyUri(R.drawable.ic_default)// url為空的話顯示哪張圖片 .showImageOnFail(R.drawable.ic_default)// 加載圖片失敗顯示哪張圖片 .cacheInMemory(true)// 在內存中緩存該圖片 .cacheOnDisk(true)// 在硬盤中緩存該圖片 .imageScaleType(ImageScaleType.EXACTLY)// 將會對圖片進一步縮放,縮放的程度參考ImageVIew的寬高 .bitmapConfig(Bitmap.Config.RGB_565)// 該種渲染模式也是比較節省內存的 .considerExifParams(true)// 會識別圖片的方向信息 // .displayer(new FadeInBitmapDisplayer(800)).build();//漸漸顯示的動畫效果 .displayer(new RoundedBitmapDisplayer(28)).build();// 圓角的效果 // 顯示大圖的options DisplayImageOptions pager_options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_default)// 加載圖片過程中顯示哪張圖片 .showImageForEmptyUri(R.drawable.ic_default)// url為空的話顯示哪張圖片 .showImageOnFail(R.drawable.ic_default)// 加載圖片失敗顯示哪張圖片 .cacheInMemory(false)// 不在內存中緩存該圖片 .cacheOnDisk(true)// 在硬盤中緩存該圖片 .imageScaleType(ImageScaleType.EXACTLY)// 將會對圖片進一步縮放,縮放的程度參考ImageVIew的寬高 .bitmapConfig(Bitmap.Config.RGB_565)// 該種渲染模式也是比較節省內存的 .considerExifParams(true)// 會識別圖片的方向信息 .displayer(new FadeInBitmapDisplayer(500)).build();// 漸漸顯示的動畫效果 // .displayer(new RoundedBitmapDisplayer(28)).build();// 圓角的效果 }
(4)在 getView 方法中調用加載:
/** * displayImage(uri, imageView, options, listener, progressListener); * 參數解析: * imageUrl : 圖片的URL地址 * imageView : 顯示圖片的ImageView控件 * options : DisplayImageOptions配置信息 * listener : 圖片下載情況的監聽 * progressListener : 圖片下載進度的監聽 */ //加載圖片 ImageLoader.getInstance().displayImage(Url.IMAGE, holder.iv_icon, ImageLoaderOptions.options);
→ 拓展:漸顯動畫與圓角效果默認無法同時設置。解決辦法是——在步驟(3)中配置圓角效果,然後捨棄(4)的直接加載方式,改用下面的寫法,如此魚與熊掌便可兼得:
//加載圖片 ImageLoader.getInstance().displayImage(Url.IMAGE,holder.iv_icon, options ,new SimpleImageLoadingListener(){ @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { super.onLoadingComplete(imageUri, view, loadedImage); FadeInBitmapDisplayer.animate(view, 800);//漸漸顯示的效果 } });
(1)橫屏大圖(width > height)在低配手機或部分機型上無法顯示。
解決辦法:在 ImageLoaderConfiguration 中配置相應參數,最好在 Application 中統一設置(如上面的 MyApplication.java)。注意該配置只能生效一次,若多次配置,系統只認第一次的參數。
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( getApplicationContext()) .memoryCacheExtraOptions(480, 800) // 保存每個緩存圖片的最大長和寬 .threadPoolSize(3) // 線程池的大小 這個其實默認就是3 .threadPriority(Thread. NORM_PRIORITY - 2) //設置線程優先級 .denyCacheImageMultipleSizesInMemory() // 當同一個Uri獲取不同大小的圖片,緩存到內存時,只緩存一個。默認會緩存多個不同的大小的相同圖片 .memoryCache( new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) // 設置緩存的最大字節 .tasksProcessingOrder(QueueProcessingType. LIFO) //設置圖片下載和顯示的工作隊列排序 .defaultDisplayImageOptions(DisplayImageOptions. createSimple()) .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000))// connectTimeout : 超時時間 .writeDebugLogs().build(); // 開始構建 ImageLoader. getInstance().init(config); // 全局初始化此配置
按上述配置即可解決橫屏大圖在部分手機上不顯示的問題。強烈建議把配置放在 Application 中統一進行,代碼中臨時配置的缺點是「用一次配一次」,難以維護。
(2)低配手機在加載大量圖片時頻繁出現 OOM。
當項目裡圖片數量多、體積大時,後台收集的 crash 信息中常常可見大量 OOM。可嘗試以下手段:
以上並非必須全部照搬,往往只需調整其中幾項就能解決問題,關鍵在於靈活運用。
(1)只有當你需要讓圖片尺寸大於當前設備屏幕時,才有必要配置 maxImageWidthForMemoryCache() 和 maxImageHeightForMemoryCache()(例如縮放圖片的場景)。其他情況下無需設置,因為默認配置會依據屏幕尺寸,以最省內存的方式處理 Bitmap。
(2)合理設置線程池大小。較大的線程池能讓多條線程並行工作,但也會明顯拖累 UI 線程的流暢度。可以通過降低線程優先級來緩解:ImageLoader 工作時降低其優先級,UI 線程就會順暢許多。尤其在 ListView 場景下 UI 容易卡頓,建議設置 threadPoolSize() 與 threadPriority() 兩個參數來優化應用。
(3)memoryCache() 與 memoryCacheSize() 會互相覆蓋,在 ImageLoaderConfiguration 中選其一即可。
(4)diskCacheSize()、diskCache() 與 diskCacheFileCount() 三者也會互相覆蓋,只需使用其中一個。
特別提醒:不要使用 discCacheSize()、discCache() 與 discCacheFileCount(),這幾個方法已經被棄用了。
Fresco 詳解:SimpleDraweeView 加載圖片基礎用法
Fresco 簡單使用——SimpleDraweeView 學前須知: 在日常加載圖片時(無論是網路下載還是讀取本地),我們經常會遇到這樣的需求:圖片加載過程中要顯示「加
用 Fragment 做選項卡:一次完整的動態切換實踐
本文把之前講過的「動態創建 Fragment」拿來實踐一次,做一個四頁新聞切換的選項卡。 項目布局 整體結構很簡單:上面一條橫向 LinearLayout 放四個 Tex
Android 教學:透過 JNI 靜態(static)方式載入 OpenCV
透過 JNI 靜態(static)方式載入 OpenCV原文出處:https://blog.csdn.net/caroline_wendy整體步驟如下: 1. 準備 Op
dumpsys 使用指南:用一行命令看清 Android 系統服務的狀態
一、dumpsys 是什麼 dumpsys 執行在裝置上,能輸出各項系統服務的當前狀態。官方描述是「The dumpsys tool runs on the device