Universal-Image-Loader 使用步驟

来源:http://www.cnblogs.com/Mihai/archive/2016/08/16/5775946.html
-Advertisement-
Play Games

開源框架利與弊 開源框架給開發者提供了便利,避免了重覆造輪子,但是卻隱藏了一些開發上的細節,如果不關註其內部實現,那麼將不利於開發人員掌握核心技術,當然也談不上更好的使用它,計劃分析項目的集成使用和低層實現。 基本四部曲 1. ImageLoaderConfiguration(有預設) 通過Imag ...


開源框架利與弊

開源框架給開發者提供了便利,避免了重覆造輪子,但是卻隱藏了一些開發上的細節,如果不關註其內部實現,那麼將不利於開發人員掌握核心技術,當然也談不上更好的使用它,計劃分析項目的集成使用和低層實現。

基本四部曲

  1. imageLoaderConfiguration
  2. ->imageLoader.init(imageLoaderConfiguration)
  3. displayImageOptions
  4. ->imageLoader.displayImage(...,displayImageOptions,...)

1. ImageLoaderConfiguration(有預設)

通過ImageLoaderConfiguration進行配置
兩種方式:

  1. 預設
ImageLoaderConfiguration configuration = ImageLoaderConfiguration  
                .createDefault(this);

  

  1. 詳細的
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
        .diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, 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();

  

2.給ImageLoader配置上一步信息

ImageLoader.getInstance().init(configuration);

ImageLoader使用單例模式:源碼

public static  ImageLoader getInstance(){
    /*為了線程安全加了鎖
    *一但ImageLoader對象有了,就直接跳過同步
    *這是單例設計的思想(可以參考HeadFirst設計模式)
    */
    if(instance == null){
        synchorinized(ImageLoader.class){
            if(instance == null){
                instance = new ImageLoader();
            }
        }
    }
    return instance;
}

  

public synchronized void init(){
    if(configuration == null){
        throw new IlleagalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);
    if(this.configuration == null){
        this.configuration = configuration;
        emptyListener = new SimpleIageLoadingListener();
        fakeBitmapDisplayer = new FakeBitmapDisplayer();
    }
  }
}

  

3.下載圖片偏好

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

  

4.正式載入圖片

兩種方式:loadImage();displayImage()-一般用這個;

final ImageView mImageView = (ImageView) findViewById(R.id.image);
		String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg";
		ImageSize mImageSize = new ImageSize(100, 100);
		
		//顯示圖片的配置
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.build();
		
		ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options, new SimpleImageLoadingListener(){

			@Override
			public void onLoadingComplete(String imageUri, View view,
					Bitmap loadedImage) {
				super.onLoadingComplete(imageUri, view, loadedImage);
				mImageView.setImageBitmap(loadedImage);
			}
			
		});

  

GirdView,ListView中使用

使用GridView,ListView來顯示大量的圖片,而當我們快速滑動GridView,ListView,我們希望能停止圖片的載入,而在GridView,ListView停止滑動的時候載入當前界面的圖片,這個框架當然也提供這個功能,使用起來也很簡單,它提供了PauseOnScrollListener這個類來控制ListView,GridView滑動過程中停止去載入圖片

listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));

參考

  1. https://github.com/nostra13/Android-Universal-Image-Loader
  2. http://blog.csdn.net/xiaanming/article/details/26810303

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • CSS常用樣式之變形樣式(2D平移、2D旋轉、2D縮放、斜切扭曲),過渡動畫(過渡屬性、過渡時間、過渡函數、過渡延遲時間) ...
  • 這是個css3動畫框架,現在很流行,在小動畫效果有很多,使用的方式 一、atention Seekers 1、bounce 2、flash 3、pulse 4、rubberBand 5、shake 6、swing 7、tada 8、wobble 9、jello 二、Bouncing Entrance ...
  • ListView是開發中最常用的控制項了,但是總是會寫重覆的代碼,浪費時間又沒有意義。 最近參考一些資料,發現一個萬能ListView適配器,代碼量少,節省時間,總結一下分享給大家。 首先有一個自定義的Adapter繼承於BaseAdapter,下麵是自定義的Adapter,精華在getView()方 ...
  • 接上節:[APP] Android 開發筆記 001 4. 預設項目結構說明: 這裡我使用Sublime Text 進行載入。 1) res目錄: -drawable-*dpi 目錄: 該目錄下放置這各種圖片資源.不同的dpi對應不同的屏幕尺寸 -layout 目錄: 該目錄下放置佈局文件 -val ...
  • 1. 安裝SDK Android SDK http://developer.android.com/sdk/index.html https://dl.google.com/android/android-sdk_r24.4.1-windows.zip (No installer) https:// ...
  • 1、把aar複製到項目中的 libs 裡面 2、在module 裡面的build.gradle 的根目錄添加 3、在module 裡面的build.gradle 的根目錄的 dependencies 標簽裡面添加 其中 SDK-release 是你的aar的名字 4、在做完了前三步以後,會看到在項目 ...
  • iOS開發 中的代理實現 關於今天為什麼要發這篇文字的原因:今天在和同事聊天的時候他跟我說項目中給他的block有時候不太能看的懂,讓我儘量用代理寫,好吧心累了,那就先從寫個代理demo,防止以後他看不懂,嘿嘿 iOS開發 中的代理實現 今天我舉例的東西呢我就不寫demo了,直接從項目中吧需要的片段 ...
  • 解決辦法Item xml 根節點添加 android:descendantFocusability="blocksDescendants" Button 設置 android:focusable="false" 這樣點擊Button 和ListView Item 可以分別響應自己的點擊事件 開發中很 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...