获取旋转并以度数显示 [英] Get rotation and display in degrees

查看:29
本文介绍了获取旋转并以度数显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些非常简单的东西,但我找不到合适的例子来学习.我的唯一目的如下:

I need something very simple, but I could not find a suitable example to learn from. My sole purpose is the following:

当设备平放在桌子上(背面)时,它的 X 和 Y 轴应显示为 0(或接近于 0).当我从顶部(扬声器所在的位置)和底部(麦克风所在的位置)提起它时 - 它应该显示手机倾斜了多少度.数学描述 - 以度为单位显示手机背面和桌子之间的角度,其中一个轴.当我抬起底部(顶部保持放下)时,显示负度数.

As the device is placed flat (on its back) on the desk, it should show 0 (or close to 0) for X and Y axis. When I lift it from the top part (where the speaker is) and the bottom part (where the microphone is) stays put down - it should show me how many degrees is the phone tilted. Mathematically described - show in degrees the angle between the back of the phone and the table, for one of the axises. When I lift the bottom part (and the top part stays put down) then show minus degrees.

另一个轴也是如此 - 围绕手机的长边旋转手机.

The same goes for the other axis - rotating the phone around its long sides.

我尝试使用陀螺仪或加速度计或旋转矢量传感器从不同的示例组装应用程序,但无法正常工作.

I tried assembling an app from different examples, using Gyroscope or Accelerometer or Rotation Vector Sensors, but could not come with something working properly.

谁能给我一个 onSensorChanged 函数的例子(因为所有的工作都在这里进行)并告诉我使用的是哪个传感器,这样我就知道要注册什么?

Can someone give me an example of the onSensorChanged function (as all the work goes on in here) and just tell me which sensor is used, so I know what to register?

推荐答案

网上有一些示例和教程,但要小心.Sensor.TYPE_ORIENTATION 已弃用.您需要通过监听这两个传感器 Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD 来计算旋转.

There are a few examples and tutorials on the web, but be careful. Sensor.TYPE_ORIENTATION became deprecated. You need to calculate rotations by listening to these two sensors Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD.

注册接收来自这些传感器的通知后的棘手部分是弄清楚如何处理从它们接收到的数据.关键部分如下:

The tricky part after registering to receive notifications from these sensors, is to figure out how to handle the data received from them. The key part is the following:

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
       mGravity = event.values;

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
       mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) {
       float R[] = new float[9];
       float I[] = new float[9];

       boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
       if (success) {
          float orientation[] = new float[3];
          SensorManager.getOrientation(R, orientation);
          azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll
          pitch = orientation[1];
          roll = orientation[2];
       }
    }
}

这是您应该如何在 onSensorChanged(SensorEvent event) 回调中计算设备的方位角、俯仰角、滚动值的方法.请记住以上所有三个角度都是弧度,逆时针方向为正".您可以使用 Math.toDegrees()

This is how you should be calculating the azimuth, pitch, roll values of your device in the onSensorChanged(SensorEvent event) callback. Keep in mind that "All three angles above are in radians and positive in the counter-clockwise direction". You can simply convert them to degrees with Math.toDegrees()

正如 Louis CAD 在评论中指出的那样,将 I、R 和方向数组的初始化移出 onSensorChanged 回调是一个好主意,因为它被频繁调用.创建然后将它们留在 GC 中对您的应用程序性能不利.为了简单起见,我把它留在了那里.

As Louis CAD pointed out in the comments, it is a good idea to move the initialization of the I, R and orientation arrays out of the onSensorChanged callback, since it is called frequently. Creating and then leaving them behind for the GC is bad for your apps performance. I left it there for the sake of simplicity.

根据您设备的旋转方式,您可能需要重新映射坐标以获得所需的结果.您可以在 android 文档

Based on how your device is rotated you might need to remap the coordinates to get the result you want. You can read more about remapCoordinateSystem and also about getRotationMatrix and getOrientation in the android documentation

示例代码:http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

这篇关于获取旋转并以度数显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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