当前位置: 移动技术网 > IT编程>移动开发>Android > Android之Lottie动画详解

Android之Lottie动画详解

2019年04月08日  | 移动技术网IT编程  | 我要评论

敦豪中外运,锤头笠,淄博妇科

文章大纲

一、lottie介绍
二、lottie实战
三、项目源码下载
四、参考文章

 

一、lottie介绍

1. 什么是lottie

  lottie是android和ios的移动库,用于解析adobe after effects动画与bodymovin一起导出为json 并在移动设备上呈现它们!其实在移动端就是通过一个json文件显示相应的动画,其实这样很好的解决了动态改变动画的能力,只需要动态加载相应的josn文件就能实现动画的改变。(josn由美工提供)

2. lottie优点

(1)能够解析渲染通过 ae 上的 bodymovin 插件将 ae 中制作好的动画导出成的 json 文件
(2)数据源多样性—可从assets,sdcard,网络加载动画资源,动态更新
(3)跨平台—设计稿导出一份动画描述文件,android,ios,react native通用(android使用的api不能低于16)

3. lottie动画制作流程图

 

4. lottie例子

 

二、lottie实战

1. 常见的api介绍

(1)判断是否动画正在运行中
  isanimating()
(2)设置动画的进度值(float值)
  setprogress();
(3)监听动画进度
  addanimatorupdatelistener
(4)开始动画
  playanimation()
(5)动态设置json文件
  setcomposition

2. 代码实操

(1)在项目的 build.gradle 文件添加依赖

dependencies {
    implementation 'com.android.support:appcompat-v7:24+'

    //lottie依赖
    implementation 'com.airbnb.android:lottie:1.0.1'

    //网络请求相关依赖
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
}

(2)相关json文件

 

(3)xml加载lottie动画

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingbottom="@dimen/activity_vertical_margin"
    android:paddingleft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingright="@dimen/activity_horizontal_margin"
    android:paddingtop="@dimen/activity_vertical_margin">

    <!--如此,动画就能跑起来了,解释下一下属性:
1.lottie_filename:在app/src/main/assets目录下的动画json文件名。
2.lottie_loop:动画是否循环播放,默认不循环播放。
3.lottie_autoplay:动画是否自动播放,默认不自动播放。
4.alottie_imageassetsfolder:动画所依赖的图片目录,在pp/src/main/assets/目录下的子目录,该子目录存放所有图片。-->
    <com.airbnb.lottie.lottieanimationview
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_filename="data.json"
        app:lottie_loop="true"
        app:lottie_autoplay="true"/>

</linearlayout>

其他常见api:
(1)lottie_repeatcount 重复次数
(2)lottie_repeatmode 设置动画的重复模式restart:重复、reverse:反向
(3)lottie_colorfilter 设置动画的着色颜色,这个就是把你的动画变成了一个颜色的了

activity代码如下所示

/**
 * 简单动画
 */
public class simpleactivity extends appcompatactivity {

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);

        // activity_simple.xml中 lottie_filename="data.json"
        // 所以只需要在 app/src/main/assets 中添加ae 生成的 json文件,重命名为data.json就可以显示动画
        setcontentview(r.layout.activity_simple);
    }
}

(4)activity中加载lottie
xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingbottom="@dimen/activity_vertical_margin"
    android:paddingleft="@dimen/activity_horizontal_margin"
    android:orientation="horizontal"
    android:paddingright="@dimen/activity_horizontal_margin"
    android:paddingtop="@dimen/activity_vertical_margin">

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


    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">


        <textview
            android:id="@+id/tv_seek"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textcolor="@android:color/white"
            android:textsize="16sp" />

        <button
            android:id="@+id/button1"
            android:layout_width="100dip"
            android:layout_height="40dip"
            android:background="@color/colorprimary"
            android:text="开始动画" />

        <button
            android:id="@+id/button2"
            android:layout_width="100dip"
            android:layout_height="40dip"
            android:layout_marginleft="10dip"
            android:background="@color/colorprimary"
            android:text="结束动画" />

    </linearlayout>
</linearlayout>

activity代码如下

package com.zkk.lottietest;

import android.animation.valueanimator;
import android.annotation.suppresslint;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.button;
import android.widget.textview;

import com.airbnb.lottie.lottieanimationview;
import com.airbnb.lottie.model.lottiecomposition;


public class clickactivity extends appcompatactivity {

    private button button1,button2;
    private textview tv_seek;
    lottieanimationview animation_view_click;
    @suppresslint("restrictedapi")
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_click);

        animation_view_click=(lottieanimationview)findviewbyid(r.id.animation_view_click);

        tv_seek=(textview)findviewbyid(r.id.tv_seek);

        button1=(button)findviewbyid(r.id.button1);

        button2=(button)findviewbyid(r.id.button2);

        button1.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

                startanima();
            }
        });
        button2.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

                stopanima();
            }
        });

        //代码设置lottie
        lottiecomposition.fromassetfilename(this, "lottielogo1.json", new lottiecomposition.oncompositionloadedlistener() {

            @override
            public void oncompositionloaded(lottiecomposition composition) {

                animation_view_click.setcomposition(composition);

                animation_view_click.setprogress(0.333f);//progress范围0~1f,设置动画进度

                animation_view_click.playanimation();//播放动画
            }
        });

        //动画状态监听回调
        animation_view_click.addanimatorupdatelistener(new valueanimator.animatorupdatelistener() {
            @override
            public void onanimationupdate(valueanimator animation) {
                tv_seek.settext(" 动画进度" +(int) (animation.getanimatedfraction()*100) +"%");
            }
        });
    }

    /*
     * 开始动画
     */
    private  void startanima(){

        //判断动画是否在运行
        boolean inplaying = animation_view_click.isanimating();

        if (!inplaying) {

            animation_view_click.setprogress(0f);//设置开始时的进度值

            animation_view_click.playanimation();
        }
    }
    /*
    * 停止动画
    */
    private  void stopanima(){

        //判断动画是否在运行
        boolean inplaying = animation_view_click.isanimating();

        if (inplaying) {

            animation_view_click.cancelanimation();
        }
    }

    @override
    protected void onstop() {

        super.onstop();

        animation_view_click.cancelanimation();
    }

}

(5)网络加载动画
xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingbottom="@dimen/activity_vertical_margin"
    android:paddingleft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingright="@dimen/activity_horizontal_margin"
    android:paddingtop="@dimen/activity_vertical_margin">

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

</linearlayout>

activity代码如下

package com.zkk.lottietest;

import android.annotation.suppresslint;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;

import com.airbnb.lottie.lottieanimationview;
import com.airbnb.lottie.model.lottiecomposition;

import org.json.jsonexception;
import org.json.jsonobject;

import java.io.ioexception;

import okhttp3.call;
import okhttp3.callback;
import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.response;


/**
 * 网络请求动画
 */
public class networkactivity extends appcompatactivity {

    lottieanimationview animation_view_network;//lottie组件

    private okhttpclient client;//okhttp客户端

    @override
    protected void oncreate(bundle savedinstancestate) {

        super.oncreate(savedinstancestate);

        setcontentview(r.layout.activity_network);

        //获取lottie组件
        animation_view_network=(lottieanimationview)findviewbyid(r.id.animation_view_network);

        //在线加载lottie的json
        loadurl("http://cdn.trojx.me/blog_raw/lottie_data_edit.json");
    }

    private void loadurl(string url) {

        request request;

        try {

            request = new request.builder()
                    .url(url)
                    .build();

        } catch (illegalargumentexception e) {

            return;
        }


        if (client == null) {

            client = new okhttpclient();
        }
        client.newcall(request).enqueue(new callback() {

            @override public void onfailure(call call, ioexception e) {

            }

            @suppresslint("restrictedapi")
            @override public void onresponse(call call, response response) throws ioexception {

                if (!response.issuccessful()) {
                }

                try {

                    //获取返回的json内容
                    jsonobject json = new jsonobject(response.body().string());

                    //设置动画
                    lottiecomposition
                            .fromjson(getresources(), json, new lottiecomposition.oncompositionloadedlistener() {
                                @override
                                public void oncompositionloaded(lottiecomposition composition) {
                                    setcomposition(composition);
                                }
                            });
                } catch (jsonexception e) {
                }
            }
        });
    }

    private  void setcomposition(lottiecomposition composition){

        animation_view_network.setprogress(0);//设置当前进度

        animation_view_network.loop(true);//动画是否循环播放

        animation_view_network.setcomposition(composition);

        animation_view_network.playanimation();//开始动画

    }

    @override
    protected void onstop() {

        super.onstop();

        animation_view_network.cancelanimation();//取消动画
    }
}

三、项目源码下载

链接:https://pan.baidu.com/s/1fflg3gmws-hh_3nj1n7fwg
密码:1etb

四、参考文章

  1. https://blog.csdn.net/qq_15538877/article/details/80503773

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网