Android Universal-Image-Loader初級使用

來源:互聯網
上載者:User

Android Universal-Image-Loader初級使用

 

其主要特徵:

 

  1. 多線程下載圖片,圖片可以來源於網路,檔案系統,專案檔夾assets中以及drawable中等支援隨意的配置ImageLoader,例如線程池,圖片下載器,記憶體緩衝策略,硬碟緩衝策略,圖片顯示選項以及其他的一些配置支援圖片的記憶體緩衝,檔案系統快取或者SD卡緩衝支援圖片下載過程的監聽根據控制項(ImageView)的大小對Bitmap進行裁剪,減少Bitmap佔用過多的記憶體較好的控製圖片的載入過程,例如暫停圖片載入,重新開始載入圖片,一般使用在ListView,GridView中,滑動過程中暫停載入圖片,停止滑動的時候去載入圖片提供在較慢的網路下對圖片進行載入

     

     

    簡單使用:

    1.建立一個Android appication.名字叫IApplicaiton.

    2.把下載下來的項目裡的sample裡的libs下的jar拷貝到此項目中。

    3.建立一個類MyApplicaiton繼承application:

     

    package com.example.iapplication;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import android.app.Application;public class MyApplication extends Application { @Override      public void onCreate() {          super.onCreate();            //建立預設的ImageLoader配置參數          ImageLoaderConfiguration configuration = ImageLoaderConfiguration                  .createDefault(this);                    //Initialize ImageLoader with configuration.          ImageLoader.getInstance().init(configuration);      }  }

    4.配置mainfest.xml檔案:

     

     

                                                                                                   
    注意application節點和許可權的加入。

     

    接下來,載入圖片。

    5.定義一個layout布局:

     

    <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">    </framelayout>

    6.使用displayImage()載入圖片:

     

     

    //使用displayImage方法載入public void advanceLoad2(){final ImageView mImageView = (ImageView) findViewById(R.id.image);          String imageUrl = http://smartcost.com.cn/global/upload/img/20150206/14223432211081942.jpg;          //顯示圖片的配置          DisplayImageOptions options = new DisplayImageOptions.Builder()                  .showImageOnLoading(R.drawable.ic_stub)                  .showImageOnFail(R.drawable.ic_error)                  .cacheInMemory(true)                  .cacheOnDisk(true)                  .bitmapConfig(Bitmap.Config.RGB_565)                  .build();                    ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);  }

    搞定。如果有需要顯示載入圖片進度的話,只需要在displayImage()方法中傳入ImageLoadingProgressListener介面就行了:

     

     

    imageLoader.displayImage(imageUrl, mImageView, options, new SimpleImageLoadingListener(), new ImageLoadingProgressListener() {                            @Override              public void onProgressUpdate(String imageUri, View view, int current,                      int total) {                                }          });  

    載入其他資源的圖片:

     

     

     String imagePath = /mnt/sdcard/image.png;          String imageUrl = Scheme.FILE.wrap(imagePath);            //      String imageUrl = http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg;                    imageLoader.displayImage(imageUrl, mImageView, options);  

    //圖片來源於Content provider          String contentprividerUrl = content://media/external/audio/albumart/13;                    //圖片來源於assets          String assetsUrl = Scheme.ASSETS.wrap(image.png);                    //圖片來源於          String drawableUrl = Scheme.DRAWABLE.wrap(R.drawable.image);  
    當我們快速滑動GridView,ListView,我們希望能停止圖片的載入,而在GridView,ListView停止滑動的時候載入當前介面的圖片,這個架構當然也提供這個功能,使用起來也很簡單,它提供了PauseOnScrollListener這個類來控制ListView,GridView滑動過程中停止去載入圖片

     

     

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

    第一個參數就是我們的圖片載入對象ImageLoader, 第二個是控制是否在滑動過程中暫停載入圖片,如果需要暫停傳true就行了,第三個參數控制猛的滑動介面的時候圖片是否載入

     

     

    OutOfMemoryError

     

    雖然這個架構有很好的緩衝機制,有效避免了OOM的產生,一般的情況下產生OOM的機率比較小,但是並不能保證OutOfMemoryError永遠不發生,這個架構對於OutOfMemoryError做了簡單的catch,保證我們的程式遇到OOM而不被crash掉,但是如果我們使用該架構經常發生OOM,我們可以使用一下方式去改善。

     

    • 減少線程池中線程的個數,在ImageLoaderConfiguration中的(.threadPoolSize)中配置,推薦配置1-5在DisplayImageOptions選項中配置bitmapConfig為Bitmap.Config.RGB_565,因為預設是ARGB_8888, 使用RGB_565會比使用ARGB_8888少消耗2倍的記憶體在ImageLoaderConfiguration中配置圖片的記憶體緩衝為memoryCache(new WeakMemoryCache()) 或者不使用記憶體緩衝在DisplayImageOptions選項中設定.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY)

       

      我們在使用該架構的時候盡量的使用displayImage()方法去載入圖片,loadImage()是將圖片對象回調到ImageLoadingListener介面的onLoadingComplete()方法中,需要我們手動去設定到ImageView上面,displayImage()方法中,對ImageView對象使用的是Weak references,方便記憶體回收行程回收ImageView對象,如果我們要載入固定大小的圖片的時候,使用loadImage()方法需要傳遞一個ImageSize對象,而displayImage()方法會根據ImageView對象的測量值,或者android:layout_width and android:layout_height設定的值,或者android:maxWidth and/or android:maxHeight設定的值來裁剪圖片


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.