天天看點

初識Facebook的強大Android圖檔加載的架構:Fresco

fresco是facebook推出的一款強大的圖檔加載的架構,這個架構出來一段時間了,前一段時間使用過了imageloader的架構,生命在于折騰,今天就來折騰一下fresco這個強大的架構。

初識Facebook的強大Android圖檔加載的架構:Fresco

1.配置環境

由于我用的是android studio是以這裡就隻是說一下android studio下如何配置,在強大的gradle,隻需要一句話搞定,gradle會幫你下載下傳這個fresco架構,gradle真好,可以自動維護你項目中的架構

compile 'com.facebook.fresco:fresco:0.5.0+'

2.開始使用fresco

因為我這裡加載的是一張網絡圖檔,是以要獲得網絡權限

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

初始化fresco,如果項目中多處用到fresco,就直接在application中初始化,如果我隻是寫着玩玩,直接放在activity中的setcontentview()的前面就ok了

fresco.initialize(context);

用人家的架構就要按照人家的要求來是不,是以控件名字,命名空間都要聽人家的

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:fresco="http://schemas.android.com/apk/res-auto" 

    android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <com.facebook.drawee.view.simpledraweeview 

        android:id="@+id/image_view" 

        android:layout_width="300dp" 

        android:layout_height="300dp" 

        fresco:placeholderimage="@mipmap/ic_launcher"/> 

</linearlayout> 

當然你也可以不寫它的命名空間,用裡面的屬性的時候加上去,不用就别給自己找麻煩了,直接幹掉給控件uri就ok了

uri uri = uri.parse("http://pic1.nipic.com/2008-09-08/200898163242920_2.jpg");

        imageview.setimageuri(uri);

ok,剩下的圖檔下載下傳,緩存,圖檔移除就交給fresco了,是不是很強大,但這裡有一個問題,你的控件的大小必須确定,不能想以前直接使用wrap_content,當然你也可以設定寬高中的一個值,但是要設定寬高比

imageview.setaspectratio(1.0f);

當然上面說到它強大了,強大就不止隻是加載網絡圖檔吧,它本身也支援本地,content provider,asset,res的圖檔

本地:file:// content provider:content://  asset:  asset://  res: res://,就是讓你拼一個uri

當然上面提到的simpledraweeview隻是drawee其中的控件,沒有什麼很特别的需求使用它就夠了,下面貼一下它裡面的一些屬性,友善食用的時候查找

<com.facebook.drawee.view.simpledraweeview 

    android:id="@+id/image_view" 

    android:layout_width="300dp" 

    android:layout_height="300dp" 

    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" 

  /> 

簡單介紹一下上面的屬性:

placeholderimage就是所謂的展位圖啦,在圖檔沒有加載出來之前你看到的就是它

failureiamge看到名字就知道是什麼了,圖檔加載失敗時顯示的圖檔就是它了

retryimage圖檔加載失敗時顯示,提示使用者點選重新加載,重複加載4次還是沒有加載出來的時候才會顯示failureimage的圖檔

progressbarimage進度條圖檔

backgroundimage背景圖檔,這裡的背景圖檔首先被繪制

overlayimage設定疊加圖,在xml中隻能設定一張疊加圖檔,如果需要多張圖檔的話,需要在java代碼中設定哦

pressedstateoverlayimage設定點選狀态下的疊加圖,此疊加圖不能縮放

imagescaletype這個就是各種各樣的圖檔縮放樣式了,center,centercrop,fousecrop,centerinside,fitcenter,fitstart,fitend,fitxy

剩下的就是對圓角的處理了…

來源:51cto

繼續閱讀