Lottie动画框架入门及源码简析

2023-12-26 04:18

本文主要是介绍Lottie动画框架入门及源码简析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

现在越来越多的APP中添加动画来提升用户体验,下面简单介绍下Airbnb开源的动画框架Lottie的使用

一、基本使用

首先添加依赖
compile ‘com.airbnb.android:lottie:1.0.1’

方法一

1、xml文件中添加布局文件

 <com.airbnb.lottie.LottieAnimationViewandroid:id="@+id/lottie_anim"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

2、Activity中对动画进行控制,注释写的很清楚了

//初始化控件
LottieAnimationView mLottieAnimView= (LottieAnimationView) findViewById(R.id.lottie_anim);/***  添加json格式文件从assets中导入*  第一个参数:Context*  第二个参数:动画文件*  第三个参数:动画数据监听*/LottieComposition.fromAssetFileName(this, "LottieLogo.json", new LottieComposition.OnCompositionLoadedListener() {@Overridepublic void onCompositionLoaded(LottieComposition composition) {//设置动画数据mLottieAnimView.setComposition(composition);//播放动画mLottieAnimView.playAnimation();//设置循环mLottieAnimView.loop(true);}});// 设置动画监听mLottieAnimView.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {}});}

直接运行程序动画就开始播放了

方法二

1、直接通过XML布局文件控制动画,不通过Activity进行操作

<com.airbnb.lottie.LottieAnimationViewandroid:id="@+id/lottie_anim"android:layout_width="wrap_content"android:layout_height="wrap_content"app:lottie_fileName="LottieLogo.json"app:lottie_autoPlay="true"app:lottie_loop="true"/>
属性说明:

//指定动画文件名
app:lottie_fileName=”“

//设置自动播放 true/false
app:lottie_autoPlay=”true”

//设置动画是否循环 true/false
app:lottie_loop=”true”

接下来直接运行项目即可

方法三

1、xml文件中添加布局文件

 <com.airbnb.lottie.LottieAnimationViewandroid:id="@+id/lottie_anim"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="开始"/>

2、通过在Activity中按钮来控制动画播放

 mLottieAnim = (LottieAnimationView) findViewById(R.id.lottie_anim);Button mBtnStart= (Button) findViewById(R.id.btn_start);mBtnStart.setOnClickListener(this);//通过直接指定动画名的方式设置动画mLottieAnim.setAnimation("LottieLogo.json");//点击后播放@Overridepublic void onClick(View v) {//判断动画是否在播放if (!mLottieAnim.isAnimating()){//设置进度mLottieAnim.setProgress(0);//开始播放动画mLottieAnim.playAnimation();}}

OK,简单使用介绍完毕

二、常用API

        // 判断动画是否正在播放中mLottieAnim.isAnimating()//设置动画数据  可以接收json格式数据  String格式 、Animation格式mLottieAnim.setAnimation();//播放动画mLottieAnim.playAnimation();//暂停动画mLottieAnim.pauseAnimation();//取消动画mLottieAnim.cancelAnimation();//获取动画时长,一定要在动画开始播放后才能获取否则为0mLottieAnim.getDuration();// 设置动画更新监听mLottieAnim.addAnimatorUpdateListener();//移除动画更新监听mLottieAnim.removeUpdateListener();//动画监听,可以用于监听动画的开始、结束、取消、重复mLottieAnim.addAnimatorListener();// 移除动画监听mLottieAnim.removeAnimatorListener();// 设置合成物  LottieComposition类型mLottieAnim.setComposition();// 设置循环mLottieAnim.loop();// 添加assets中动画文件LottieComposition.fromAssetFileName()// 使用同步方式接收动画文件LottieComposition.fromFileSync();// 添加json格式动画文件LottieComposition.fromJson()// 使用同步方式接收json格式文件LottieComposition.fromJsonSync()// 接收流的形式动画LottieComposition.fromInputStream()

三、源码分析

由简到难,一步步分析

1、类LottieComposition中方法

A: 加载assets中文件

/*** Loads a composition from a file stored in /assets.*/public static Cancellable fromAssetFileName(Context context, String fileName, OnCompositionLoadedListener loadedListener) {InputStream stream;try {stream = context.getAssets().open(fileName);} catch (IOException e) {throw new IllegalStateException("Unable to find file " + fileName, e);}return fromInputStream(context, stream, loadedListener);}

可以看到通过读取assets中文件返回一个流格式,直接跳到fromInputStream()方法

B:接下来我们查看fromInputStream()方法

 /*** Loads a composition from an arbitrary input stream.** ex: fromInputStream(context, new FileInputStream(filePath), (composition) -> {});*/public static Cancellable fromInputStream(Context context, InputStream stream, OnCompositionLoadedListener loadedListener) {FileCompositionLoader loader = new FileCompositionLoader(context.getResources(), loadedListener);loader.execute(stream);return loader;}

通过上面代码我们发现直接通过FileCompositionLoader来执行流文件 loader.execute(stream);

C:查看FileCompositionLoader

private static final class FileCompositionLoader extends CompositionLoader<InputStream> {private final Resources res;private final OnCompositionLoadedListener loadedListener;FileCompositionLoader(Resources res, OnCompositionLoadedListener loadedListener) {this.res = res;this.loadedListener = loadedListener;}@Overrideprotected LottieComposition doInBackground(InputStream... params) {return fromInputStream(res, params[0]);}@Overrideprotected void onPostExecute(LottieComposition composition) {loadedListener.onCompositionLoaded(composition);}}

发现FileCompositionLoader继承CompositionLoader,并且在doInBackground()方法中返回了方法
fromInputStream(res, params[0]);

D:查看CompositionLoader

  private abstract static class CompositionLoader<Params>extends AsyncTask<Params, Void, LottieComposition>implements Cancellable {@Overridepublic void cancel() {cancel(true);}}

CompositionLoader继承了AsyncTask可见该类是采用异步执行的方式加载流

E:fromInputStream(res, params[0])
C中我们发现在方法doInBackground()中异步执行了该方法,继续分析

 @SuppressWarnings("WeakerAccess")public static LottieComposition fromInputStream(Resources res, InputStream file) {try {int size = file.available();byte[] buffer = new byte[size];//noinspection ResultOfMethodCallIgnoredfile.read(buffer);file.close();String json = new String(buffer, "UTF-8");JSONObject jsonObject = new JSONObject(json);return LottieComposition.fromJsonSync(res,jsonObject);} catch (IOException e) {throw new IllegalStateException("Unable to find file.", e);} catch (JSONException e) {throw new IllegalStateException("Unable to load JSON.", e);}}

通过查看发现该方法将数据转化成json格式并返回给LottieComposition.fromJsonSync(res,jsonObject)方法执行,可见fromJsonSync对json数据进行了解析操作

F:继续跟进方法

@SuppressWarnings("WeakerAccess")public static LottieComposition fromJsonSync(Resources res, JSONObject json) {LottieComposition composition = new LottieComposition(res);int width = -1;int height = -1;try {width = json.getInt("w");height = json.getInt("h");} catch (JSONException e) {// ignore.}if (width != -1 && height != -1) {int scaledWidth = (int) (width * composition.scale);int scaledHeight = (int) (height * composition.scale);if (Math.max(scaledWidth, scaledHeight) > MAX_PIXELS) {float factor = (float) MAX_PIXELS / (float) Math.max(scaledWidth, scaledHeight);scaledWidth *= factor;scaledHeight *= factor;composition.scale *= factor;}composition.bounds = new Rect(0, 0, scaledWidth, scaledHeight);}try {composition.startFrame = json.getLong("ip");composition.endFrame = json.getLong("op");composition.frameRate = json.getInt("fr");} catch (JSONException e) {//}if (composition.endFrame != 0 && composition.frameRate != 0) {long frameDuration = composition.endFrame - composition.startFrame;composition.duration = (long) (frameDuration / (float) composition.frameRate * 1000);}try {JSONArray jsonLayers = json.getJSONArray("layers");for (int i = 0; i < jsonLayers.length(); i++) {Layer layer = Layer.fromJson(jsonLayers.getJSONObject(i), composition);addLayer(composition, layer);}} catch (JSONException e) {throw new IllegalStateException("Unable to find layers.", e);}// These are precomps. This naively adds the precomp layers to the main composition.// TODO: Significant work will have to be done to properly support them.try {JSONArray assets = json.getJSONArray("assets");for (int i = 0; i < assets.length(); i++) {JSONObject asset = assets.getJSONObject(i);JSONArray layers = asset.getJSONArray("layers");for (int j = 0; j < layers.length(); j++) {Layer layer = Layer.fromJson(layers.getJSONObject(j), composition);addLayer(composition, layer);}}} catch (JSONException e) {// Do nothing.}return composition;}

通过查看方法,方法中对json格式数据进行了解析并且通过 LottieComposition composition对象进行了接收并且调用了Layer.fromJson和addLayer(composition, layer);方法

G:查看Layer.fromJson及addLayer(composition, layer);

通过fromJson()方法将json数据解析并赋值给layer

 static Layer fromJson(JSONObject json, LottieComposition composition) {Layer layer = new Layer(composition);try {if (L.DBG) Log.d(TAG, "Parsing new layer.");layer.layerName = json.getString("nm");if (L.DBG) Log.d(TAG, "\tName=" + layer.layerName);layer.layerId = json.getLong("ind");if (L.DBG) Log.d(TAG, "\tId=" + layer.layerId);layer.frameRate = composition.getFrameRate();int layerType = json.getInt("ty");if (layerType <= LottieLayerType.Shape.ordinal()) {layer.layerType = LottieLayerType.values()[layerType];} else {layer.layerType = LottieLayerType.Unknown;}.....省略代码段return layer;

通过addLayer方法将解析出来的json数据Layer添加到composition.layers中
可以将composition理解为一个包含图层信息的对象

 private static void addLayer(LottieComposition composition, Layer layer) {composition.layers.add(layer);composition.layerMap.put(layer.getId(), layer);if (!layer.getMasks().isEmpty()) {composition.hasMasks = true;}if (layer.getMatteType() != null && layer.getMatteType() != Layer.MatteType.None) {composition.hasMattes = true;}}

H:通过以上的分析我们只发现了composition对象存储了解析出来的json信息,下面我们就分析怎样将composition对象转化为动画
接下来查看LottieAnimationView中的setAnimation()方法

  /*** Sets the animation from a file in the assets directory.* This will load and deserialize the file asynchronously.** Will not cache the composition once loaded.*/public void setAnimation(String animationName) {setAnimation(animationName, CacheStrategy.None);}

上面的方法返回setAnimation(animationName, CacheStrategy.None);
那么我们接着查看setAnimation方法做了什么

 @SuppressWarnings("WeakerAccess")public void setAnimation(final String animationName, final CacheStrategy cacheStrategy) {this.animationName = animationName;if (weakRefCache != null && weakRefCache.containsKey(animationName)) {WeakReference<LottieComposition> compRef = weakRefCache.get(animationName);if (compRef.get() != null) {setComposition(compRef.get());return;}} else if (strongRefCache != null && strongRefCache.containsKey(animationName)) {setComposition(strongRefCache.get(animationName));return;}isAnimationLoading = true;setProgressWhenCompositionSet = false;playAnimationWhenCompositionSet = false;this.animationName = animationName;cancelLoaderTask();compositionLoader = LottieComposition.fromAssetFileName(getContext(), animationName, new LottieComposition.OnCompositionLoadedListener() {@Overridepublic void onCompositionLoaded(LottieComposition composition) {if (cacheStrategy == CacheStrategy.Strong) {if (strongRefCache == null) {strongRefCache = new HashMap<>();}strongRefCache.put(animationName, composition);} else if (cacheStrategy == CacheStrategy.Weak) {if (weakRefCache == null) {weakRefCache = new HashMap<>();}weakRefCache.put(animationName, new WeakReference<>(composition));}setComposition(composition);}});} 

通过上面的方法我们发现最后调用了 setComposition(composition);
继续分析,查看setComposition(composition);

  public void setComposition(@NonNull LottieComposition composition) {if (L.DBG) {Log.v(TAG, "Set Composition \n" + composition);}lottieDrawable.setCallback(this);lottieDrawable.setComposition(composition);// If you set a different composition on the view, the bounds will not update unless// the drawable is different than the original.setImageDrawable(null);setImageDrawable(lottieDrawable);isAnimationLoading = false;if (setProgressWhenCompositionSet) {setProgressWhenCompositionSet = false;setProgress(progress);} else {setProgress(0f);}this.composition = composition;if (playAnimationWhenCompositionSet) {playAnimationWhenCompositionSet = false;playAnimation();}requestLayout();}

通过上面的分析发现方法 lottieDrawable.setComposition(composition);处理了接收到的composition对象
继续跟踪该方法

 public void setComposition(LottieComposition composition) {if (getCallback() == null) {throw new IllegalStateException("You or your view must set a Drawable.Callback before setting the composition. This gets done automatically when added to an ImageView. " +"Either call ImageView.setImageDrawable() before setComposition() or call setCallback(yourView.getCallback()) first.");}clearComposition();this.composition = composition;animator.setDuration(composition.getDuration());setBounds(0, 0, composition.getBounds().width(), composition.getBounds().height());buildLayersForComposition(composition);getCallback().invalidateDrawable(this);}private void clearComposition() {recycleBitmaps();clearLayers();}

有木有发现一个很熟悉的单词
animator.setDuration(composition.getDuration());
到这里我们终于将composition和动画建立了联系
最终我们发现了下面这个方法
private final ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
哈哈,原来是通过ValueAnimator来实现的,好了简单分析了下,就到这里,如有不足,请指正。

参考:http://www.jianshu.com/p/0882ea3b59e3

这篇关于Lottie动画框架入门及源码简析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/538081

相关文章

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很