1.前言
Fresco框架的設(shè)計(jì)主要采用的是MVC模式。DraweeView實(shí)現(xiàn)了View的功能,DraweeHierarchy實(shí)現(xiàn)了Model的功能,DraweeController實(shí)現(xiàn)Controller的功能。
2.用法
⑴依賴(lài)
implementation 'com.facebook.fresco:fresco:1.12.1'
⑵在進(jìn)行圖片加載之前,需要配置Fresco類(lèi)议经,F(xiàn)resco.initialize只需要調(diào)用一次斧账,所以我們?cè)贏pplication中進(jìn)行初始化
public class MyApplication extends Application {
@Override
public void onCreate(){
super.onCreate();
Fresco.initialize(this);
}
⑶在AndroidManifest.xml中配置MyApplication谴返,如果要從網(wǎng)絡(luò)下載圖片,還需要添加網(wǎng)絡(luò)訪問(wèn)權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
...
</application>
⑷在xml中配置SimpleDraweeView,注意SimpleDraweeView不能使用wrap_content
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv"
android:layout_width="match_parent"
app:placeholderImage="@mipmap/ic_launcher"
android:layout_height="match_parent" />
⑸在Activity中加載圖片
private String mUrl = "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg";
mSdv.setImageURI(mUrl);
⑹其他屬性
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="20dp"
android:layout_height="20dp"
fresco:fadeDuration="300" // 淡出時(shí)間咧织,毫秒嗓袱。
fresco:actualImageScaleType="focusCrop" // 等同于android:scaleType。
fresco:placeholderImage="@color/wait_color" // 加載中…時(shí)顯示的圖习绢。
fresco:placeholderImageScaleType="fitCenter" // 加載中…顯示圖的縮放模式渠抹。
fresco:failureImage="@drawable/error" // 加載失敗時(shí)顯示的圖。
fresco:failureImageScaleType="centerInside" // 加載失敗時(shí)顯示圖的縮放模式闪萄。
fresco:retryImage="@drawable/retrying" // 重試時(shí)顯示圖梧却。
fresco:retryImageScaleType="centerCrop" // 重試時(shí)顯示圖的縮放模式。
fresco:progressBarImage="@drawable/progress_bar" // 進(jìn)度條顯示圖败去。
fresco:progressBarImageScaleType="centerInside" // 進(jìn)度條時(shí)顯示圖的縮放模式放航。
fresco:progressBarAutoRotateInterval="1000" // 進(jìn)度條旋轉(zhuǎn)時(shí)間間隔。
fresco:backgroundImage="@color/blue" // 背景圖圆裕,不會(huì)被View遮擋广鳍。
fresco:roundAsCircle="false" // 是否是圓形圖片。
fresco:roundedCornerRadius="1dp" // 四角圓角度數(shù)吓妆,如果是圓形圖片赊时,這個(gè)屬性被忽略。
fresco:roundTopLeft="true" // 左上角是否圓角行拢。
fresco:roundTopRight="false" // 右上角是否圓角祖秒。
fresco:roundBottomLeft="false" // 左下角是否圓角。
fresco:roundBottomRight="true" // 左下角是否圓角剂陡。
fresco:roundingBorderWidth="2dp" // 描邊的寬度狈涮。
fresco:roundingBorderColor="@color/border_color" 描邊的顏色。
/>
⑺拿到緩存的Bitmap
ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(Uri.parse(mUrl))
.setProgressiveRenderingEnabled(true)
.build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage
(imageRequest, this);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(@Nullable Bitmap bitmap) {
mIv.setImageBitmap(bitmap);
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
}
}, CallerThreadExecutor.getInstance());