了解 Flutter 的调度程序在动画时间轴的上下文中绑定



我正在努力理解下面代码中使用SchedulerBinding.instance.scheduleFrameCallback(beginFrame);的部分。beginFrame列在下面的另一个代码块中。

代码来自这里,这是Flutter的动画时间线。显然,我不希望任何人读到这一切。但给定一些上下文,你能理解它被用来做什么吗?

上下文:这部分代码位于一个名为setViewport的函数内部。时间线的视口只是该时间线的可见部分。因此,一旦设置了视口(给出了时间线中的startend点(,它就结束了对时间线中某些内容的动画设置。你可以看到,在做它的过程中,它调用SchedulerBinding.instance.scheduleFrameCallback,这就是我想知道的用途。很明显,我进入了SchedulerBinding的页面,但解释太笼统了,我不知道它是用来做什么的。

if (!animate) {
_renderStart = start;
_renderEnd = end;
advance(0.0, false);
if (onNeedPaint != null) {
onNeedPaint();
}
} else if (!_isFrameScheduled) {
_isFrameScheduled = true;
_lastFrameTime = 0.0;
SchedulerBinding.instance.scheduleFrameCallback(beginFrame);
}

开始帧:

/// Make sure that all the visible assets are being rendered and advanced
/// according to the current state of the timeline.
void beginFrame(Duration timeStamp) {
_isFrameScheduled = false;
final double t =
timeStamp.inMicroseconds / Duration.microsecondsPerMillisecond / 1000.0;
if (_lastFrameTime == 0.0) {
_lastFrameTime = t;
_isFrameScheduled = true;
SchedulerBinding.instance.scheduleFrameCallback(beginFrame);
return;
}
double elapsed = t - _lastFrameTime;
_lastFrameTime = t;
if (!advance(elapsed, true) && !_isFrameScheduled) {
_isFrameScheduled = true;
SchedulerBinding.instance.scheduleFrameCallback(beginFrame);
}
if (onNeedPaint != null) {
onNeedPaint();
}
}

根据项目自述,它用于保持Flare动画的同步:

"为了使动画正确再现,还需要在每帧调用当前FlutterActor的advance(perated(。此外,当前ActorAnimation要求调用函数apply(time(以显示其正确的插值。这一切都是依靠Flutter的SchedulerBinding.scheduleFrameCallback((实现的。">

相关内容

最新更新