Fresco圖片加載庫的使用(SimpleDraweeView的簡單使用 )

一、簡介

Fresco 的出現,似乎將 Android 的圖片加載做到了極致

Fresco 中文說明:http://www.fresco-cn.org/

Fresco 項目GitHub地址:https://github.com/facebook/fresco

Fresco 的Drawees 負責圖片的呈現,Image Pipeline 負責圖片的獲取和管理。

Drawees功能: 

設置要加載的圖片
設置佔位圖
設置加載失敗時的佔位圖
點擊重新加載
顯示一個進度條
設置背景
疊加圖
按壓狀態下的疊加圖
縮放
圓角和圓圈
使用ControllerBuilder
漸進式JPEG圖
動畫支持(GIF 和 WebP 格式的動畫)
多圖請求及圖片複用
監聽下載事件
縮放和旋轉圖片
修改圖片
圖片請求(Image Requests)
自定義View

Image Pipeline功能:

配置Image Pipeline
緩存
直接使用Image Pipeline
數據源和數據訂閱者
可關閉的引用
Webp支持

二、使用Fresco

1.引入Fresco,編輯 build.gradle 文件:

compile 'com.facebook.fresco:fresco:1.3.0'

2.在 AndroidManifest.xml 中聲明瞭網絡請求的權限

<uses-permission android:name="android.permission.INTERNET" />

3.始化Fresco類

// 在加載圖片之前,你必須初始化Fresco類
Fresco.initialize(this);

4.在xml佈局文件中, 加入命名空間

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

5.加入SimpleDraweeView

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="130dp"
    android:layout_height="130dp"
    fresco:placeholderImage="@drawable/my_drawable"
  />

6.開始加載圖片

Uri uri = Uri.parse("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3103457797,1011040581&fm=26&gp=0.jpg");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

三、幾種常用的效果展示

相關參數說明:

<com.facebook.drawee.view.SimpleDraweeView
  android:id="@+id/my_image_view"
  android:layout_width="20dp"
  android:layout_height="20dp"
  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"-----圓形或圓角的邊框顏色
/>

四、示例代碼

示例代碼:http://download.csdn.net/detail/wang295689649/9863934



注:官網文檔非常詳細,建議查看文檔API,各功能介紹


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章