Lottie,一个强大的移动端动画库
来源:互联网 发布:卡通形象设计软件 编辑:程序博客网 时间:2024/06/07 23:18
Lottie是一个针对移动端打造的动画库,其内部根据json解析出的路径在onDraw下绘制多个图层的每一帧动画实现高效流畅的效果,本文简单介绍其使用,以帮助读者更好的理解。
通过Gradle添加依赖
dependencies { compile 'com.airbnb.android:lottie:2.1.0'}
使用方式一:XML加载
<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_autoPlay="true" />
其中三个自定义属性分别对应了json文件在Assert文件夹下的名称,动画是否循环,是否自动播放动画。
使用方式二:代码加载
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);animationView.setAnimation("hello-world.json");animationView.loop(true);animationView.playAnimation();
对于以上的两种方式而言比较适合单一的动画加载,如果需要在RecyclerView等控件中展示多个相同的动画,请使用下面的方式
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();
和属性动画类似,Lottie具有高度的可控性
animationView.addAnimatorUpdateListener((animation) -> { // Do something.});animationView.playAnimation();...if (animationView.isAnimating()) { // Do something.}...animationView.setProgress(0.5f);...// Custom animation speed or duration.ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f) .setDuration(500);animator.addUpdateListener(animation -> { animationView.setProgress(animation.getAnimatedValue());});animator.start();...animationView.cancelAnimation();
在最新的2.1.0版本下,你能轻松的为动画添加ColorFilter
// Any class that conforms to the ColorFilter interfacefinal PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);// Adding a color filter to the whole viewanimationView.addColorFilter(colorFilter);// Adding a color filter to a specific layeranimationView.addColorFilterToLayer("hello_layer", colorFilter);// Adding a color filter to specfic content on the "hello_layer"animationView.addColorFilterToContent("hello_layer", "hello", colorFilter);// Clear all color filtersanimationView.clearColorFilters();
同样,Lottie也支持通过XML的方式添加ColorFilter,此时的合并规则为PorterDuff.Mode.SRC_ATOP:
<com.airbnb.lottie.LottieAnimationView android:layout_width="wrap_content" android:layout_height="wrap_content" app:lottie_fileName="hello-world.json" app:lottie_colorFilter="@color/blue" />
通过LottieComposition.Factory的默认方法加载json文件
。
fromAssetFileName:从Assert文件夹下加载资源文件fromFileSync:同上,但是同步的加载方式fromInputStream:从文件流加载fromJson(Resources res, JSONObject json,OnCompositionLoadedListener loadedListener):通过JSONObject的方式加载网络文件
简单的加载的演示:
LottieComposition.Factory.fromAssetFileName(this, "hello-world.json", new OnCompositionLoadedListener() { @Override public void onCompositionLoaded(@Nullable LottieComposition composition) { if (composition != null) { lav.setComposition(composition); } lav.playAnimation(); } });
如果你的json文件附带了图片资源并放置在Assert或其子文件夹下,那么请使用setImageAssetsFolder指明对应路径,如
call setImageAssetsFolder(images/hello)
如果你需要加载非assert下的图片资源请通过如下方式
animationView.setImageAssetDelegate(new ImageAssetDelegate() { @Override public Bitmap fetchBitmap(LottieImageAsset asset) { getBitmap(asset); } });
LottieImageAsset用于帮助指定Bitmap的宽高等信息。
基本就是一篇翻译文了,写的比较快,有什么疑问可以留言讨论。
阅读全文
0 0
- Lottie,一个强大的移动端动画库
- Lottie-移动动画效果框架
- Lottie动画库的使用 & 源码解析
- Lottie动画库 Android 端源码浅析
- android--Airbnb 动画库Lottie
- Lottie安卓开源动画库使用
- Lottie安卓开源动画库使用
- Lottie安卓开源动画库使用
- Lottie安卓开源动画库使用
- Android Lottie动画库介绍
- 强大的动画库
- Lottie一个可轻松实现各种动画(SVG、Animations)的开源项目
- Android Lottie动画的简单使用
- Lottie开源动画库使用教程
- airbnb开源动画库lottie
- Lottie动画库使用方法详解 教程 示例
- 开源App动画Lottie
- Airbnb的开源项目Lottie简易实现动画
- Incorrect datetime value: '0000-00-00 00:00:00' for column
- 第七章 进程控制及操作(一)
- 【Python网络爬虫 】新手实践笔记--urllib2
- oralce无法删除用户ORA-01940:无法删除当前连接的用户
- 重启Storm时,启动Supervisor 启动后自动退出
- Lottie,一个强大的移动端动画库
- AdapterViewFlipper
- Python的时间函数
- JsonObject
- 元素等待————显式等待一
- CentOS 安装uwsgi提示找不到Python.h文件
- maven 打包报数组越界问题
- lombok 介绍及基本使用方法
- Android判断进程在前后台最靠谱的方法