Code Monkey home page Code Monkey logo

svgaplayer-flutter's Introduction

Archived

本仓库已经停止维护,你仍然继续阅读源码及创建分叉,但本仓库不会继续更新,也不会回答任何 issue。

This repo has stopped maintenance, you can still continue to read the source code and create forks, but this repo will not continue to be updated, nor will it answer any issues.

SVGAPlayer-Flutter

简体中文

支持本项目

如果 SVGA-Flutter 为您提供了便利与帮助,诚恳建议您通过以下方式支持作者、贡献者持续为该项目发电。

  1. 轻点 GitHub Star,让更多人看到该项目。
  2. 通过赞赏码(页面底部可见)的方式鼓励作者继续维护代码。

关注作者另外一个开源项目,MPFlutter,使用 Flutter 开发微信小程序。

Introduce

SVGAPlayer is a light-weight animation renderer. You use tools to export svga file from Adobe Animate CC or Adobe After Effects, and then use SVGAPlayer to render animation on mobile application.

SVGAPlayer-Flutter render animation natively via Flutter CustomPainter, brings you a high-performance, low-cost animation experience.

If wonder more information, go to this website.

  • SVGAPlayer-Flutter supports 2.0 format only.

Usage

Here introduce SVGAPlayer-Flutter usage. Wonder exporting usage? Click here.

Add dependency

dependencies:
  svgaplayer_flutter: ^2.0.0  #latest version

Locate files

SVGAPlayer could load svga file from Flutter local assets directory or remote server. Add file to pubspec.yaml by yourself.

Super simple to use

The simplest way to render an animation is to use SVGASimpleImage component.

class MyWidget extends Widget {

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SVGASimpleImage(
          resUrl: "https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true"),
    );
  }

}

Animation will run repeatedly. If you wondering a stronger animation controls, use code.

Code to use

To control an animation rendering, you need to create a SVGAAnimationController instance just like Flutter regular animation. Assign to SVGAImage, load and decode resource using SVGAParser, and then do things as you want with 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() // Try to use .forward() .reverse()
        .whenComplete(() => this.animationController.videoItem = null);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SVGAImage(this.animationController),
    );
  }
}

Reuse MovieEntity

The MovieEntity will dispose after AnimationController dispose or AnimationController::videoItem reset.

After dispose, the MovieEntity can not reused.

If you eager to reuse the MovieEntity, assign MovieEntity::autorelease to false.

final videoItem = await SVGAParser.shared.decodeFromURL(
        "https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true");
videoItem.autorelease = false;

You need to call MovieEntity::dispose() method when no need to use.

Cache

We will not manage any caches, you need to use dio or other libraries to handle resource caches.

Use SVGAParser.decodeFromBytes method to decode caching data.

Features

Here are many feature samples.

License

996.ICU

Anti 996

感谢或联系作者

svgaplayer-flutter's People

Contributors

errnull avatar gdmzldc avatar hustlion avatar maxlee avatar ponycui avatar yrom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svgaplayer-flutter's Issues

too heavy animation

just tested my first svga file and it's causing the app to almost crash , many frames are lost ( app frames , not just the animation )

recorded on real device ( although recording caused it to be worse but it was pretty much the same even not recording it ) :

mobizen_20191107_151653.zip

and this is the svga file :

first_card.zip

is this the current normal ?

使用svga播放礼物动画,发现内存暴涨的可怕

我们有连送的需求,所以会连续播放多个SVGA动画,观察发现内存暴涨,而且渲染也会卡顿。SVGA是不是再生成的时候就要注意运行内存,然后如果降低运行内存,但是多个同时运行还是会出现内存暴涨的情况。这时候低性能手机就会出现OOM的崩溃。请问有什么思路或者方法可以尽量避免吗?再不改动原有逻辑需求的情况下

Android flutter_cache_manager error

File file = await DefaultCacheManager().getSingleFile(url);
final videoItem = await SVGAParser.shared.decodeFromAssets(file.path);

iOS success
Android error

快速切换SVGA问题

场景为头像框预览,对应地,设置一个SVGASimpleImage,传入不同的svgaUrl来实现快速切换,
或者模仿SVGASimpleImage的实现方式,通过动态切换解析svga资源来替换controller的videoItem。
目前出现切换失灵的问题,解析之后替换,但svga动画没有按照预期展示

`class SVGAView extends StatefulWidget {
final String svgaUrl;

SVGAView(this.svgaUrl);

@OverRide
_SVGAViewState createState() => _SVGAViewState();
}

class _SVGAViewState extends State with TickerProviderStateMixin {
SVGAAnimationController _controller;

@OverRide
void initState() {
super.initState();
_controller = SVGAAnimationController(vsync: this);
}

@OverRide
Widget build(BuildContext context) {
prepareForPlay();
return SVGAImage(
_controller,
fit: BoxFit.fill,
);
}

void prepareForPlay() async {
MovieEntity entity = await SVGAParser.shared.decodeFromURL(widget.svgaUrl);
if (entity != null) {
print(widget.svgaUrl);
_controller.videoItem = entity;
_controller.repeat();
}
}

@OverRide
void dispose() {
_controller.stop(canceled: true);
_controller.videoItem = null;
_controller.dispose();
super.dispose();
}
}`
将prepareForPlay在initState中调用有类似的问题,所以改在build中调用

在解析clipPath时候报错了

image

数据是这样的
M408.380 2.842e-14 C408.380 2.842e-14 229.818 2.842e-14 229.818 2.842e-14 C215.505 2.842e-14 204.880 9.937 204.880 24 C204.880 24 204.880 256.687 204.880 256.687 C204.880 271 217.505 282 228.642 282 C228.642 282 408.568 282 408.568 282 C421.505 282 432.880 270.062 432.880 258.937 C432.880 258.937 432.880 23 432.880 23 C432.880 11.954 421.880 2.842e-14 408.380 2.842e-14 Z

我也不清楚美术给的图层为什么有个科学计数法
image
因为这个正则的原因,把e给过滤隔开了,导致firstLetter“C”只有两个解析报错

我把正则改成RegExp('([a-df-zA-Z])')解决了问题,但是不确定这样是否真的没问题?

trim path issue!

this simple individually trimming path (ae preview):
Comp 1_1

is rendered like this (containing svga file):
test5.zip

help me out here ❤️
thanks

动态加文本没反应

使用dynamicItem.setText设置动态文本没有任何效果,请问是什么原因?key对应正确。

AE2020 无法正常导出

更新了AE2020后,svga导出时会提示「将不写入渲染日志」「无法转换Unicode字符」

[web] Support flutter web.

We are trying to make SVGAPlayer support flutter web. Please stay turn.

Code https://github.com/yyued/SVGAPlayer-Flutter/tree/web_support

  • Parser
  • Renderer (regular mode)
  • Renderer (skia mode)

Definitions

  • The regular mode is flutter build web
  • The skia mode is flutter build web --dart-define=FLUTTER_WEB_USE_SKIA=true

Regular mode issue

The regular mode draws image as a HtmlElement, which performance is not good. And cannot display bitmap as we need.

We need a way to control the CustomPainter, access the canvas context, draw image via CanvasContext2D drawImage method.

Skia mode

The SVGAPlayer-Flutter plays in skia mode fine.

有一个bug,同一个svga 控件 切换不同的资源路径时,会切换不了

如题,在如下场景,有一个头像装饰列表,里面都是svga,一个头像控件,点击头像装饰列表中的item时,将对应的item中资源加载到头像上,会切换不成功,
原因是在源码 SVGASimpleImage 的initState 中,直接通过 如下代码解析资源:
Future decode;
if (widget.resUrl != null) {
decode = SVGAParser.shared.decodeFromURL(widget.resUrl);
} else if (widget.assetsName != null) {
decode = SVGAParser.shared.decodeFromAssets(widget.assetsName);
} else if (widget.file != null) {
decode = widget.file.readAsBytes().then((bytes) {
return SVGAParser.shared.decodeFromBuffer(bytes);
});
}
decode.then((videoItem) {
if (mounted) {
this.animationController
..videoItem = videoItem
..repeat();
}
});
但是在点击列表切换的时候,这个initState并不会执行,而是会执行到 build 方法中,在 build方法中 初始化SVGAImage 时用的animationController 并没有变,不重新解析的话,animationController.videoItem 并不会改变,最终导致资源路径没有变,

目前的做法是把解析这段代码在提出来,在build中在解析一遍,不知道有没有别的更好的方法

SVGAPainter buildDPath解析科学计数法数值出错

版本:v2.0.0
播放SVGA动画时,出现RangeError (index): Invalid value: Not in inclusive range 0..1: 2错误
image
定位错误出现SVGAPainter buildDPath方法上,结合源码和SVGA数据分析后发现,buildDPath在解析带有科学计数法的数值时会出错,原因出现在下面这行代码中:
image
上面这段正则表达式的作用是解析SVGA DPath的数据,分析可以得出结论,当解析带有科学计数法的数值时,数值后的e会被"|||"截掉。下面是一段带有科学计数法数值的SVGA的DPath数据:
M340.262 2.842e-14 C340.262 2.842e-14 161.700 2.842e-14 161.700 2.842e-14 C147.387 2.842e-14 136.762 9.937 136.762 24 C136.762 24 136.762 256.687 136.762 256.687 C136.762 271 149.387 282 160.524 282 C160.524 282 340.450 282 340.450 282 C353.387 282 364.762 270.062 364.762 258.937 C364.762 258.937 364.762 23 364.762 23 C364.762 11.954 353.762 2.842e-14 340.262 2.842e-14 Z
通过上面的代码解析后,得到下面的内容:
|||M 340.262 2.842|||e -14 |||C 340.262 2.842|||e -14 161.700 2.842|||e -14 161.700 2.842|||e -14 |||C 147.387 2.842|||e -14 136.762 9.937 136.762 24 |||C 136.762 24 136.762 256.687 136.762 256.687 |||C 136.762 271 149.387 282 160.524 282 |||C 160.524 282 340.450 282 340.450 282 |||C 353.387 282 364.762 270.062 364.762 258.937 |||C 364.762 258.937 364.762 23 364.762 23 |||C 364.762 11.954 353.762 2.842|||e -14 340.262 2.842|||e -14 |||Z
`
可以看到带有科学计数法的数值都被截断了,所有没办法正确解析数据了,请官方尽快修复这个问题。

部分动画出不来

painter.dart中的变换会导致部分图片出不来,改为如下可以正常显示
if(frameItem.hasTransform()) { canvas.transform(Float64List.fromList([ frameItem.transform.a, frameItem.transform.b, 0.0, 0.0, frameItem.transform.c, frameItem.transform.d, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, frameItem.transform.tx, frameItem.transform.ty, 0.0, 1.0 ].toList())); }

该组件跟dio4.0.0网络请求组件依赖冲突,能否尽快升级一下该组件依赖库到最新版本呢?

Because no versions of dio match >4.0.0 <5.0.0 and dio 4.0.0 depends on http_parser ^4.0.0, dio ^4.0.0 requires http_parser ^4.0.0.
And because svgaplayer_flutter >=1.2.0 <2.0.0-nullsafety.0 depends on http ^0.12.2 which depends on http_parser >=0.0.1 <4.0.0, dio ^4.0.0 is incompatible with svgaplayer_flutter >=1.2.0 <2.0.0-nullsafety.0.
So, because creamchat depends on both svgaplayer_flutter ^1.2.0 and dio ^4.0.0, version solving failed.
pub get failed (1; So, because creamchat depends on both svgaplayer_flutter ^1.2.0 and dio ^4.0.0, version solving failed.)

能不能升级一下protobuf的版本到最新版本

Because every version of login_plugin from path depends on protoc_plugin ^19.0.1 and no versions of protoc_plugin match >19.0.1 <20.0.0, every version of frog_plugin from path requires protoc_plugin 19.0.1.
And because protoc_plugin 19.0.1 depends on protobuf >=0.14.4 <2.0.0 and svgaplayer_flutter <0.1.1 depends on protobuf ^0.13.4, frog_plugin from path is incompatible with svgaplayer_flutter <0.1.1.

Empty path 的问题

目前的svga通过PE导出的时候会有一段没有path的形状。

type: ELLIPSE
ellipse: {
}
styles: {
  stroke: {
    r: 0.19607843458652496
    g: 0.6666666865348816
    b: 1.0
    a: 1.0
  }

} else if (shape.type == ShapeEntity_ShapeType.ELLIPSE) {
final args = shape.ellipse;
final xv = args.x ?? 0.0;
final yv = args.y ?? 0.0;
final rxv = args.radiusX ?? 0.0;
final ryv = args.radiusY ?? 0.0;
path.addOval(Rect.fromLTWH(xv - rxv, yv - ryv, rxv * 2, ryv * 2));

在这一段代码中,他会画成一个方块

image

attach file:
shape.svga.zip

I cannot play the animation in the Flutter Web

errors.dart:147 Uncaught (in promise) Error: Unsupported operation: newZLibInflateFilter
at Object.throw
[as throw] (errors.dart:196)
at Function.makeZLibInflateFilter (io_patch.dart:612)
at new io.ZLibDecoderSink.
(data_transformer.dart:586)
at io.ZLibDecoder.new.startChunkedConversion (data_transformer.dart:481)
at io.ZLibDecoder.new.convert (data_transformer.dart:466)
at io.ZLibCodec.new.decode (codec.dart:28)
at parser.SVGAParser.new.decodeFromBuffer (parser.dart:22)
at decodeFromBuffer.next ()
at runBody (async_patch.dart:86)
at Object._async [as async] (async_patch.dart:125)
at parser.SVGAParser.new.decodeFromBuffer (parser.dart:21)
at parser.SVGAParser.new.decodeFromAssets (parser.dart:18)
at decodeFromAssets.next ()
at onValue (async_patch.dart:47)
at _RootZone.runUnary (zone.dart:1381)
at _FutureListener.thenAwait.handleValue (future_impl.dart:139)
at handleValueCallback (future_impl.dart:680)
at Function._propagateToListeners (future_impl.dart:709)
at _Future.new.[_completeWithValue] (future_impl.dart:524)
at async._AsyncCallbackEntry.new.callback (future_impl.dart:554)
at Object._microtaskLoop (schedule_microtask.dart:43)
at _startMicrotaskLoop (schedule_microtask.dart:52)
at async_patch.dart:168

Unable to load asset

tried implementing the exported svga file like this :

SVGASimpleImage(assetsName: "assets/svga/first_card.svga")

animation didn't load and got this :

E/flutter (12135): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Unable to load asset: assets/svga/first_card.svga
E/flutter (12135): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:230:7)
E/flutter (12135): <asynchronous suspension>
E/flutter (12135): #1 SVGAParser.decodeFromAssets (package:svgaplayer_flutter/parser.dart:18:52) E/flutter (12135): <asynchronous suspension>
E/flutter (12135): #2 new _SVGASimpleImageState (package:svgaplayer_flutter/simple_player.dart:28:25)
E/flutter (12135): #3 SVGASimpleImage.createState (package:svgaplayer_flutter/simple_player.dart:11:12)
E/flutter (12135): #4 new StatefulElement (package:flutter/src/widgets/framework.dart:4041:25)
E/flutter (12135): #5 StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:802:38)
E/flutter (12135): #6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3104:40)
E/flutter (12135): #7 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #8 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5165:14)
E/flutter (12135): #9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #11 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5165:14)
E/flutter (12135): #12 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3978:16)
E/flutter (12135): #15 Element.rebuild (package:flutter/src/widgets/framework.dart:3755:5)
E/flutter (12135): #16 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3941:5)
E/flutter (12135): #17 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3936:5)
E/flutter (12135): #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #20 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5165:14)
E/flutter (12135): #21 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3978:16)
E/flutter (12135): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:3755:5)
E/flutter (12135): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3941:5)
E/flutter (12135): #26 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4111:11)
E/flutter (12135): #27 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3936:5)
E/flutter (12135): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #30 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3978:16)
E/flutter (12135): #31 Element.rebuild (package:flutter/src/widgets/framework.dart:3755:5)
E/flutter (12135): #32 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3941:5)
E/flutter (12135): #33 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3936:5)
E/flutter (12135): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3109:14)
E/flutter (12135): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:2903:12)
E/flutter (12135): #36 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5165:14)

didn't find anything else about how to properly implement everything
did i miss sth ?

protobuf version conflict

My project is using protobuf ^1.0.0 which is higher than your version. When I rollback to ^0.13.4, tons of error happened in proto generated dart file. I have checked the release message of dart protobuf and the 1.0.0 is released in the same month with 0.13, so I think you should update the protobuf version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.