Fresco是facebook推出的一款強大的圖片加載的框架,這個框架出來一段時間了,前一段時間使用過了ImageLoader的框架,生命在於折騰,今天就來折騰一下Fresco這個強大的框架。 java
因爲我用的是android studio因此這裏就只是說一下android studio下如何配置,在強大的gradle,只須要一句話搞定,gradle會幫你下載這個Fresco框架,gradle真好,能夠自動維護你項目中的框架 android
compile 'com.facebook.fresco:fresco:0.5.0+' 緩存
2.開始使用Fresco由於我這裏加載的是一張網絡圖片,因此要得到網絡權限 網絡
<uses-permission android:name="android.permission.INTERNET"/> app
初始化Fresco,若是項目中多處用到Fresco,就直接在application中初始化,若是我只是寫着玩玩,直接放在activity中的setContentView()的前面就ok了 框架
Fresco.initialize(context); ide
用人家的框架就要按照人家的要求來是不,因此控件名字,命名空間都要聽人家的。 gradle
01
02
03
04
05
06
07
08
09
10
|
<LinearLayoutxmlns: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了 spa
Uri uri = Uri.parse("http://pic1.nipic.com/2008-09-08/200898163242920_2.jpg"); orm
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其中的控件,沒有什麼很特別的需求使用它就夠了,下面貼一下它裏面的一些屬性,方便用的時候查找
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<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
剩下的就是對圓角的處理了…