Flutter SVG动画播放插件svgaplayer_flutter_plus_3的使用
Flutter SVG动画播放插件svgaplayer_flutter_plus_3的使用
简体中文
支持本项目
如果 SVGA-Flutter 为您提供了便利与帮助,诚恳建议您通过以下方式支持作者、贡献者持续为该项目发电。
- 轻点 GitHub Star,让更多人看到该项目。
- 通过赞赏码(页面底部可见)的方式鼓励作者继续维护代码。
关注作者另外一个开源项目,MPFlutter,使用 Flutter 开发微信小程序。
介绍
SVGAPlayer 是一个轻量级的动画渲染器。您可以使用 工具 从 Adobe Animate CC 或 Adobe After Effects 导出 svga
文件,然后使用 SVGAPlayer 在移动应用程序中渲染动画。
SVGAPlayer-Flutter
使用 Flutter 的 CustomPainter 原生渲染动画,为您提供高性能、低成本的动画体验。
更多信息可以访问 网站。
支持格式
- SVGAPlayer-Flutter 支持 2.0 格式。
使用方法
这里介绍 SVGAPlayer-Flutter
的使用方法。想知道如何导出动画?点击 这里。
添加依赖
在 pubspec.yaml
中添加以下依赖:
dependencies:
svgaplayer_flutter: ^3.0.0 # 最新版本
定位文件
SVGAPlayer 可以从 Flutter 的本地 assets
目录或远程服务器加载 svga
文件。请自行将文件添加到 pubspec.yaml
中。
极简使用
最简单的渲染动画的方法是使用 SVGASimpleImage
组件。
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: SVGASimpleImage(
resUrl: "https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true"),
);
}
}
动画将循环播放。如果您需要更强大的动画控制功能,请使用代码。
使用代码控制动画
要控制动画的渲染,您需要创建一个 SVGAAnimationController
实例,就像 Flutter 的常规动画一样。将其分配给 SVGAImage
,使用 SVGAParser
加载和解码资源,然后使用 SVGAAnimationController
进行操作。
import 'package:flutter/material.dart';
import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
SVGAAnimationController animationController;
@override
void initState() {
this.animationController = SVGAAnimationController(vsync: this);
this.loadAnimation();
super.initState();
}
@override
void dispose() {
this.animationController.dispose();
super.dispose();
}
void loadAnimation() async {
final videoItem = await SVGAParser.shared.decodeFromURL(
"https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true");
this.animationController.videoItem = videoItem;
this
.animationController
.repeat() // 尝试使用 .forward() 或 .reverse()
.whenComplete(() => this.animationController.videoItem = null);
}
@override
Widget build(BuildContext context) {
return Container(
child: SVGAImage(this.animationController),
);
}
}
重用 MovieEntity
MovieEntity
在 AnimationController
被释放或 AnimationController::videoItem
被重置后会自动销毁。
一旦销毁,MovieEntity
就不能被重用。
如果您希望重用 MovieEntity
,可以将 MovieEntity::autorelease
设置为 false
。
final videoItem = await SVGAParser.shared.decodeFromURL(
"https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true");
videoItem.autorelease = false;
更多关于Flutter SVG动画播放插件svgaplayer_flutter_plus_3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter SVG动画播放插件svgaplayer_flutter_plus_3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
svgaplayer_flutter_plus_3
是一个用于在 Flutter 应用中播放 SVG 动画的插件。它基于 SVGA 格式,SVGA 是一种高效的动画格式,特别适合在移动端使用。以下是如何在 Flutter 项目中使用 svgaplayer_flutter_plus_3
插件的步骤。
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 svgaplayer_flutter_plus_3
依赖:
dependencies:
flutter:
sdk: flutter
svgaplayer_flutter_plus_3: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在需要使用 SVGA 动画的 Dart 文件中导入包:
import 'package:svgaplayer_flutter_plus_3/svgaplayer_flutter_plus_3.dart';
3. 使用 SVGA 动画
你可以使用 SVGAImage
或 SVGASimpleImage
来加载和播放 SVGA 动画。
使用 SVGAImage
SVGAImage
提供了更多的控制选项,比如循环次数、动画完成回调等。
class MySVGAAnimation extends StatefulWidget {
@override
_MySVGAAnimationState createState() => _MySVGAAnimationState();
}
class _MySVGAAnimationState extends State<MySVGAAnimation> {
SVGAAnimationController? _controller;
@override
void initState() {
super.initState();
_controller = SVGAAnimationController();
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SVGA Animation'),
),
body: Center(
child: SVGAImage(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
onCompleted: () {
print("Animation completed!");
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller?.start();
},
child: Icon(Icons.play_arrow),
),
);
}
}
使用 SVGASimpleImage
SVGASimpleImage
是一个更简单的组件,适合不需要复杂控制的场景。
class MySimpleSVGAAnimation extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple SVGA Animation'),
),
body: Center(
child: SVGASimpleImage(
"https://example.com/path/to/your/animation.svga",
),
),
);
}
}
4. 控制动画
你可以通过 SVGAAnimationController
来控制动画的播放、暂停、停止等操作。
_controller?.start(); // 开始播放
_controller?.pause(); // 暂停播放
_controller?.stop(); // 停止播放
_controller?.reset(); // 重置动画
5. 处理本地文件
如果你有本地的 SVGA 文件,可以使用 AssetBundle
来加载:
SVGAImage.asset(
"assets/animation.svga",
controller: _controller!,
);
6. 处理网络文件
如果你需要从网络加载 SVGA 文件,可以直接使用 URL:
SVGAImage.network(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
);
7. 处理动画完成事件
你可以通过 onCompleted
回调来处理动画完成事件:
SVGAImage(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
onCompleted: () {
print("Animation completed!");
},
);
8. 设置循环次数
你可以通过 loops
参数来设置动画的循环次数:
SVGAImage(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
loops: 3, // 循环3次
);
9. 设置动画大小
你可以通过 width
和 height
参数来设置动画的大小:
SVGAImage(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
width: 200,
height: 200,
);
10. 处理错误
你可以通过 onError
回调来处理加载或播放错误:
SVGAImage(
"https://example.com/path/to/your/animation.svga",
controller: _controller!,
onError: (error) {
print("Error: $error");
},
);