微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何在颤动中播放 Rive 动画

如何解决我如何在颤动中播放 Rive 动画

我是 Rive 的新手,因为 Rive 2 几乎没有好的文档,我想在这里问一下。如何在 Flutter 中播放我的 Rive 动画?我复制并粘贴了 pub.dev 上的示例以供依赖,并将它们的动画名称切换为我的,但它只是向我显示了我的动画的奇怪冻结帧。这是代码

import 'package:Flutter/material.dart';
import 'package:Flutter/services.dart';
import 'package:rive/rive.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),);
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _togglePlay() {
    setState(() => _controller.isActive = !_controller.isActive);
  }

  /// Tracks if the animation is playing by whether controller is running.
  bool get isPlaying => _controller?.isActive ?? false;

  Artboard _riveArtboard;
  RiveAnimationController _controller;
  @override
  void initState() {
    super.initState();

    // Load the animation file from the bundle,note that you Could also
    // download this. The RiveFile just expects a list of bytes.
    rootBundle.load('assets/file.riv').then(
      (data) async {
        // Load the RiveFile from the binary data.
        final file = RiveFile.import(data);
        // The artboard is the root of the animation and gets drawn in the
        // Rive widget.
        final artboard = file.mainArtboard;
        // Add a controller to play back a kNown animation on the main/default
        // artboard.We store a reference to it so we can toggle playback.
        artboard.addController(_controller = SimpleAnimation('idle'));
        setState(() => _riveArtboard = artboard);
      },);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _riveArtboard == null
            ? const SizedBox()
            : Rive(artboard: _riveArtboard),),floatingActionButton: FloatingActionButton(
        onpressed: _togglePlay,tooltip: isPlaying ? 'Pause' : 'Play',child: Icon(
          isPlaying ? Icons.pause : Icons.play_arrow,);
  }
}

解决方法

您可能需要更改要从 Rive 文件播放的动画的名称。

这一行:

artboard.addController(_controller = SimpleAnimation('idle'));

尝试播放名为“空闲”的动画。如果您的动画名称不同,请尝试在此处替换名称。

blog post 包含有关将 Rive 与 Flutter 结合使用的更多信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?