Android Sensor感应器介绍(三)获取用户移动方向,指南针原理

晚上好~今天继续给大家分享一下第二个重要的感应器,其实获取方向本应该很简单的事情,在文章一中看到 有个TYPE_ORIENTATION 关键字,说明可以直接获取设备的移动方向,但是最新版的SDK加上了这么一句话“TYPE_ORIENTATION     This constant is deprecated. use SensorManager.getOrientation() instead. ”也就是说,这种方式已经被取消,要开发者使用SensorManager.getOrientation()来获取原来的数据。

   实际上,android获取方向是通过磁场感应器和加速度感应器共同获得的,至于具体的算法SDK已经封装好了。也就是说现在获取用户方向有两种方式,一是官方推荐的,通过SensorManager.getOrientation()来获取,这个方法表面看似容易(那是因为你还没看到他的参数。。一会再说),但实际上需要用到两个感应器共同完成工作,特点是更加的准确。第二种方法非常简单,就像前一篇文章获取加速度一样,直接得到三个轴上的数据。

   额,从难一些的介绍吧,因为毕竟第一种方法会是android未来的一个选择,第二种不知道什么时候就要成为历史了。

 

android给我们提供的方向数据是一个float型的数组,包含三个方向的值 如图

\

 

 

当你的手机水平放置时,被默认为静置状态,即XY角度均为0

 

values[0]  表示Z轴的角度:方向角,我们平时判断的东西南北就是看这个数据的,经过我的实验,发现了一个有意思的事情,也就是说使用第一种方式获得方向(磁场+加速度)得到的数据范围是(-180~180),也就是说,0表示正北,90表示正东,180/-180表示正南,-90表示正西。而第二种方式(直接通过方向感应器)数据范围是(0~360)360/0表示正北,90表示正东,180表示正南,270表示正西。

values[1]  表示X轴的角度:俯仰角   即由静止状态开始,前后翻转

values[2]  表示Y轴的角度:翻转角  即由静止状态开始,左右翻转

可见统一获取方向的方法是必须的,因为处理这些数据的算法可能针对第一种获取方式,那么当用在第二种方式时,移植性就不好了。

看下面的方法

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public static float[] getOrientation (float[] R, float[] values)

Since: API Level 3

Computes the device's orientation based on the rotation matrix.

When it returns, the array values is filled with the result:

values[0]: azimuth, rotation around the Z axis.

values[1]: pitch, rotation around the X axis.

values[2]: roll, rotation around the Y axis.

The reference coordinate-system used is different from the world coordinate-system defined for the rotation matrix:

X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points West).

Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.

Z points towards the center of the Earth and is perpendicular to the ground.

All three angles above are in radians and positive in the counter-clockwise direction.

通常我们并不需要获取这个函数的返回值,这个方法会根据参数R[]的数据填充values[]而后者就是我们想要的。

那么R表示什么呢?又将怎么获取呢?

R[] 是一个旋转矩阵,用来保存磁场和加速度的数据,大家可以理解未加工的方向数据吧

R通过下面的静态方法获取,这个方法也是用来填充R[]

public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)

 

解释以下参数,第一个就是我们需要填充的R数组,大小是9

                            第二个是是一个转换矩阵,将磁场数据转换进实际的重力坐标中 一般默认情况下可以设置为null

                            第三个是一个大小为3的数组,表示从加速度感应器获取来的数据  在onSensorChanged中

                            第四个是一个大小为3的数组,表示从磁场感应器获取来的数据    在onSensorChanged中

 

好了基本逻辑就是这样的,下面给大家演示一个简单的测试方向的例子,可以时刻监听用户的方向

 

/*

 * @author octobershiner

 * 2011 07 28

 * SE.HIT

 * 一个演示通过磁场和加速度两个感应器获取方向数据的例子

 * */ 

 

 

package uni.sensor; 

 

import android.app.Activity; 

import android.content.Context; 

import android.hardware.Sensor; 

import android.hardware.SensorEvent; 

import android.hardware.SensorEventListener; 

import android.hardware.SensorManager; 

import android.os.Bundle; 

import android.util.Log; 

 

public class OrientationActivity extends Activity{ 

 

    private SensorManager sm; 

    //需要两个Sensor 

    private Sensor aSensor; 

    private Sensor mSensor; 

     

    float[] accelerometerValues = new float[3]; 

    float[] magneticFieldValues = new float[3]; 

     

    private static final String TAG = "sensor"; 

     

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        // TODO Auto-generated method stub 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

 

        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 

        aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

        mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 

 

        sm.registerListener(myListener, aSensor, SensorManager.SENSOR_DELAY_NORMAL); 

        sm.registerListener(myListener, mSensor,SensorManager.SENSOR_DELAY_NORMAL); 

        //更新显示数据的方法 

        calculateOrientation(); 

 

    } 

    //再次强调:注意activity暂停的时候释放 

    public void onPause(){ 

        sm.unregisterListener(myListener); 

        super.onPause(); 

    }    

     

     

    final SensorEventListener myListener = new SensorEventListener() { 

    public void onSensorChanged(SensorEvent sensorEvent) { 

         

    if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) 

    magneticFieldValues = sensorEvent.values; 

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 

        accelerometerValues = sensorEvent.values; 

    calculateOrientation(); 

    } 

    public void onAccuracyChanged(Sensor sensor, int accuracy) {} 

    }; 

 

     

    private  void calculateOrientation() { 

          float[] values = new float[3]; 

          float[] R = new float[9]; 

          SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);          

          SensorManager.getOrientation(R, values); 

 

          // 要经过一次数据格式的转换,转换为度 

          values[0] = (float) Math.toDegrees(values[0]); 

          Log.i(TAG, values[0]+""); 

          //values[1] = (float) Math.toDegrees(values[1]); 

          //values[2] = (float) Math.toDegrees(values[2]); 

           

          if(values[0] >= -5 && values[0] < 5){ 

             Log.i(TAG, "正北"); 

          } 

          else if(values[0] >= 5 && values[0] < 85){ 

              Log.i(TAG, "东北"); 

          } 

          else if(values[0] >= 85 && values[0] <=95){ 

              Log.i(TAG, "正东"); 

          } 

          else if(values[0] >= 95 && values[0] <175){ 

              Log.i(TAG, "东南"); 

          } 

          else if((values[0] >= 175 && values[0] <= 180) || (values[0]) >= -180 && values[0] < -175){ 

              Log.i(TAG, "正南"); 

          } 

          else if(values[0] >= -175 && values[0] <-95){ 

              Log.i(TAG, "西南"); 

          }  

          else if(values[0] >= -95 && values[0] < -85){ 

              Log.i(TAG, "正西"); 

          } 

          else if(values[0] >= -85 && values[0] <-5){ 

              Log.i(TAG, "西北"); 

          } 

        } 

     

 

 

   实训的时间非常紧张,抽时间写总结感觉很累,但是感觉收获很多,如果有时间的话,也想给大家分享第二种方法,和这种比起来简单很多,其实大家可以完全参考上篇文章中的代码:http://www.2cto.com/kf/201111/110232.html

只要把其中的两个Sensor。TYPE_ACCELEROMETER改成Sensor.TYPE_ORIENTATIO就好了,但是今天分享的方法大家最好掌握,这应该是未来android的标准。

 

 

   Sensor感应器应该就先暂时介绍到这里吧,该看一下进程线程的东西了,其实hardware包中还有个非常重要的类,Camera摄像头,相信大家也听过android扫描器,很强大。以后有时间和大家分享吧。

   接下来的安排 应该是 线程activity然后是geocode

   话说我也没有个指导老师,一个人对着SDK研究这些,有些累阿~求高人指点。

猜你喜欢

转载自blog.csdn.net/asde1239/article/details/45721441