天天看點

facebook強大的圖檔加載工具Fresco,比imageload加載速度快

關于 Fresco

Fresco 是一個強大的圖檔加載元件。

Fresco 中設計有一個叫做 image pipeline 的子產品。它負責從網絡,從本地檔案系統,本地資源加載圖檔。為了最大限度節省空間和CPU時間,它含有3級緩存設計(2級記憶體,1級檔案)。

Fresco 中設計有一個叫做 Drawees 子產品,友善地顯示loading圖,當圖檔不再顯示在螢幕上時,及時地釋放記憶體和空間占用。

Fresco 支援 Android2.3(API level 9) 及其以上系統。

特性記憶體管理

一個沒有未壓縮的圖檔,即Android中的Bitmap,占用大量的記憶體。大的記憶體占用勢必引發更加頻繁的GC。在5.0以下,GC将會顯著地引發界面卡頓。

在5.0以下系統,Fresco将圖檔放到一個特别的記憶體區域。當然,在圖檔不顯示的時候,占用的記憶體會自動被釋放。這會使得APP更加流暢,減少因圖檔記憶體占用而引發的OOM。

Fresco 在低端機器上表現一樣出色,你再也不用因圖檔記憶體占用而思前想後。

經過一天的努力,終于弄出了eclipse版本。隻需要引用lib就可以實作你想要的圖檔加載

初始化配置的兩種方式:

                 * 在 Application 初始化時,在應用調用 setContentView() 之前,進行初始化:

                 * Fresco.initialize(context);

public static void initConfig(Context context) {
                        ImagePipelineConfig initImagePipelineConfig = initImagePipelineConfig(context);
                        Fresco.initialize(context, initImagePipelineConfig);
                }
                        
                public static ImagePipelineConfig initImagePipelineConfig(Context context) {
                        if (sImagePipelineConfig == null) {
                                sImagePipelineConfig = configureCaches(context);
                        }
                        return sImagePipelineConfig;
                }
           
使用起來也是超級友善的,根據自己的需求随意配置XML檔案屬性
<com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/my_image_view"
            android:layout_width="20dp"   // 不支援wrap_content 如果要設定寬高比, 需要在Java代碼中指定setAspectRatio(1.33f);
            android:layout_height="20dp"    // 不支援wrap_content 
            fresco:fadeDuration="300"
            
            fresco:actualImageScaleType="focusCrop"// 設定圖檔縮放. 通常使用focusCrop,該屬性值會通過算法把人頭像放在中間
            fresco:placeholderImage="@color/wait_color"// 下載下傳成功之前顯示的圖檔
         
            fresco:placeholderImageScaleType="fitCenter"
            fresco:failureImage="@drawable/error"// 加載失敗的時候顯示的圖檔
        
            fresco:failureImageScaleType="centerInside"
            fresco:retryImage="@drawable/retrying"// 加載失敗,提示使用者點選重新加載的圖檔(會覆寫failureImage的圖檔)
        
            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"// 圓角角度,180的時候會變成圓形圖檔
            
            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"
                  />
           
加載圖檔非常友善的
/**
         * 加載圖檔(預設加載中占位圖)
         * @param draweeView 控件
         * @param uriString 位址
         */
        public static void loadImage(SimpleDraweeView draweeView , String uriString) {
                Uri uri = Uri.parse(uriString);
                GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
                hierarchy.setPlaceholderImage(R.drawable.pic10);//修改占位圖為資源
                hierarchy.setActualImageScaleType(ScaleType.CENTER_INSIDE);//修改縮放類型
                draweeView.setHierarchy(hierarchy);
                draweeView.setImageURI(uri);
        }
        
        /**
         * 加載圖檔(無占位圖)
         * @param draweeView 控件
         * @param uriString 位址
         */
        public static void loadImg(SimpleDraweeView draweeView , String uriString ) {
                Uri uri = Uri.parse(uriString);
                draweeView.setImageURI(uri);
        }
           

很多屬性都是 可以自定義的,這樣子就給我們的工作帶來了很大的友善

下載下傳之後有不明白的可以跟帖或是郵件  [email protected]

點選打開連結

繼續閱讀