美文网首页
【Flutter】屏幕刷新频率

【Flutter】屏幕刷新频率

作者: 24c41111e47b | 来源:发表于2024-07-03 17:56 被阅读0次

OC编程中,有一个类CADisplayLink,我们可以借助这个类来实现一些循环检测任务,那么在Flutter中有没有类似的机制呢?


Flutter中可以借用下面两种方式实现类似的功能

Ticker

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
   late Ticker _ticker;

  @override
  void initState() {
  
    _ticker = this.createTicker((elapsed) {
         // 循环调用,退后台停止,到前台自动激活
        // 这里会一直循环调用,实测试60次/s 左右的频率,等同于帧率
        print("_ticker $elapsed");
    });
     _ticker.start();
    super.initState();
  }
}

AnimationController

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {

  late AnimationController _animationController;

  @override
  void initState() {
    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 2));
    _animationController.addListener(() {
    // 循环调用,退后台停止,到前台自动激活
      print("_animationController ${DateTime.now()}");
    });
    _animationController.repeat();
    super.initState();
  }
}

相关文章

网友评论

      本文标题:【Flutter】屏幕刷新频率

      本文链接:https://www.haomeiwen.com/subject/pcticjtx.html