AndroidUniversalImageLoader圖片異步加載類庫的使用

  • 開發App過程當中,免不了要進行網絡請求操做進行數據交換,好比下載圖片,若是本身寫一個下載圖片的類進行操做的話,要考慮太多太多內容,必須線程池,內存溢出,圖片磁盤緩存操做,圖片內存緩存操做等等,至關麻煩。好在偉大的開源者們已經寫好了一個比較完美的開源類庫供你們使用Android-Universal-Image-Loader,這個類庫已經被許多知名的軟件所採用,當時我本身用這個開源類庫的時候,百度了一大推,有查看了官方文檔。如今把記錄寫下來供你們參考。android

    1、介紹:

    Android-Universal-Image-Loader的目的是提供一個功能強大的,靈活的,高度可定製的圖像加載,緩存和顯示工具。它提供了大量的配置選項,並很好地控制圖像加載和緩存。緩存

    類庫的特色網絡

    l 多線程的圖像加載(同步或異步)多線程

    l 靈活的圖像加載配置選項(線程執行,下載,解碼,內存和磁盤緩存,圖像顯示選項)app

    l 自定義每一個顯示圖像的配置框架

    l 在內存或磁盤上緩存(設備內存或SD卡)異步

    l 監聽加載進程(包括下載進度)ide

    效果圖工具

    2、用法

    先來看下引入該類包後,加載圖片的運行流程圖post

    從圖中可看出,加載圖片的時候一共分爲三種狀況:

    (post process Bitmap一步,實際就是對該圖片進行處理,好比加水印,加圓角等等,該框架沒有給出具體實現,默認爲null,若有須要本身能夠實現,因此分析的時候能夠忽略)

    1. 圖片在內存中緩存:直接顯示圖片。

    2. 圖片在磁盤中緩存:先解碼,再暫時緩存到內存中,最後顯示。

    3. 圖片沒有緩存:先下載,再是否緩存到磁盤和內存,最後顯示。

    (一) Include Library

    官網下載依賴jar包,導入工程中libs文件夾下並添加到工程路徑便可。

    (二) 加權限

     

    <manifest>
        <!-- Include following permission if you load images from Internet -->
        <uses-permission android:name="android.permission.INTERNET" />
        <!-- Include following permission if you want to cache images on SD card -->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        ...
    </manifest>
    (三)全局配置

     

    全局配置我通常寫個類繼承application,而後在Mainfest文件中聲明

     

    public class MyApplication extends Application {
    
    	@Override
    	public void onCreate() {
    		// TODO Auto-generated method stub
    		super.onCreate();
    		initImageLoader(getApplicationContext());
    	}
    	public static void initImageLoader(Context context){
    		File cacheDir = StorageUtils.getCacheDirectory(context);
    		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    		        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
    		        .diskCacheExtraOptions(480, 800, null)
    //		        .taskExecutor(...)
    //		        .taskExecutorForCachedImages(...)
    		        .threadPoolSize(3) // default
    		        .threadPriority(Thread.NORM_PRIORITY - 2) // 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();
    		// Initialize ImageLoader with configuration.初始化配置
    		ImageLoader.getInstance().init(config);
    	}
    	
    }
    <application
            android:name=".base.MyApplication"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    以上的配置大部分都有默認值,按照本身的須要進行配置,不必所有都寫上,如今的顯示配置也是同樣。

     

    (四)顯示配置

    顯示配置是具體到每一個顯示圖片任務的配置

     

    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();
    (五)配置完了使用

     

    用默認配置顯示:ImageLoader.getInstance().displayImage(imageUrl, imageView);

    用自定義配置顯示:ImageLoader.getInstance().displayImage(imageUrl, imageView,options);

    帶監聽事件的顯示:

    imageLoader.displayImage(imageUrl, imageView, options, new ImageLoadingListener() {  
        @Override  
        public void onLoadingStarted() {  
           //開始加載的時候執行  
        }  
        @Override  
        public void onLoadingFailed(FailReason failReason) {        
           //加載失敗的時候執行  
        }   
        @Override   
        public void onLoadingComplete(Bitmap loadedImage) {  
           //加載成功的時候執行  
        }   
        @Override   
        public void onLoadingCancelled() {  
           //加載取消的時候執行  
      
        }});
    帶監聽事件和進度條的顯示:
    imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            ...
        }
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            ...
        }
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            ...
        }
        @Override
        public void onLoadingCancelled(String imageUri, View view) {
            ...
        }
    }, new ImageLoadingProgressListener() {
        @Override
        public void onProgressUpdate(String imageUri, View view, int current, int total) {
            ...
        }
    });
    有的文章提出加載本地圖片也用ImageLoader.getInstance().displayImage,徹底沒有必要。

     

     

    3、注意事項

    1.上述提到的2個權限必須加入,不然會出錯
    2.ImageLoaderConfiguration必須配置而且全局化的初始化這個配置ImageLoader.getInstance().init(config); 不然也會出現錯誤提示
    3.ImageLoader是根據ImageView的height,width肯定圖片的寬高。
    4.若是常常出現OOM
    ①減小配置之中線程池的大小,(.threadPoolSize).推薦1-5;
    ②使用.bitmapConfig(Bitmap.config.RGB_565)代替ARGB_8888;
    ③使用.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者 try.imageScaleType(ImageScaleType.EXACTLY);
    ④避免使用RoundedBitmapDisplayer.他會建立新的ARGB_8888格式的Bitmap對象;
    ⑤使用.memoryCache(new WeakMemoryCache()),不要使用.cacheInMemory();

    對該類庫源碼的解析能夠參考這篇大神的文章http://blog.csdn.net/xiaanming/article/details/39057201

相關文章
相關標籤/搜索