浏览代码

Merge pull request #189 from Unity-Technologies/siyaoH/1.17/lottie

Siyao h/1.17/lottie
/main
GitHub 3 年前
当前提交
34b0ec4e
共有 4 个文件被更改,包括 47 次插入214 次删除
  1. 13
      Samples/UIWidgetsSamples_2019_4/Assets/Script/ImageTest.cs
  2. 113
      com.unity.uiwidgets/Runtime/widgets/LottiePainter.cs
  3. 132
      com.unity.uiwidgets/Runtime/rendering/lottie.cs
  4. 3
      com.unity.uiwidgets/Runtime/rendering/lottie.cs.meta

13
Samples/UIWidgetsSamples_2019_4/Assets/Script/ImageTest.cs


class ExampleState : State<ExampleApp>
{
private float frame = 0;
public override Widget build(BuildContext context)
{
return new Container(

AnimatedLottie.file("wine.json", frame: frame, curve: Curves.linear),
new Lottie("wine.json", size: new Size(100, 100)),
new Container(
width: 100,
height: 100,

child: Image.network(
"https://unity-cn-cms-prd-1254078910.cos.ap-shanghai.myqcloud.com/assetstore-cms-media/img-7dfe215f-0075-4f9c-9b5a-be5ee88b866b",
gaplessPlayback: true)
),
new GestureDetector(
onTap: () => { setState(() => { frame += 1; }); },
child: new Container(
color: Color.black,
padding: EdgeInsets.symmetric(20, 20),
child: new Text("Click Me",
style: new TextStyle(fontWeight: FontWeight.w700))
)
)
}
)

113
com.unity.uiwidgets/Runtime/widgets/LottiePainter.cs


using System;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.async;
using Unity.UIWidgets.rendering;
using Canvas = Unity.UIWidgets.ui.Canvas;
public int _round = 0;
public float _duration = 0;
public Size _size = null;
public Lottie(string path, float frame) {
public Lottie(string path, float frame = 0, Size size = null, int round = -1) {
_frame = frame;
_duration = _skottie.duration();
_round = round;
_frame = frame * _duration;
_size = size;
return new LottieState();
return new LottieState(_frame, _round);
public override Widget build(BuildContext context) {
return new LottieRenderObjectWidget(widget._skottie, widget._frame);
}
}
public class LottieRenderObjectWidget : LeafRenderObjectWidget {
Skottie _anime;
float _frame;
float _duration;
float _frame = 0;
int _round = 0;
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
base.updateRenderObject(context, renderObject);
var a = (RenderLottie) renderObject;
a.frame = _frame*_duration;
}
public LottieRenderObjectWidget(Skottie anime, float frame) {
_anime = anime;
public LottieState(float frame, int round) {
_duration = anime.duration();
_round = round;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderLottie(_anime, 100, 100, frame: _frame);
public override Widget build(BuildContext context) {
if (_round != 0) {
WidgetsBinding.instance.addPostFrameCallback((_) => {
setState(() => {
_frame += Time.deltaTime;
if (_frame > widget._duration) {
_frame -= widget._duration;
if (_round > 0) {
_round--;
}
}
});
});
}
return new CustomPaint(
size: widget._size,
painter: new LottiePainter(
skottie: widget._skottie,
frame: _frame
)
);
public class AnimatedLottie : ImplicitlyAnimatedWidget {
public class LottiePainter : AbstractCustomPainter {
public float _frame = 0;
public AnimatedLottie(
string path,
Key key = null,
Curve curve = null,
TimeSpan? duration = null,
float frame = 0
) :base(key: key, curve: curve, duration: duration){
_skottie = new Skottie(Path.Combine(Application.streamingAssetsPath, path));
_frame = frame;
}
float _frame = 0;
AnimatedLottie(
Skottie skottie,
Key key = null,
Curve curve = null,
TimeSpan? duration = null,
float frame = 0
) :base(key: key, curve: curve, duration: duration){
public LottiePainter(Skottie skottie, float frame) {
public static AnimatedLottie file(string path, Key key = null, Curve curve = null, TimeSpan? duration = null,
float frame = 0) {
var skottie = new Skottie(Path.Combine(Application.streamingAssetsPath, path));
duration = duration ?? TimeSpan.FromSeconds(skottie.duration());
return new AnimatedLottie(skottie, key, curve, duration, frame);
}
public override State createState() {
return new _AnimatedLottieState();
}
}
class _AnimatedLottieState : AnimatedWidgetBaseState<AnimatedLottie> {
FloatTween frame;
protected override void forEachTween(TweenVisitor visitor) {
frame = (FloatTween) visitor.visit(this, frame, widget._frame,
(value) => new FloatTween(begin: value, value));
public override void paint(Canvas canvas, Size size) {
_skottie.paint(canvas, Offset.zero, size.width, size.height, _frame);
public override Widget build(BuildContext context) {
return new LottieRenderObjectWidget(widget._skottie, frame.lerp(animation.value));
public override bool shouldRepaint(CustomPainter oldDelegate) {
if (oldDelegate is LottiePainter oldLottiePainter) {
return _frame != oldLottiePainter._frame;
}
return true;
}
}
}

132
com.unity.uiwidgets/Runtime/rendering/lottie.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.rendering {
public class RenderLottie : RenderBox {
public RenderLottie(
Skottie skottie,
float? width = null,
float? height = null,
float scale = 1.0f,
float frame = 0
) {
_width = width;
_height = height;
_scale = scale;
_skottie = skottie;
_frame = frame;
_duration = skottie.duration();
}
Skottie _skottie;
float _frame = 0;
float _duration = 0;
public float frame {
get { return _frame; }
set {
while (value > _duration) {
value -= _duration;
}
if (value == _frame) {
return;
}
_frame = value;
markNeedsLayout();
}
}
float? _width;
public float? width {
get { return _width; }
set {
if (value == _width) {
return;
}
_width = value;
markNeedsLayout();
}
}
float? _height;
public float? height {
get { return _height; }
set {
if (value == _height) {
return;
}
_height = value;
markNeedsLayout();
}
}
float _scale;
public float scale {
get { return _scale; }
set {
if (value == _scale) {
return;
}
_scale = value;
markNeedsLayout();
}
}
Size _sizeForConstraints(BoxConstraints constraints) {
constraints = BoxConstraints.tightFor(
_width,
_height
).enforce(constraints);
return constraints.smallest;
}
protected internal override float computeMinIntrinsicWidth(float height) {
D.assert(height >= 0.0);
if (_width == null && _height == null) {
return 0.0f;
}
return _sizeForConstraints(BoxConstraints.tightForFinite(height: height)).width;
}
protected internal override float computeMaxIntrinsicWidth(float height) {
D.assert(height >= 0.0);
return _sizeForConstraints(BoxConstraints.tightForFinite(height: height)).width;
}
protected internal override float computeMinIntrinsicHeight(float width) {
D.assert(width >= 0.0);
if (_width == null && _height == null) {
return 0.0f;
}
return _sizeForConstraints(BoxConstraints.tightForFinite(width: width)).height;
}
protected internal override float computeMaxIntrinsicHeight(float width) {
D.assert(width >= 0.0);
return _sizeForConstraints(BoxConstraints.tightForFinite(width: width)).height;
}
protected override bool hitTestSelf(Offset position) {
return true;
}
protected override void performLayout() {
size = _sizeForConstraints(constraints);
}
public override void paint(PaintingContext context, Offset offset) {
_skottie.paint(context.canvas, offset, _width ?? 0, _height ?? 0, _frame);
}
}
}

3
com.unity.uiwidgets/Runtime/rendering/lottie.cs.meta


fileFormatVersion: 2
guid: d638e264913843d89da562fca3c33484
timeCreated: 1605844378
正在加载...
取消
保存