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

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

问题描述

我需要的东西很简单,但我无法找到一个合适的例子可以借鉴。我唯一​​的目的如下:

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 成为德precated。你需要通过听这两个传感器 Sensor.TYPE_ACCELEROMETER Sensor.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);
         azimut = orientation[0]; // orientation contains: azimut, pitch and roll
         pitch = orientation[1];
         roll = orientation[2];
       }
    }
}

这是你应该怎么计算的 onSensorChanged(SensorEvent事件)回调方位角,俯仰,滚转设备的价值。请记住,这三个角度以上是在弧度和积极的反时针方向。你可以简单地将它们转换成度 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()

根据您的设​​备如何旋转,你可能需要重新映射坐标得到你想要的结果。你可以阅读更多关于 remapCoordinateSystem 也关于 getRotationMatrix getOrientation 安卓文档

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

例如code: http://www.codingforandroid.com/2011/01/using取向的传感器,simple.html

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

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