【Android筆記 七】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://blog.csdn.net/octobershiner/article/details/6639040

只要把其中的兩個Sensor。TYPE_ACCELEROMETER改成 Sensor.TYPE_ORIENTATIO就好了,但是今天分享的方法大家最好掌握,這應該是未來android的標準。


   Sensor感應器應該就先暫時介紹到這裏吧,該看一下進程線程的東西了,其實hardware包中還有個非常重要的類,Camera攝像頭,相信大家也聽過android掃描器,很強大。以後有時間和大家分享吧。

   接下來的安排 應該是 線程 activity然後是geocode

   話說我也沒有個指導老師,一個人對着SDK研究這些,有些累阿~求高人指點。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章