Lottie 源码解析(一)
来源:互联网 发布:js跳转页面保留参数 编辑:程序博客网 时间:2024/05/29 19:24
标签(空格分隔): json lottie
LottieCompostion
composition : 构成
原文注释:
/**
* After Effects/Bodymovin composition model. This is the serialized model from which the
* animation will be created.
* It can be used with a {@link com.airbnb.lottie.LottieAnimationView} or
* {@link com.airbnb.lottie.LottieDrawable}.
*/
这段意思大致是讲:
利用AE或者Bodymovin会构成一个模型,这段序列化的模型将被用于创建动画。它可以与LottieDrawable,LottieAnimationView一起使用
Lottie的三大核心类,在这段注释上全都涉及到了,从语义上也能看出,这个类的核心功能是获得构建动画的数据,也就是用这个类完成对json解析数据的封装
构造方法
首先json数据有多处来源,可能是你工程自带asset文件中的资源,也可能来源于网络,还有同步与异步的解析方法,因此在构造时为了区分不同的来源,此处采用静态工厂设计模式应对不同的数据源。
- 私有构造方法
内部静态工厂类
public static class Factory {
private Factory() {
}
同步解析
/*** 从asset中解析*/static LottieComposition fromFileSync(Context context, String fileName) { InputStream stream; try { stream = context.getAssets().open(fileName); } catch (IOException e) { throw new IllegalStateException("Unable to find file " + fileName, e); } return fromInputStream(context.getResources(), stream);}/*** json解析*/static LottieComposition fromJsonSync(Resources res, JSONObject json) { Rect bounds = null; float scale = res.getDisplayMetrics().density; int width = json.optInt("w", -1); int height = json.optInt("h", -1); if (width != -1 && height != -1) { int scaledWidth = (int) (width * scale); int scaledHeight = (int) (height * scale); bounds = new Rect(0, 0, scaledWidth, scaledHeight); } long startFrame = json.optLong("ip", 0); long endFrame = json.optLong("op", 0); int frameRate = json.optInt("fr", 0); LottieComposition composition = new LottieComposition(bounds, startFrame, endFrame, frameRate, scale); JSONArray assetsJson = json.optJSONArray("assets"); parseImages(assetsJson, composition); parsePrecomps(assetsJson, composition); parseLayers(json, composition); return composition;}
异步解析
两个继承了AsyncTask的异步解析类,一个用来解析文件,一个用来解析json
- JsonCompositionLoader ->CompositionLoader -> AsyncTask
- FileCompositionLoader ->CompositionLoader -> AsyncTask
解析方式差不多,只不过更换成为了异步,实现了一个Cancellable接口,用于取消解析
成员变量
0 0
- Lottie 源码解析(一)
- iOS-Lottie源码解析
- Lottie动画库的使用 & 源码解析
- Android Lottie 使用以及源码解析
- Lottie初探(一)
- Lottie
- Lottie
- 开源项目- Lottie 源码分析
- Lottie动画库 Android 端源码浅析
- Lottie源码简单分析以及使用
- SaltStack源码解析一
- box2d源码解析 一
- TFS源码解析一
- bind源码解析(一)
- EventBus 源码解析(一)
- Lire源码解析一
- quartz源码解析(一)
- Volley源码解析(一)
- RecyclerView分别展示ListView,GrideView,瀑布流效果之总结
- Spark技术在京东智能供应链预测的应用
- Windows下查看一个软件/文件的安装路径
- Android中强弱指针解析
- 学习他人博客中的命令
- Lottie 源码解析(一)
- 提交数据判断kindediter编辑器是否为空
- ListIterator类
- 各种输入框的各种placeholder
- ubuntu下如何搜索文件?
- 遍历List集合(3种方式)
- Linux mount命令
- CDH5.10 ubuntu14.04部署文档--离线安装
- 数据库中B-Tree索引和hash索引的特征