浏览代码

update lottie

/main
siyao 4 年前
当前提交
7702acd0
共有 3 个文件被更改,包括 44 次插入202 次删除
  1. 111
      com.unity.uiwidgets/Runtime/widgets/LottiePainter.cs
  2. 132
      com.unity.uiwidgets/Runtime/rendering/lottie.cs
  3. 3
      com.unity.uiwidgets/Runtime/rendering/lottie.cs.meta

111
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 Lottie(string path, float frame) {
public Lottie(string path, float frame = 0, int round = -1) {
_frame = frame;
_duration = _skottie.duration();
_round = round;
_frame = frame * _duration;
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;
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
base.updateRenderObject(context, renderObject);
var a = (RenderLottie) renderObject;
a.frame = _frame*_duration;
}
float _frame = 0;
int _round = 0;
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 += 0.01f;
if (_frame > widget._duration) {
_frame = 0;
if (_round > 0) {
_round--;
}
}
});
});
}
return new CustomPaint(
size: Size.infinite,
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 void paint(Canvas canvas, Size size) {
_skottie.paint(canvas, Offset.zero, size.width, size.height, _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 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
正在加载...
取消
保存