介绍

Lottie 是Airbnb推出Library 它可将After Effects动画经由Bodymovi扩展插件输入成一个JSON动画文件格式, 适用于 Web、iOS、Android、Windows、QT、Tizen 和其他平台。
lottie_1

使用

如果你有After Effects动画, 可通过AE插件Bodymovi 生成JSON动画

没有After Effects动画可以让设计来制作, 或者学习制作~

当然也可以使用 Lottie Files 提供的免费动画

lottie_2

接着从Lottie Files下载一个动画任意动画

Flutter中使用

  1. lottie json 放入项目assets 目录下
  2. 安装 lottie
    1
    2
    dependencies:
    lottie: ^1.3.0
  3. 使用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import 'package:flutter/material.dart';
    import 'package:lottie/lottie.dart';

    void main() => runApp(const MyApp());

    class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    body: Center(
    child: Container(child: Lottie.asset('assets/lottie/animation.json')),
    )),
    );
    }
    }

    lottie_3

我们可以使用AnimationController 控制动画

  • forward() 启动
  • stop() 停止
  • reverse() 反向播放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';

void main() => runApp(LottieScreen());

class LottieScreen extends StatefulWidget {
LottieScreen({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _LottieScreenState();
}

class _LottieScreenState extends State<LottieScreen>
with SingleTickerProviderStateMixin {
late AnimationController lottieController;

@override
void initState() {
super.initState();

lottieController = AnimationController(
vsync: this,
);

lottieController.addStatusListener((status) async {
if (status == AnimationStatus.completed) {
lottieController.reset();
}
});
}

@override
void dispose() {
lottieController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(children: [
Lottie.asset("assets/lottie/animation.json",
repeat: false,
controller: lottieController, onLoaded: (composition) {
lottieController.duration = composition.duration;
lottieController.forward();
}),
const SizedBox(
height: 24,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: () {
lottieController.forward();
},
child: Text("启动"),
),
RaisedButton(
onPressed: () {
lottieController.stop();
},
child: Text("停止"),
),
RaisedButton(
onPressed: () {
lottieController.reverse();
},
child: Text("反向播放"),
),
],
)
]),
)),
);
}
}

lottie_4

Vue中使用