Lottie动画库最新版使用方法详解 教程 示例

版权声明:本文为博主原创文章,如需转载请注明出处 https://blog.csdn.net/u013640004/article/details/73822078

QQ群交流:425219113(计算机语言交流)

Lottie是一个可以播放由AE打包的json动画文件的开源库,使用它,你可以简单实现播放利用AE制作的动画。

Lottie项目地址:https://github.com/airbnb/lottie-android
这里写图片描述
这里写图片描述

使用方法:
如上所述,该库需要配合AE制作json动画来使用,AE方面的具体方法可以参考这篇博客:http://www.jianshu.com/p/9a2136ecbc7b
下面我主要说的是在android studio 上的使用方法(eclipse需要自行提取aar的文件搭建项目)。

一、导入Lottie库。

dependencies {
	//目前最新版是2.8.0
	implementation 'com.airbnb.android:lottie:2.5.5'
}

二、添加Lottie组件。

<com.airbnb.lottie.LottieAnimationView
         android:id="@+id/animationView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:lottie_autoPlay="true"
         app:lottie_fileName="anim/done.json"
         app:lottie_imageAssetsFolder="anim/images"
         app:lottie_scale="0.3"
         />
字段 作用
lottie_autoPlay 是否自动播放
lottie_fileName json 文件路径(assets)
lottie_imageAssetsFolder 图片资源路径(如果有图片资源,则必须要填,否则会崩溃)
lottie_scale 动画居中缩放
lottie_progress 播放进度
lottie_repeatCount 循环次数 -1无限循环 0不循环
lottie_repeatMode 循环模式

这些属性也是可以在代码里面设置的

animationView.setImageAssetsFolder(“anim/images”);
animationView.setAnimation(“anim/chest.json”);
animationView.playAnimation();

三、高级用法

我在使用的过程中发现了这个组件的一些bug,那就是,当你的应用中用到了大量的AsyncTask,会导致动画播放不出来,因为我也不敢想象,它会用AsyncTask来作为异步线程,而总所周知,AsyncTask是会无脑阻塞的。看他源码。

public void setAnimation(final String animationName, final CacheStrategy cacheStrategy) {
	...
	compositionLoader = LottieComposition.Factory.fromAssetFileName(getContext(), animationName,
        new OnCompositionLoadedListener() {
          @Override public void onCompositionLoaded(LottieComposition composition) {
            if (cacheStrategy == CacheStrategy.Strong) {
              ASSET_STRONG_REF_CACHE.put(animationName, composition);
            } else if (cacheStrategy == CacheStrategy.Weak) {
              ASSET_WEAK_REF_CACHE.put(animationName, new WeakReference<>(composition));
            }

            setComposition(composition);
          }
        });
}
public static Cancellable fromAssetFileName(
        Context context, String fileName, OnCompositionLoadedListener listener) {
      InputStream stream;
      try {
        stream = context.getAssets().open(fileName);
      } catch (IOException e) {
        throw new IllegalArgumentException("Unable to find file " + fileName, e);
      }
      return fromInputStream(stream, listener);
}
   public static Cancellable fromInputStream(
        InputStream stream, OnCompositionLoadedListener listener) {
      return fromJsonReader(new JsonReader(new InputStreamReader(stream)), listener);
    }
 public static Cancellable fromJsonReader(
        JsonReader reader, OnCompositionLoadedListener listener) {
      AsyncCompositionLoader loader = new AsyncCompositionLoader(listener);
      loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, reader);
      return loader;
    }
public final class AsyncCompositionLoader
    extends AsyncTask<JsonReader, Void, LottieComposition> implements Cancellable {}

看,这里的AsyncCompositionLoader其实就是继承AsyncTask的,而 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, reader);是有线程数量限制的,好像是5个。而同样的,现在很多广告SDK都是无脑地使用了AsyncTask,导致很容易形成大塞车的情景。

解决方法

使用以下代码代替setAnimation,同时,fromFileSync可以放到自己的线程里面,但setComposition和playAnimation需要放到UI线程里面。当然,如果是小json文件,直接全部放在UI线程里面也行。

LottieComposition lottieComposition = LottieComposition.Factory.fromFileSync(context,"anim/done.json");
if(lottieComposition!=null)
{
	lottieAnimationView.setComposition(lottieComposition);
	lottieAnimationView.playAnimation();
}

四、需要注意

1、资源是要放在assets中的

我这里的目录结构为:
json文件: assets/anim/done.json
图片资源: assets/anim/images/image_0.png

2、assets区分大小写,哪怕是后缀名

3、图片资源文件名必须要和json文件里面的相应图片资源文件名相同。

我们来看一下json动画文件的基本格式。

{"v":"4.7.1","fr":25,...,"assets":[{"id":"image_0","w":100,"h":100,"u":"images/","p":"image_0.png"}]

重点,“p”:“image_0.png”。图片资源的文件名必须要和这里面的名称相同。因为AE生成的json动画会将图片资源默认命名为img_0.png,img_1.png等等。而美工可能给你的图片是经过她改名称的,这时就需要自己改变其中的名称了。

如果需要对动画进行定位和缩放,这里还有一篇文章做了介绍。

http://blog.csdn.net/u013640004/article/details/75667228

猜你喜欢

转载自blog.csdn.net/u013640004/article/details/73822078