59

Android Lottie 动画的简单使用

 5 years ago
source link: https://wuzhaohui026.github.io/2018/06/01/Android Lottie-动画的简单使用/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
vYBfqaF.gif

很久很久以前,Airbnb 开源了一个项目叫做 Lottie,我觉得屌爆了。

我来介绍下 Lottie 在 Android 里的使用吧。

虽然很久以前就有人介绍过了,而且还很多。

但是,我偏就再介绍一下。

介绍一下

gayhub 上的地址: lottie-android

效果图

fIFNZ3b.gifNfU3MvE.gif

嗯,就是这样,很炫的。

在 Android 项目里使用

首先,在项目的 build.gradle 文件中引入依赖:

implementation 'com.airbnb.android:lottie:2.5.4'

然后,在 main 文件夹下新建一个 assets 文件夹,用来放动画文件(一个 json 文件)。这个动画文件一般由设计师用 Adobe 旗下的 After Effects (简称 AE)来做个动画出来,之后在 AE 上导入一款叫做 Bodymovin 的插件,可以直接根据 AE 上的动画文件导出 json 文件,这个 json 文件描述了该动画的一些关键点的坐标以及运动轨迹。

我在使用的时候没有设计师,我也不会用 AE,所以我推荐下这个网址: lottiefiles ,上面有很多可以下载的动画文件。

这下,我们正式使用动画了。

有2种方式:

第一种,直接在布局文件中使用。

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        // 放在 res/raw 目录下的动画文件
        app:lottie_rawRes="@raw/hello_world"
        // 放在assets目录下的动画文件
        app:lottie_fileName="hello_world.json"
        // 开启循环
        app:lottie_loop="true"
        // 自动播放
        app:lottie_autoPlay="true" />

第二种,在 Java 代码中使用

LottieAnimationView animationView = findViewById(R.id.animation_view);
animationView.setAnimation(R.raw.hello_world);
// or
animationView.setAnimation(R.raw.hello_world.json);
animationView.playAnimation();

好的,让我们运行一下项目。

然后你就会发现奇迹出现了,没有一张图片,没有一个gif,但是动画效果出来了!就是这么简单,就是这么暴力!

常用的方法

  • LottieAnimationView.loop(true);
    设置动画循环演示。
  • mLottieAnimationView.isAnimating();
    是否在演示中。
  • mLottieAnimationView.setProgress(0.5f);
    设置演示的进度。
  • mLottieAnimationView.getProgress();
    获取演示的进度。
  • mLottieAnimationView.getDuration();
    获取演示的时间。
  • mLottieAnimationView.playAnimation();
    运行动画。
  • mLottieAnimationView.pauseAnimation();
    暂停动画。
  • mLottieAnimationView.cancelAnimation();
    关闭动画。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK