使用ImageLoader實現圖片非同步載入

來源:互聯網
上載者:User

標籤:android   http   使用   os   檔案   io   

註:下面使用的是包:1.8.4,其他版本包的,DisplayImageOptions defaultOptions和 ImageLoaderConfiguration config2配置不一樣,請看官網 我們經常會從網上載入大量的圖片,處理不好,經常會出現記憶體溢出,導致app崩潰,還有下載速度慢登問題。ImageLoader基本避免了這些問題,下載速度快,基本不會出現記憶體流失,還有很好的緩衝管理機制,自己感覺很好用的,下面,簡單介紹其使用方法: 官方網址:https://github.com/nostra13/Android-Universal-Image-Loader 1.到官方網站下載最新的包,地址上面這個,進入官網,點擊zip,就可以下載需要的包了,裡面有使用例子。 2.將下載包解壓,把downloads裡面的包universal-image-loader-1.8.4.jar加入到你項目的libs裡面,注意引入項目,點擊你的項目,右鍵——選擇build path——configure build path——add jars,選擇你項目下面libs裡面的包。 點擊ok。 3.開始使用了,使用之前,需要進行配置: 官方網站這麼寫的: 1. Include library Manual: Download JAR Put the JAR in the libs subfolder of your Android project or Maven dependency: com.nostra13.universalimageloader universal-image-loader 1.8.4 這種方法,沒用過 2. Android Manifest 設定檔裡面加入訪問網路的許可權,註冊全域使用的類 MyApplication(下面建立的),不要忘了註冊相應的activity ,應用啟動後,會首先調用這個類,然後完成我們希望的初始化操作,類在下面 3. Application class public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // Create global configuration and initialize ImageLoader with this configuration ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) ... .build(); ImageLoader.getInstance().init(config); } } // 建立一個名字叫MyApplicationf的類(名字可以自己隨便取的),繼承Application,重寫onreat方法,在裡面初始化Imagerloader。 完整的初始化代碼是這樣的,例子裡面拷出來的。 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .enableLogging() // Not necessary in common .build(); ImageLoader.getInstance().init(config); 這個配置沒有把圖片緩衝起來(但是我們可以在後面,載入圖片的時候,設定緩衝,看後面使用介紹)。 如果你想把使用的圖片都緩衝起來,可以這樣配置:(後面載入圖片,就不需要設定緩衝了) DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() .cacheInMemory() //1.8.6包使用時候,括弧裡面傳入參數true .cacheOnDisc() //1.8.6包使用時候,括弧裡面傳入參數true .build(); ImageLoaderConfiguration config2 = new ImageLoaderConfiguration.Builder(context) //contex上下文,在activity裡面填入 this即可 .defaultDisplayImageOptions(defaultOptions) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .enableLogging() // Not necessary in common //1.8.6包,把這句刪除 .build(); 怎加錄色部分代碼就是了。 也就是說第3步驟完整的應該是這樣:(注意快速鍵ctrl+shift+o 引入相應包) //沒有把圖片緩衝的sdcard的 public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .enableLogging() // Not necessary in common //1.8.6包,把這句刪除 .build(); ImageLoader.getInstance().init(config); }} //有緩存的配置: public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() .cacheInMemory() //1.8.6包使用時候,括弧裡面傳入參數true .cacheOnDisc() //同上 .build(); ImageLoaderConfiguration config2 = new ImageLoaderConfiguration.Builder(context) .defaultDisplayImageOptions(defaultOptions) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .enableLogging() // 1.8.6,把這句刪除 .build(); ImageLoader.getInstance().init(config); } } //配置的參數介紹請看官方文檔 好了,配置完成後,我們就可以在任何想用它的地方使用了。具體使用如下(有多種): 就列常用的幾種吧: 1. ImageLoader.getInstance().displayImage(url, ImageView); 在你需要加載圖片的地方調用上面這句話就ok了,url為圖片的url地址,Imageview為你要顯示的imageview。 如果你在上面配置的是有緩存的,那麼使用這個加載圖片,圖片也會被緩衝起來的 2. DisplayImageOptions options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_stub) 載入開始預設的圖片 .showImageForEmptyUri(R.drawable.ic_empty) //url爲空會顯示該圖片,自己放在drawable裡面的 .showImageOnFail(R.drawable.ic_error) //載入圖片出現問題,會顯示該圖片 .cacheInMemory() //緩衝用 .cacheOnDisc() //緩衝用 .displayer(new RoundedBitmapDisplayer(5)) //圖片圓角顯示,值為整數 .build(); ImageLoader.getInstance().displayImage(url, imageView,options); 多了一個options,上面已經有註解。當我們在初始化imagerloader時候,沒有加緩衝的話,我們可以在這裡來配置, 還有可以實現圖片圓角額 3. ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() { public void onLoadingComplete(String imageUri, android.view.View view, android.graphics.Bitmap loadedImage) { imageView.setImageBitmap(loadedImage); //imageView,你要顯示的imageview控制項對象,布局檔案裡面//配置的 }; } ); //還可以給它弄個監聽事件SimpleImageLoadingListener,url還是圖片url地址,SimpleImageLoadingListener裡面有好幾個方法,上面這個是圖片下載完成後,我們需要做什麼操作。這裡是,把擷取的bitmap,顯示在imageview上面。 也就是說,可以用這個方法擷取一個bitmap對象 SimpleImageLoadingListener裡面還有幾個重要方法,根據需要在裡面進行相應處理 public void onLoadingFailed(String imageUri, android.view.View view, com.nostra13.universalimageloader.core.assist.FailReason failReason) { Toast.makeText(ShowOneImage.this,"載入失敗", Toast.LENGTH_LONG).show(); }; @Override public void onLoadingStarted(String imageUri, View view) { } @Override public void onLoadingCancelled(String imageUri, View view) { }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.