本文介绍了如何获得方向的安卓(如北,西)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我是新的Andr​​oid和我想根据我的相机获得的方向。根据我的相机我怎样才能得到方向信息?你能不能给一个想法呢?

I am new in Android and i want to get direction according to my camera. How can i get direction information according to my camera? Could you give an idea for this?

问候。

推荐答案

我们不能使用方向传感器了,我们就可以利用磁场传感器和加速度传感器串联来获得同样的功能。这是更多的工作,但它允许继续使用一个回调来处理方向改变。

TYPE_ORIENTATION is deprecated

We cannot use the Orientation Sensor anymore, we can use the Magnetic Field Sensor and Accelerometer Sensors in tandem to get equivalent functionality. It's more work but it does allow to continue to use a callback to handle orientation changes.

下面是一个指南针样品 http://www.codingforandroid.com/2011/01/using取向的传感器,simple.html

的加速度计和磁场的转换来的 AZIMUT 的:

float[] mGravity;
float[] mGeomagnetic;

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];

        if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {

            // orientation contains azimut, pitch and roll
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            azimut = orientation[0];
        }
    }
}

要指向北方,你可以计算出度的旋转:

To point the north you can calculate a rotation in degrees :

float rotation = -azimut * 360 / (2 * 3.14159f);

这篇关于如何获得方向的安卓(如北,西)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:12