【问题标题】:How to play lottie animation for specific time duration in flutter如何在颤动中播放特定持续时间的彩票动画
【发布时间】:2021-06-09 09:39:28
【问题描述】:

我想在颤振中播放特定持续时间的彩票动画。我不想通过快进播放它。就像如果我的动画总共包含90 frames 那么如果我只想先播放30 frames 那么我可以做到这一点吗?或者像如果我的动画总共有 2 秒但我只想播放 1 秒。那么如何使用动画控制器或任何其他东西来做到这一点?

【问题讨论】:

    标签: flutter flutter-animation lottie animationcontroller


    【解决方案1】:

    好的,您可以尝试直接编辑控制器,因此您的 initState 将如下所示:

      @override
      void initState() {
        super.initState();
        _controller = AnimationController(vsync: this);
        _controller.addListener(() {
          print(_controller.value);
        //  if the full duration of the animation is 8 secs then 0.5 is 4 secs
          if (_controller.value > 0.5) {
    // When it gets there hold it there.
            _controller.value = 0.5;
          }
        });
      }
    

    然后你的 lottie 小部件看起来像这样:

      Lottie.asset( 
          'asset/path.json', 
           controller: _controller, 
           onLoaded: (comp){
              _controller
              ..duration = comp.duration
              ..forward(); 
        })
    

    -----------下面的上一个答案-----------

    没有机会检查这一点,但尝试将控制器添加到您的动画并在 onLoaded 上使用 animateTo。

     Lottie.asset( 
      'asset/path.json', 
       controller: _controller, 
       onLoaded: (comp){
          _controller.animateTo(frameNumber); 
    })
    

    【讨论】:

    • 感谢您的回答,但我之前已经尝试过,但它没有按预期工作。具体来说,我有一个总共 90 帧的动画,但只有前 30 帧在移动。最后 60 帧是稳定的,这是我不希望在我的应用程序中使用的,因为在动画完成后我也在反转该动画。但由于它有 60 帧稳定,我得到1 second 的滞后。所以我想消除稳定帧的滞后。如果您有其他选择,请分享。在此先感谢:)
    • 你可以尝试在你的控制器中添加一个监听器并在那里暂停它。我将在下面发布一个新答案。
    • 太棒了!!!!非常感谢 :) 它完美运行?
    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2021-07-15
    • 2021-01-17
    • 2019-11-16
    • 2019-04-09
    • 2019-01-28
    相关资源
    最近更新 更多