Android開源架構Universal-Image-Loader

來源:互聯網
上載者:User

標籤:android 開源架構 univers

先看效果
架構特點
  1. 多線程下載圖片,圖片可以來源於網路,檔案系統,專案檔夾assets中以及drawable中等
  2. 支援隨意的配置ImageLoader,例如線程池,圖片下載器,記憶體緩衝策略,硬碟緩衝策略,圖片顯示選項以及其他的一些配置
  3. 支援圖片的記憶體緩衝,檔案系統快取或者SD卡緩衝
  4. 支援圖片下載過程的監聽
  5. 根據控制項(ImageView)的大小對Bitmap進行裁剪,減少Bitmap佔用過多的記憶體
  6. 較好的控製圖片的載入過程,例如暫停圖片載入,重新開始載入圖片,一般使用在ListView,GridView中,滑動過程中暫停載入圖片,停止滑動的時候去載入圖片
  7. 提供在較慢的網路下對圖片進行載入
1.自訂XXXAppllication類,初始化ImageLoader
public class XXXApplication extends Application {@Overridepublic void onCreate() {super.onCreate();        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())        .denyCacheImageMultipleSizesInMemory()        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))        .memoryCacheSize(2 * 1024 * 1024)        .discCacheSize(50 * 1024 * 1024)        .denyCacheImageMultipleSizesInMemory()              .discCacheFileNameGenerator(new Md5FileNameGenerator())              .tasksProcessingOrder(QueueProcessingType.LIFO)          .discCacheFileCount(100)        .writeDebugLogs()            .build();        ImageLoader.getInstance().init(config);}}

2.在AndroidManifest.xml檔案的application標籤裡加入
android:name=".XXXApplication"

3.使用imageloader
public class ImageManager{ public static void Load(String imgUrl,ImageView imageView){ImageLoader.getInstance().displayImage(imgUrl, imageView); } public static void Load(String imgUrl,ImageView imageView,DisplayImageOptions o){ ImageLoader.getInstance().displayImage(imgUrl, imageView,o); }}
4.部分參數介紹(DisplayImageOptions)
     //設定圖片在下載期間顯示的圖片     showStubImage(R.drawable.ic_launcher)      //設定圖片Uri為空白或是錯誤的時候顯示的圖片     showImageForEmptyUri(R.drawable.ic_empty)      //設定圖片載入/解碼過程中錯誤時候顯示的圖片     showImageOnFail(R.drawable.ic_error)      //設定圖片在下載前是否重設,複位     resetViewBeforeLoading()      //設定下載的圖片是否緩衝在記憶體中     cacheInMemory()      //設定下載的圖片是否緩衝在SD卡中     cacheOnDisc()      //設定圖片的解碼類型     bitmapConfig(Bitmap.Config.RGB_565)      //設定圖片的解碼配置     decodingOptions(android.graphics.BitmapFactory.Options decodingOptions)      //設定圖片下載前的延遲     delayBeforeLoading(int delayInMillis)      //設定額外的內容給ImageDownloader     extraForDownloader(Object extra)      //設定圖片加入緩衝前,對bitmap進行設定     preProcessor(BitmapProcessor preProcessor)      //設定顯示前的圖片,顯示後這個圖片一直保留在緩衝中     postProcessor(BitmapProcessor postProcessor)      //設定圖片以如何的編碼方式顯示     imageScaleType(ImageScaleType imageScaleType)/** * 設定圖片的顯示方式 * @param displayer */displayer(BitmapDisplayer displayer)displayer:          RoundedBitmapDisplayer(int roundPixels)設定圓角圖片          FakeBitmapDisplayer()這個類什麼都沒做          FadeInBitmapDisplayer(int durationMillis)設定圖片漸顯的時間     SimpleBitmapDisplayer()正常顯示一張圖片  /** * 圖片的縮放方式 * @param imageScaleType */    imageScaleType(ImageScaleType imageScaleType)    imageScaleType:                   EXACTLY :映像將完全按比例縮小的目標大小                   EXACTLY_STRETCHED:圖片會縮放到目標大小完全                   IN_SAMPLE_INT:映像將被二次採樣的整數倍                   IN_SAMPLE_POWER_OF_2:圖片將降低2倍,直到下一減少步驟,使映像更小的目標大小                   NONE:圖片不會調整/***DisplayImageOptions簡單樣本*/// 建立預設的DisplayImageOptionsDisplayImageOptions option_0=DisplayImageOptions.createSimple();// 使用DisplayImageOptions.Builder()建立DisplayImageOptionsDisplayImageOptions options = new DisplayImageOptions.Builder()  .imageScaleType(ImageScaleType.EXACTLY_STRETCHED).showStubImage(R.drawable.loading)  .showImageForEmptyUri(R.drawable.load_fail)  .showImageOnFail(R.drawable.load_fail)  .cacheInMemory(true)  .cacheOnDisc(true)    .build(); 

5.最後別忘了許可權
    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
源碼下載

Android開源架構Universal-Image-Loader

聯繫我們

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