Flutter AnimationController回调原理,2024年最新flutter文字识别

///

/// * Transient callbacks, triggered by the system’s [Window.onBeginFrame]

/// callback, for synchronizing the application’s behavior to the system’s

/// display. For example, [Ticker]s and [AnimationController]s trigger from

/// these.

///

/// * Persistent callbacks, triggered by the system’s [Window.onDrawFrame]

/// callback, for updating the system’s display after transient callbacks have

/// executed. For example, the rendering layer uses this to drive its

/// rendering pipeline.

///

/// * Post-frame callbacks, which are run after persistent callbacks, just

/// before returning from the [Window.onDrawFrame] callback.

///

/// * Non-rendering tasks, to be run between frames. These are given a

/// priority and are executed in priority order according to a

/// [schedulingStrategy].

调度运行时的这些回调

  • Transient callbacks,由系统的[Window.onBeginFrame]回调,用于同步应用程序的行为到系统的展示。例如,[Ticker]s和[AnimationController]s触发器来自与它。

  • Persistent callbacks 由系统的[Window.onDrawFrame]方法触发回调,用于在TransientCallback执行后更新系统的展示。例如,渲染层使用他来驱动渲染管道进行build,layout,paint

  • _Post-frame callbacks_在下一帧绘制前回调,主要做一些清理和准备工作

  • Non-rendering tasks 非渲染的任务,在帧构造之间,他们具有优先级,通过[schedulingStrategy]的优先级进行执行,例如用户的输入

**总结:**FrameCallback:SchedulerBinding 类中有三个FrameCallback回调队列, 在一次绘制过程中,这三个回调队列会放在不同时机被执行:

  1. transientCallbacks:用于存放一些临时回调,一般存放动画回调。可以通过SchedulerBinding.instance.scheduleFrameCallback 添加回调。

  2. persistentCallbacks:用于存放一些持久的回调,不能在此类回调中再请求新的绘制帧,持久回调一经注册则不能移除。SchedulerBinding.instance.addPersitentFrameCallback(),这个回调中处理了布局与绘制工作。

  3. postFrameCallbacks:在Frame结束时只会被调用一次,调用后会被系统移除,可由 SchedulerBinding.instance.addPostFrameCallback() 注册,注意,不要在此类回调中再触发新的Frame,这可以会导致循环刷新。

/// Schedules the given transient frame callback.

///

/// Adds the given callback to the list of frame callbacks and ensures that a

/// frame is scheduled.

///

/// If this is a one-off registration, ignore the rescheduling argument.

///

/// If this is a callback that will be re-registered each time it fires, then

/// when you re-register the callback, set the rescheduling argument to

/// true. This has no effect in release builds, but in debug builds, it

/// ensures that the stack trace that is stored for this callback is the

/// original stack trace for when the callback was first registered, rather

/// than the stack trace for when the callback is re-registered. This makes it

/// easier to track down the original reason that a particular callback was

/// called. If rescheduling is true, the call must be in the context of a

/// frame callback.

///

/// Callbacks registered with this method can be canceled using

/// [cancelFrameCallbackWithId].

int scheduleFrameCallback(FrameCallback callback, { bool rescheduling = false }) {

scheduleFrame();

_nextFrameCallbackId += 1;

_transientCallbacks[_nextFrameCallbackId] = _FrameCallbackEntry(callback, rescheduling: rescheduling);

return _nextFrameCallbackId;

}

调度 transient frame callback队列

添加一个callBack,确保fram绘制之前能够回调到他

void scheduleFrame() {

if (_hasScheduledFrame || !_framesEnabled)

return;

assert(() {

if (debugPrintScheduleFrameStacks)

debugPrintStack(label: ‘scheduleFrame() called. Current phase is $schedulerPhase.’);

return true;

}());

ensureFrameCallbacksRegistered();

window.scheduleFrame();

_hasScheduledFrame = true;

}

@protected

void ensureFrameCallbacksRegistered() {

window.onBeginFrame ??= _handleBeginFrame;

window.onDrawFrame ??= _handleDrawFrame;

}

赋给了window.onBeginFram方法

/// Called by the engine to prepare the framework to produce a new frame.

///

/// This function calls all the transient frame callbacks registered by

/// [scheduleFrameCallback]. It then returns, any scheduled microtasks are run

/// (e.g. handlers for any [Future]s resolved by transient frame callbacks),

/// and [handleDrawFrame] is called to continue the frame.

///

/// If the given time stamp is null, the time stamp from the last frame is

/// reused.

///

/// To have a banner shown at the start of every frame in debug mode, set

/// [debugPrintBeginFrameBanner] to true. The banner will be printed to the

/// console using [debugPrint] and will contain the frame number (which

/// increments by one for each frame), and the time stamp of the frame. If the

/// given time stamp was null, then the string “warm-up frame” is shown

/// instead of the time stamp. This allows frames eagerly pushed by the

/// framework to be distinguished from those requested by the engine in

/// response to the “Vsync” signal from the operating system.

///

/// You can also show a banner at the end of every frame by setting

/// [debugPrintEndFrameBanner] to true. This allows you to distinguish log

/// statements printed during a frame from those printed between frames (e.g.

/// in response to events or timers).

void handleBeginFrame(Duration rawTimeStamp) {

Timeline.startSync(‘Frame’, arguments: timelineWhitelistArguments);

_firstRawTimeStampInEpoch ??= rawTimeStamp;

_currentFrameTimeStamp = _adjustForEpoch(rawTimeStamp ?? _lastRawTimeStamp);

if (rawTimeStamp != null)

_lastRawTimeStamp = rawTimeStamp;

_hasScheduledFrame = false;

try {

// TRANSIENT FRAME CALLBACKS

Timeline.startSync(‘Animate’, arguments: timelineWhitelistArguments);

_schedulerPhase = SchedulerPhase.transientCallbacks;

final Map<int, _FrameCallbackEntry> callbacks = _transientCallbacks;

//调用callBack******

_transientCallbacks = <int, _FrameCallbackEntry>{};

callbacks.forEach((int id, _FrameCallbackEntry callbackEntry) {

if (!_removedIds.contains(id))

_invokeFrameCallback(callbackEntry.callback, _currentFrameTimeStamp, callbackEntry.debugStack);

//**************************callback(timeStamp);

});

_removedIds.clear();

} finally {

_schedulerPhase = SchedulerPhase.midFrameMicrotasks;

}

}

  • 当框架准备构建一帧的时候由engine调用,可以猜想endgine调用的实际上是window的onBegainFrame

  • 它会回调[scheduleFrameCallback]中的所有方法(包括一些future的方法),当执行完成后,系统会调用[handleDrawFrame]

/// Signature for frame-related callbacks from the scheduler.

///

/// The timeStamp is the number of milliseconds since the beginning of the

/// scheduler’s epoch. Use timeStamp to determine how far to advance animation

/// timelines so that all the animations in the system are synchronized to a

/// common time base.

typedef FrameCallback = void Function(Duration timeStamp);

callback(timeStamp);

  • “时间戳”是自scheduler开始以来的毫秒数。使用时间戳来确定动画时间线要前进多远,以便系统中的所有动画都同步到一通用的时间线上。

**总结:**当每次系统绘制的之前,会回调到ui.windows的onBegainFrame,而这个onBegainFrame执行的是handleBeginFrame,将时间值回调给了每一个callback。这里注意的是,是在在绘制之前,因为我们一般在绘制之前去通过改变控件的属性值完成动画,而这个动作必须在绘制前完成。

Ticker

反向搜索谁调用了scheduleFrameCallback,发现是在Ticker中的scheduleTick,而scheduleTick有几个地方调用后面来看

/// Schedules a tick for the next frame.

///

/// This should only be called if [shouldScheduleTick] is true.

@protected

void scheduleTick({ bool rescheduling = false }) {

assert(!scheduled);

assert(shouldScheduleTick);

_animationId = SchedulerBinding.instance.scheduleFrameCallback(_tick, rescheduling: rescheduling);

}

这里发现ticker传入了一个_tick对象到scheduleFrameCallback中

void _tick(Duration timeStamp) {

assert(isTicking);

assert(scheduled);

_animationId = null;

_startTime ??= timeStamp;

_onTick(timeStamp - _startTime);

// The onTick callback may have scheduled another tick already, for

// example by calling stop then start again.

if (shouldScheduleTick)

scheduleTick(rescheduling: true);

}

final TickerCallback _onTick;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

文末

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

文末

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家

[外链图片转存中…(img-j3lkguw1-1712632515691)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-TPH8ZMb8-1712632515691)]

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值