Lottie是什么
Lottie是Airbnb 开源的一个项目,我在知道了这个项目后感觉真的遇见了新大陆一样
想必android开发的程序猿们都或多或少的为动画的实现苦恼过,或者都曾认为,动画这个实现在android的代码里总感觉会消耗系统的性能,那么今天说的这个Lottie,就解决了所有的烦恼,一起来看实现方法吧!走起来!!!!!
首先说一下,动画效果是由UI 或者设计通过AE来做一个后缀为.json的文件,如果有图片,还需要把图片一并给你。
Lottie支持最小API 16及以上。
1. 第一步 不用犹豫,添加支持
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.airbnb.android:lottie:2.2.5'
}
其他的忽略 就是加上 compile 'com.airbnb.android:lottie:2.2.5' 这一句;
2. 添加assets文件
把kong.json文件和images文件夹放在assets里(imges文件夹就存放的动画所需要的图片素材)
说明一下 kong.json需要图片 那就把图片文件夹一起放到assets里,data.json 没有图片就直接放进assets里就可以了,
3. xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccddcc">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
布局就不用多说了。
4. 代码部分
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LottieAnimationView animation= findViewById(R.id.animation_view);
animation.setImageAssetsFolder("images/");
animation.setAnimation("kong.json");
animation.loop(true);
animation.playAnimation();
}
}
没有图片就去掉setImageAssetsFolder() 这个方法。
怎么样,简单吧,效果还666呢。(弱的不会做gif)