java - 向前倾斜还是向后倾斜?类型旋转 vector

标签 java android rotation android-sensors pitch

我正在尝试从 Sensor.TYPE_ROTATION_VECTOR 获取音高,但我不知道如何解释结果。

无论手机向前还是向后倾斜,俯仰值似乎都是相同的(向前=手机顶部远离用户面部,手机底部靠近用户面部)。

图像中的线条代表从侧面观察并倾斜到不同角度的手机。顶部/中间手机垂直于地面。

我需要一个类似于方位角的值(从 -180 到 +180 并且不重复)。

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
  if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    SensorManager.getRotationMatrixFromVector(rMat, sensorEvent.values);
    SensorManager.getOrientation(rMat, orientation);
    pitch = (int) (Math.toDegrees(orientation[1]) ) ;

enter image description here

最佳答案

我最终添加了另一个 TYPE_ACCELEROMETER 传感器监听器。 我检查 value[2] 是否为正值,并根据结果计算音高为 0 到 360 之间的数字(从 12 点钟开始顺时针方向)。 它工作正常,但它会跳过 355-5 和 175-185 之间的一些数字。 如果您有更好的解决方案,请告诉我。

来自 Android 文档:

values[2] : angular speed (w/o drift compensation) around the Z axis in rad/s

boolean isPositive = true;

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
  if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    isPositive = sensorEvent.values[2] > 0;
  }

  if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    SensorManager.getRotationMatrixFromVector(rMat, sensorEvent.values);
    SensorManager.getOrientation(rMat, orientation);
    pitch = (int) (Math.toDegrees(orientation[1]) ) ;

    if(isPositive) //between 6 and 12 o'clock
      pitch = 270 - pitch;
    else //between 12 and 6 o'clock
      pitch = 90 + pitch;
  //...

关于java - 向前倾斜还是向后倾斜?类型旋转 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60354728/

相关文章:

java - Android webview读取cookies

java - LinkedList 的 add 方法(带有父类(super class)型)不是一切都好吗?

java - 为什么我的 recyclerview 适配器不起作用?/为什么该类没有运行?

c++ - 计算多边形在网格中旋转后所占的位置

java - Eclipse - 导入了一个项目,Eclipse 给我错误的@override 方法注释错误

java - CardLayout不切换卡片

iphone - 旋转 iPhone 并改变 View

android - 在代码中旋转一个按钮(或里面的文字)

android - 任何 android Studio 版本 > 3.2.1 显示红色资源但仍可编译

java - 识别当前 View