还未了解Lottie的同志,请观看我上篇文章: Lottie Android 初探java
做为IT攻城狮,安装软件这方面还都是很强大的,我就不献丑出安装教程了,最后要记得安装插件bodymovin。android
我是在Android Material 材料风格图标LOGO生成器生成的素材,打开网页,而后选择“从下面挑选一个图标”或滚动页面到下方,随便选择一个图标,我选择的是下载图标。进入配置下载界面,配置完成后点击下载。git
下载后的内容以下: github
打开AE,进入主界面,使用Ctrl+i快捷键导入刚才下载的图标:json
使用Ctrl+N快捷键新建一个合成:bash
左键按住左上方的ic_launcher.png图标拖动到下方操做框中来:app
选择下载的图标,使其位置上移一段距离:oop
左击“位置”左边的圆框,肯定图标起点位置:布局
在右下方的时间轴操做面板中,使“当前时间指示器”移动到1s位置,表示动画时长一秒:post
而后拖动右上方合成面板中图标下移一段距离:
在右下方时间轴面板中,拖动时间标尺上的线和其下方的线条,使他们移动到1s位置,而后点击主界面右侧预览面板的播放按钮便可观看效果;
而后依次选择“窗口”-->“扩展”-->“bodymovin”,打开bodymovin面板,选中Selected下的圆框,选择导出位置,而后点击Render进行渲染导出json格式文件。
至此,AE制做简单的动画并导出json文件完成。
在app module的build.gradle中加入依赖:
dependencies {
compile 'com.airbnb.android:lottie:2.0.0-beta4'
}
复制代码
在main目录下新建assets目录,把导出的data.json文件放入其中。
而后在xml布局中写以下代码:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
复制代码
在Activity中添加:
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
animationView.setAnimation("data.json");
animationView.loop(true);
animationView.playAnimation();
复制代码
其中data.json文件是导入到assets中的文件。
而后运行到手机中,出现以下错误:
java.lang.IllegalStateException: You must set an images folder before loading an image. Set it with LottieComposition#setImagesFolder or LottieDrawable#setImagesFolder
复制代码
在加载image以前必须设置一个image文件夹,能够调用LottieComposition的setImagesFolder 方法或者 LottieDrawable的setImagesFolder方法。
其实,在2.0.0-beta4版本中已经再也不提供使用这两个方法,可使用LottieAnimationView的setImageAssetsFolder方法和LottieDrawable的setImageAssetsFolder方法。
setImageAssetsFolder:
若是你用了Image的资源,您必须显式地指定他们所在的assets文件夹,由于bodymovin导出的文件使用了里面成分序列的以img_ 开头的文件名。你放入到assets中的图片不要重命名图像自己。若是你的图像位于src /main/assets/ airbnb_loader / 文件夹下,你能够调用setImageAssetsFolder(“airbnb_loader /”);。
这样的话,咱们在assets下建立一个lsj文件夹,并把制做动画的原图修更名称为img_0.png后放入lsj下。 之因此修更名称为img_0.png,上面那段话已经解释。或者也能够在data.json中寻找到:
"assets":[{"id":"image_0","w":192,"h":192,"u":"images/","p":"img_0.png"}],
复制代码
咱们把Activity中代码修改成:
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
animationView.setImageAssetsFolder("lsj/");
animationView.setAnimation("data.json");
animationView.loop(true);
animationView.playAnimation();
复制代码
至此,制做动画,导出json文件,展现于Android平台完成。
https://fir.im/lottied
复制代码
[bodymovin] http://jaqen.me/mdpub/
[Lottie Android 初探] http://blog.csdn.net/vitamio/article/details/70046998
[Android Material 材料风格图标LOGO生成器] http://jaqen.me/mdpub/
复制代码