Android強大的圖片加載框架Fresco簡單用法

Android關(guān)于加載圖片的框架有Universal-Image-Loader、Picasso、Volley、Fresco,這篇文章主要簡單的分析一下Fresco的用法,F(xiàn)resco是Facebook發(fā)布的一款開源框架,在內(nèi)存方面的表現(xiàn)極為優(yōu)秀。
Fresco中文說明:http://www.fresco-cn.org/
Fresco項目GitHub地址:https://github.com/facebook/fresco
假如有這樣的一個需求當圖片正在加載時應(yīng)該呈現(xiàn)正在加載時的圖像,當圖片加載失敗時應(yīng)該呈現(xiàn)圖片加載時的圖像,當重新加載圖片時,應(yīng)該呈現(xiàn)重試時圖像,直到這張圖片加載完成。這時建議推薦用Fresco。
本例子請求的圖片地址是存在的,如果想查看重新加載
Fresco用法及其簡單:在項目中引入Fresco,只需要在Module的build.gradle文件的dependencies中添加一句代碼

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

即可。

效果:

加載效果

我們來看如何實現(xiàn)

新建一個activity_main.xml布局文件

<RelativeLayout 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_margin="20dp"
    android:layout_height="match_parent">
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/main_sdv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:actualImageScaleType="focusCrop"
        fresco:placeholderImage="@mipmap/default_error"
        fresco:placeholderImageScaleType="focusCrop"
        fresco:progressBarImage="@mipmap/icon_progress_bar"
        fresco:progressBarImageScaleType="focusCrop"
        fresco:progressBarAutoRotateInterval="5000"
        fresco:failureImage="@mipmap/icon_failure"
        fresco:failureImageScaleType="focusCrop"
        fresco:retryImageScaleType="focusCrop"
        fresco:fadeDuration="5000"/>
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/main_sdv2"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/main_sdv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:actualImageScaleType="focusCrop"
        fresco:placeholderImage="@mipmap/default_error"
        fresco:placeholderImageScaleType="focusCrop"
        fresco:progressBarImage="@mipmap/icon_progress_bar"
        fresco:progressBarImageScaleType="focusCrop"
        fresco:progressBarAutoRotateInterval="5000"
        fresco:failureImage="@mipmap/icon_failure"
        fresco:failureImageScaleType="focusCrop"
        fresco:retryImage="@mipmap/icon_retry"
        fresco:retryImageScaleType="focusCrop"
        fresco:fadeDuration="5000"
        fresco:backgroundImage="@android:color/holo_orange_light"
        fresco:roundAsCircle="true"
        fresco:roundedCornerRadius="30dp"
        fresco:roundTopLeft="true"
        fresco:roundTopRight="true"
        fresco:roundBottomLeft="true"
        fresco:roundBottomRight="true"
        fresco:roundingBorderWidth="10dp"
        fresco:roundingBorderColor="#008dd7"/>
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/main_sdv3"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/main_sdv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:actualImageScaleType="focusCrop"
        fresco:placeholderImage="@mipmap/default_error"
        fresco:placeholderImageScaleType="focusCrop"
        fresco:progressBarImage="@mipmap/icon_progress_bar"
        fresco:progressBarImageScaleType="focusCrop"
        fresco:progressBarAutoRotateInterval="5000"
        fresco:failureImage="@mipmap/icon_failure"
        fresco:failureImageScaleType="focusCrop"
        fresco:retryImage="@mipmap/icon_retry"
        fresco:retryImageScaleType="focusCrop"
        fresco:fadeDuration="5000"
        fresco:backgroundImage="@android:color/holo_orange_light"
        fresco:roundAsCircle="true"
        fresco:roundedCornerRadius="30dp"
        fresco:roundTopLeft="true"
        fresco:roundTopRight="true"
        fresco:roundBottomLeft="true"
        fresco:roundBottomRight="true"
        fresco:roundingBorderWidth="10dp"
        fresco:roundingBorderColor="#008dd7"
        ></com.facebook.drawee.view.SimpleDraweeView>
</RelativeLayout>

** MainActivity java文件**

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
public class MainActivity extends Activity {
    private SimpleDraweeView simpleDraweeView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        //創(chuàng)建SimpleDraweeView對象
        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.main_sdv);
        //創(chuàng)建將要下載的圖片的URI
        Uri imageUri = Uri.parse("http://avatar.csdn.net/5/D/9/1_vatty748895431.jpg");
        //開始下載
        simpleDraweeView.setImageURI(imageUri);
        //創(chuàng)建DraweeController
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                //重試之后要加載的圖片URI地址
                .setUri(imageUri)
                        //設(shè)置點擊重試是否開啟
                .setTapToRetryEnabled(true)
                        //設(shè)置舊的Controller
                .setOldController(simpleDraweeView.getController())
                        //構(gòu)建
                .build();
        //設(shè)置DraweeController
        simpleDraweeView.setController(controller);
    }
}

從上面可以看出來,F(xiàn)resco的一個很大的特點在于,它自定義了一個SimpleDraweeView,而不是直接在ImageView上進行操作。目前SimpleDraweeView是繼承自ImageView的,然而很多ImageView的方法已經(jīng)被@Deprecated掉了,不建議使用使用Fresco有一些需要注意的地方:一定不要忘了Fresco庫的初始化。

Fresco.initialize(this); 

通常這一句將在Application的onCreate中是比較合適的,如果只有一個Activity,那么加在Activity里也可以,但要在setContentView之前,也就是要先初始化庫,才能完成布局文件的加載。SimpleDraweeView的width和height屬性必須是明確值,而不能直接用wrap_content這種內(nèi)容填充的數(shù)值,當然有一種情況例外,就是寬度和高度有一個是固定的,然后設(shè)置了二者的比例,則另一個可以用wrap_content,其實相當于二者都是固定的。

mSimpleDraweeView.setAspectRatio(1.33f); // 設(shè)置寬高比為4:3  

小結(jié)

屬性圖

這樣關(guān)于Fresco加載圖片就算基本講完。
如需要源碼,請狠狠戳這里源碼下載地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容