Android Lottie动画的简单使用

简介

在Android中做动画效果无非是以下几种方法:

  1. 普通动画
  2. 帧动画
  3. 属性动画
  4. 通过改变LayoutParams布局参数来实现动画

现如今在Github上有一个比较火的动画库Lottie,Github上关于Lottie库介绍大概是这样的: 
Lottie是一个为Android和iOS设备提供的一个开源框架,它能够解析通过Adobe After Effects 软件做出来的动画,动画文件通过Bodymovin导出json文件,然后由Lottie中的LottieAnimationView解析json渲染动画。 

官方给出来的demo动画效果如下:


也就是说Lottie框架可以通过解析json文件来实现动画效果,而无需开发写太多的代码去控制动画。

Lottie Github地址:https://github.com/airbnb/lottie-android

Lottie的使用

1.下载Lottie库

在你工程的build.gradle文件里添加如下配置

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

2.简单使用

Lottie支持API14以及以上,动画简单使用实例如下:

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_fileName="hello-world.json"
        app:lottie_loop="true"
        app:lottie_imageAssetsFolder="images"
        app:lottie_autoPlay="true" />

如此,动画就能跑起来了,解释下一下属性: 
1.lottie_fileName:在app/src/main/assets目录下的动画json文件名。 
2.lottie_loop:动画是否循环播放,默认不循环播放。 
3.lottie_autoPlay:动画是否自动播放,默认不自动播放。 
4.lottie_imageAssetsFolder:动画所依赖的图片目录,在app/src/main/assets/目录下的子目录,该子目录存放所有图片。


你还可以在代码中这样使用:

LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
animationView.setAnimation("hello-world.json");//在assets目录下的动画json文件名。
animationView.loop(true);//设置动画循环播放
animationView.setImageAssetsFolder("images/");//assets目录下的子目录,存放动画所需的图片
animationView.playAnimation();//播放动画

Lottie框架会在后台自动解析json动画配置文件,解析完后开始跑动画。

你也可以这样使用LottieAnimationView:

 LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
 ...
 Cancellable compositionCancellable = LottieComposition.Factory.fromJson(getResources(), jsonObject, (composition) -> {
     animationView.setComposition(composition);
     animationView.playAnimation();
 });

 // Cancel to stop asynchronous loading of composition
 // compositionCancellable.cancel();
  • 3.控制动画添加动画监听
animationView.addAnimatorUpdateListener((animation) -> {
    // Do something.动画状态监听回调
});
animationView.playAnimation();//播放动画
...
if (animationView.isAnimating()) {
    // Do something.动画正在运行
}
...
//progress范围0~1f,设置动画进度
animationView.setProgress(0.5f);
...
// 自定义动画时长,此处利用ValueAnimator值动画来实时更新AnimationView的进度来达到控制动画时长。
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f)
    .setDuration(500);
animator.addUpdateListener(animation -> {
    animationView.setProgress(animation.getAnimatedValue());
});
animator.start();//启动动画
...
animationView.cancelAnimation();//取消动画

由以上代码可知:你可以随意控制动画的时常,动画的播放进度,动画的各种状态等。

Lottie动画缓存策略

由于Lottie框架是解析json文件来做动画效果的,解析json文件是I/O耗时操作,为了提高动画加载速度,在同一个动画需要多处多次使用时,就有必要对解析json的结果进行缓存,以免每次都解析json文件耗时操作。所以Lottie框架提供了三种不同程度的动画缓存策略:

  /**
   * Caching strategy for compositions that will be reused frequently.
   * Weak or Strong indicates the GC reference strength of the composition in the cache.
   */
  public enum CacheStrategy {
    None,
    Weak,
    Strong
  }

  • None 无缓存
  • Weak 弱引用缓存
  • Strong 强引用缓存

。你可以按照如下方法使用:

1.在LottieAnimationView控件的布局中添加如下属性:

app:lottie_cacheStrategy="weak"
  • 1

默认情况下是无缓存的。

2.你也可以在代码中如此使用:

animationView.setAnimation("hello_world.json", LottieAnimationView.CacheStrategy.Weak);
  • 1

如果第二个参数未设置时,默认无缓存。


猜你喜欢

转载自blog.csdn.net/qq_32666413/article/details/80537485