Android:计算设备方向的问题 [英] Android: Problems calculating the Orientation of the Device

查看:28
本文介绍了Android:计算设备方向的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的增强现实应用,因此我开始使用传感器数据.

i'am trying to build a simple Augmented Reality App, so I start working with sensor Data.

根据此线程(Android 指南针示例)和示例(http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html),使用 Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD 计算方向并不合适.

According to this thread (Android compass example) and example (http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html), the calculation of the orientation using the Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD doesn't really fit.

所以我无法获得好的"值.方位角值根本没有任何意义,所以如果我只是将手机向上移动,值会发生极大的变化.即使我只是旋转手机,这些值也不代表手机的方向.

So I'm not able to get "good" values. The azimut values doesn't make any sense at all, so if I just move the Phone upside the value changes extremly. Even if I just rotate the phone, the values doesn't represent the phones orientation.

有没有人有想法,根据给定的例子,谁来提高价值质量?

Has anybody an idea, who to improve the values quality according to the given example?

推荐答案

你在什么样的方向使用这个示例应用程序?从编写的代码来看,唯一支持的方向是桌子上的纵向或平面,这取决于设备.你说的好"是什么意思?

In what kind of orientation do you use this sample app? From what is written is this code, the only orientation supported is Portrait or flat on the table, it depends on devices. What do you mean by "good"?

旋转设备时,该值不是好"是正常的,设备坐标系应该在纵向或平面工作我不知道(沿屏幕垂直的Y轴朝上,Z轴从屏幕中心指向屏幕外,X 轴垂直于 Y 轴,沿屏幕向右移动).有了这个,旋转设备不会旋转设备坐标系,你必须重新映射它.

It is normal that the value is not "good" when rotating the device, the device coordinate system is supposed to be working in Portrait, or flat i don't know (Y axis vertical along the screen pointing up, Z axis pointing out of the screen coming from the center of screen, X axis perpendicular to the Y axis going on the right along the screen). Having this, rotating the device will not rotate the device coordinate system, you'll have to remap it.

但是如果你想要纵向方向的设备标题,这里有一段对我有用的代码:

But if you want the heading of the device in Portrait orientation, here is a piece of code that works good for me:

@Override
public void onSensorChanged(SensorEvent event)
{
    // It is good practice to check that we received the proper sensor event
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
    {
        // Convert the rotation-vector to a 4x4 matrix.
        SensorManager.getRotationMatrixFromVector(mRotationMatrix,
                event.values);
        SensorManager
                .remapCoordinateSystem(mRotationMatrix,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, orientationVals);

        // Optionally convert the result from radians to degrees
        orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
        orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
        orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

        tv.setText(" Yaw: " + orientationVals[0] + "
 Pitch: "
                + orientationVals[1] + "
 Roll (not used): "
                + orientationVals[2]);

    }
}

您将在以下位置获得航向(或方位角):

You'll get the heading (or azimuth) in:

orientationVals[0]

这篇关于Android:计算设备方向的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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