在開發中我們經常會接觸到圖檔的加載,圖檔加載是一個比較頭疼的問題,你可以要考慮到圖檔的異步加載,圖檔的緩存處理、
圖檔的壓縮處理等等,是以今天總結對比一下自己用過的2個開源圖檔架構,一個是大名鼎鼎的facebook出的fresco,
另外一個是使用人最多的Universal Image Loader (以下簡稱UIL),當然還有幾款比較有名的圖檔架構,比如square的Picasso,鄙人沒用過就不做比較了。
官方 Official
Fresco
官方文檔
http://fresco-cn.org/
Github
https://github.com/facebook/fresco
Gradle
compile ‘com.facebook.fresco:fresco:0.6.0+’
UIL
Github
https://github.com/nostra13/Android-Universal-Image-Loader
Gradle
UIL沒有上傳在maven裡面 是以得導jar包,可以在github上下載下傳jar包
使用
Fresco
使用起來非常簡單
<!-- 在xml布局裡使用 這裡列舉了很多可選項,可以根據需要來使用-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp" 注意DraweeView不支援wrap_content
fresco:fadeDuration="300"
fresco:actualImageScaleType="focusCrop" 縮放形式
fresco:placeholderImage="@color/wait_color" 臨時安放的圖檔
fresco:placeholderImageScaleType="fitCenter"
fresco:failureImage="@drawable/error" 加載失敗的圖檔
fresco:failureImageScaleType="centerInside"
fresco:retryImage="@drawable/retrying" 重試的圖檔
fresco:retryImageScaleType="centerCrop"
fresco:progressBarImage="@drawable/progress_bar" 加載進度時候的圖檔
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
fresco:backgroundImage="@color/blue"
fresco:overlayImage="@drawable/watermark" 疊加的圖檔
fresco:pressedStateOverlayImage="@color/red" 按下去時候的圖檔
fresco:roundAsCircle="false" 圖檔是否是圓形
fresco:roundedCornerRadius="1dp" 圖檔圓角角度
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundWithOverlayColor="@color/corner_color"
fresco:roundingBorderWidth="2dp" 圓角邊框寬度
fresco:roundingBorderColor="@color/border_color" 圓角邊框顔色
/>
SimpleDraweeView draweeView = (SimpleDraweeView)findViewById(R.id.my_image_view);
draweeView.setImageURI(Uri.parse("http://www.baidu.com/1.jpg"));//搞定!
剩下的,Fresco會替你完成:
- 顯示占位圖直到加載完成;
- 下載下傳圖檔;
- 緩存圖檔;
- 圖檔不再顯示時,從記憶體中移除; etc
Fresco含有的特點是:
- focusCrop縮放屬性,可以支援人臉識别(讓你的臉居中顯示)
- 預設的漸進式加載效果(這個超級棒)
- 圓角/圓形圖
- 加載進度條
- 支援Gif圖檔
- 修改圖檔(比如下載下傳完加一個logo在上面,比如微網誌的圖檔)
UIL
導入jar包
用起來也非常簡單
ImageView imageView = (ImageView)findViewById(R.id.imageView);
ImageLoader.getInstance().displayImage("http://www.baidu.com/1.jpg", imageView);//搞定
//如果需要修改預設配置參數,可以設定option 具體參考文檔
對比
首先我們來看一張圖
這種圖對比了時下流行的4種架構的異同,從表層來說,Fresco 自己定義了自己的View來配合加載,UIL則允許使用者使用系統的View來加載
問題總結
- Fresco 可能會于百度地圖發生沖突
- Fresco 在IDE布局預覽的時候可能會有問題
- UIL 的在處理圓角邊框縮放可能需要自己寫View