SmartRefresh結(jié)合Lottie實(shí)現(xiàn)一行代碼切換下拉刷新動(dòng)畫

一、介紹

SmartRefreshLayout

是目前使用比較廣泛的一款下拉刷新和上拉加載庫。實(shí)現(xiàn)起來非常方便,可以一鍵修改全局的刷新樣式。而且該庫已經(jīng)提供了大量的刷新效果,其中包括默認(rèn)的 SwipeRefresh 經(jīng)典風(fēng)格,以及一些高級(jí)的 比如游戲刷新、適用于聊天項(xiàng)目的上拉加載更多等等。


Lottie

是 Airbnb 開源的一款動(dòng)畫庫,該庫的優(yōu)勢(shì)是不需要程序員自己寫 Anim 而是將這些工作交給設(shè)計(jì)師處理,最后拿到到只是一個(gè) Json 文件,通過 Lottie 加載 Json 文件就能展示出動(dòng)畫效果。
Lottie社區(qū)已經(jīng)提供了上千套刷新的 Json 文件,如果對(duì)定制要求不是太高,完全可以拿來用,小到點(diǎn)擊效果,大到刷新,啟動(dòng)頁等等。

二、基本使用

SmartRefreshLayout

  • 引入依賴
//默認(rèn)提供三種加載效果
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
//提供了多種現(xiàn)成的刷新Header庫,可以直接用,如不需要可以不集成
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'

注意:同時(shí)需要檢查是否集成了 appcompat ,如果沒集成,需要添加,版本跟隨當(dāng)前
構(gòu)建的版本號(hào)

compile 'com.android.support:appcompat-v7:26.1.0'
  • 布局實(shí)現(xiàn)
<com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/main_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/main_rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
  • 刷新和加載
    如果需要更換刷新的 Header 只需調(diào)用refreshLayout.setRefreshHeader(new MyRefreshAnimHeader);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        if(refreshLayout.isLoading()){
              //關(guān)閉加載
              refreshLayout.finishLoadmore()
          }
    }
});
refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
         if(refreshLayout.isRefreshing()){
              //關(guān)閉刷新
              refreshLayout.finishRefresh();
          }
    }
});

  • 全局加載
    需要自定義 Application ,
public class BaseApplication extends Application{
    public static BaseApplication mContent;
    @Override
    public void onCreate() {
        super.onCreate();
        mContent = this;

    }

    /*設(shè)置全局的下拉刷新樣式*/
    static {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout refreshLayout) {
                return new MyRefreshLottieHeader(mContent);
            }
        });
    }

}

Lottie

  • 引入依賴
compile 'com.airbnb.android:lottie:2.2.0'
  • 布局 XML
    動(dòng)畫 Json 文件需要放在 main 目錄的 assets 中,如果沒有該文件,就手動(dòng)創(chuàng)建一個(gè),再將動(dòng)畫文件拷貝進(jìn)去,然后就可以在布局或者代碼中直接使用。
<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/loading_lottie"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        app:lottie_fileName="anim2.json"/>
  • 動(dòng)畫開始、結(jié)束
    動(dòng)畫的播放和結(jié)束都是只需要一行代碼就可以完成。如果需要在代碼中修改動(dòng)畫文件,只需要調(diào)用mAnimationView.setAnimation(animName);
//開始播放動(dòng)畫
mAnimationView.playAnimation();
//取消播放
mAnimationView.cancelAnimation();

三、結(jié)合Lottie使用

雖然 SmartRefreshLayout 已經(jīng)提供十多種加載的樣式,并且支持對(duì)樣式的顏色等效果的基修改。
但是如果需要實(shí)現(xiàn)其它的加載動(dòng)畫效果,就需要自己來自定義 RefreshHeader。

最簡(jiǎn)單的方法是使用幀動(dòng)畫。但是還需要我們?nèi)ツ玫綆瑘D片,然后再去設(shè)置。
這里我們已經(jīng)引入了 Lottie ,發(fā)現(xiàn)其實(shí)可以利用 Lottie 實(shí)現(xiàn)隨時(shí)一行代碼切換上千種效果豈不是美哉。

lottie動(dòng)畫切換
  • 自定義RefreshHeader
    必須要實(shí)現(xiàn)RefreshHeader類,然后針對(duì)提供的方法,來加入我們自定義的元素
//初始化View
 private void initView(Context context) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.loading_lottie, this);
        mAnimationView = (LottieAnimationView) view.findViewById(R.id.loading_lottie);

    }
//開始刷新
 @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        mAnimationView.playAnimation();
    }
//結(jié)束刷新
    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        mAnimationView.cancelAnimation();
        return 0;
    }

  • 在自定義 SmartRefresh 中提供 Lottie 動(dòng)畫切換的方法
  public void setAnimationViewJson(String animName){
        mAnimationView.setAnimation(animName);
    }

    public void setAnimationViewJson(Animation anim){
        mAnimationView.setAnimation(anim);
    }

四、一行代碼切換刷新動(dòng)畫

mRefreshLottieHeader.setAnimationViewJson("anim2.json");

Demo地址:https://github.com/wapchief/SmartRefreshLottie
已經(jīng)添加了自定義 Anim 幀動(dòng)畫的切換。Demo下載

關(guān)于 SmartRefreshLayout 更詳細(xì)的使用請(qǐng)參考官方文檔
https://github.com/scwang90/SmartRefreshLayout

Lottie基本介紹:一款非常好用的動(dòng)畫庫Lottie

Lottie社區(qū) 可以自行下載動(dòng)畫文件

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

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,073評(píng)論 4 61
  • 最近在弄spring boot 整合shiro的。這里記錄其中一個(gè)錯(cuò)誤: 1:No SecurityManager...
    凱哥Java閱讀 21,343評(píng)論 8 4
  • 1 畢業(yè)前想過千種萬種可能, 畢業(yè)后卻只剩下一種。 從小到大, 父母叫干什么就干什么, 別的小孩去玩, 自己只有眼...
    茶小冪閱讀 752評(píng)論 5 2
  • 退出了那個(gè)小目標(biāo)群。 未來無限可能,闖蕩吧孩子,力爭(zhēng)走好每一步,你的生活你做主,選擇權(quán)在你手里,根據(jù)自己的需求果敢...
    我就是那片云閱讀 219評(píng)論 0 0

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