在Flutter中获取设备方向(偏航,横摇和俯仰) [英] Getting device orientation (yaw, roll and pitch) in Flutter

查看:525
本文介绍了在Flutter中获取设备方向(偏航,横摇和俯仰)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,我可以使用GAME_ROTATION_VECTOR传感器获取设备的偏航,横摇和俯仰.

In Android I can get the device yaw, roll and pitch using a GAME_ROTATION_VECTOR sensor.

我需要在Flutter中做同样的事情,但是除了 sensors 包,该包只能访问加速度计&陀螺仪传感器.

I need to do the same thing in Flutter, but I haven't been able to find anything but the sensors package, which only gives access to accelerometer & gyroscope sensors.

我该怎么办?我需要自己从加速度计和陀螺仪计算方位吗?

What can I do? Do I need to calculate the orientation myself from the accelerometer and gyro?

推荐答案

由于问题尚未解决,所以不知道这是否仍然有用,但是对于那些寻求答案的人……现在有一个 Flutter Sensors软件包,它为您提供了所有的魔术.

Don't know if this is still relevant, as the question wasn't closed, but for those seeking the answer... there is now a Flutter Sensors package which does all the magic for you.

当然,您必须订阅流才能获得加速度计和陀螺仪的当前值.

You'll have to subscribe to the streams to get the current values of the accelerometers and gyroscopes, of course.

例如:

  @override
  void initState() {
    super.initState();
    _streamSubscriptions
        .add(accelerometerEvents.listen((AccelerometerEvent event) {
      setState(() {
        _accelerometerValues = <double>[event.x, event.y, event.z];
      });
    }));
    _streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) {
      setState(() {
        _gyroscopeValues = <double>[event.x, event.y, event.z];
      });
    }));
    _streamSubscriptions
        .add(userAccelerometerEvents.listen((UserAccelerometerEvent event) {
      setState(() {
        _userAccelerometerValues = <double>[event.x, event.y, event.z];
      });
    }));
  }

这篇关于在Flutter中获取设备方向(偏航,横摇和俯仰)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆