Lottie的使用

一、简介

  Lottie是Airbnb开源的一个面向IOS、Android、React Native的动画库,能分析Adobe After Effects导出的动画,并且能让原生App像使用静态素材一样使用这些动画,完美实现动画效果。
  现在使用个平台的native代码实现一套复杂的动画是一件很困难并且很耗时的事,需要为不同尺寸的屏幕加载不同的素材资源,还需要写大量难维护的代码,而Lottie可以做到同一个通话文件在不同平台上实现相同的效果,极少减少开发时间,实现不同的动画,只需要设置不同的动画文件即可,极少减少开发和维护成本。

二、使用

1.在项目的build.gradle文件添加依赖

dependencies {
    compile 'com.airbnb.android:lottie:2.1.0'
}

2.在布局文件中使用

  动画的文件可以在https://www.lottiefiles.com/这个网址下载。

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animator_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_fileName="trophy.json"
        app:lottie_loop="true"
        app:lottie_autoPlay="true"
        />

3.常用的方法

3.1.animationView.loop(true)

  设置动画循环演示。

3.2.animationView.setAnimation("trophy.json")

  设置动画文件。文件在assets文件夹中。

3.3.animationView.isAnimating()

  是否在演示中。

3.4.animationView.setProgress(0.5f)

  设置演示的进度。

3.5.animationView.getProgress()

  获取演示的进度。

3.6.animationView.getDuration()

  获取演示的时间。

3.7.animationView.palyAnimation()

  运行动画。

3.8.animationView.pauseAnimation()

  暂停动画。

3.9.animationView.cancleAnimation()

  关闭动画。我写的例子,运行animationView.pauseAniamtion()与cancleAnimation()的效果是一样,运行完cacleAnimation()之后,再运行playAnimation()动画不是从头开始,而是接着演示动画,查看源码,查看pauseAniamtion()与cancleAniamtion()的实现,差别只是pauseAnimation()方法多了一个setProgress(progress)而已,而cancleAnimation()没有将progress设置为0,所以显示是一样的。如果要解决,可以在使用cancleAniamtion()之前,加上animationView.setProgress(0)。

参考文章:https://mp.weixin.qq.com/s/LrkZtDZY3SE8IUQ-x1hsmQ

原文地址:https://www.cnblogs.com/zhangmiao14/p/7515760.html