Android Sensor感应器介绍,获取用户移动方向,指南针原理
轉(zhuǎn)自:http://www.2cto.com/kf/201111/110233.html
晚上好~今天繼續(xù)給大家分享一下第二個(gè)重要的感應(yīng)器,其實(shí)獲取方向本應(yīng)該很簡單的事情,在文章一中看到 有個(gè)TYPE_ORIENTATION 關(guān)鍵字,說明可以直接獲取設(shè)備的移動(dòng)方向,但是最新版的SDK加上了這么一句話“TYPE_ORIENTATION???? This constant is deprecated. use SensorManager.getOrientation() instead. ”也就是說,這種方式已經(jīng)被取消,要開發(fā)者使用SensorManager.getOrientation()來獲取原來的數(shù)據(jù)。
?? 實(shí)際上,android獲取方向是通過磁場(chǎng)感應(yīng)器和加速度感應(yīng)器共同獲得的,至于具體的算法SDK已經(jīng)封裝好了。也就是說現(xiàn)在獲取用戶方向有兩種方式,一是官方推薦的,通過SensorManager.getOrientation()來獲取,這個(gè)方法表面看似容易(那是因?yàn)槟氵€沒看到他的參數(shù)。。一會(huì)再說),但實(shí)際上需要用到兩個(gè)感應(yīng)器共同完成工作,特點(diǎn)是更加的準(zhǔn)確。第二種方法非常簡單,就像前一篇文章獲取加速度一樣,直接得到三個(gè)軸上的數(shù)據(jù)。
?? 額,從難一些的介紹吧,因?yàn)楫吘沟谝环N方法會(huì)是android未來的一個(gè)選擇,第二種不知道什么時(shí)候就要成為歷史了。
?
android給我們提供的方向數(shù)據(jù)是一個(gè)float型的數(shù)組,包含三個(gè)方向的值 如圖
?
?
當(dāng)你的手機(jī)水平放置時(shí),被默認(rèn)為靜置狀態(tài),即XY角度均為0
?
values[0]? 表示Z軸的角度:方向角,我們平時(shí)判斷的東西南北就是看這個(gè)數(shù)據(jù)的,經(jīng)過我的實(shí)驗(yàn),發(fā)現(xiàn)了一個(gè)有意思的事情,也就是說使用第一種方式獲得方向(磁場(chǎng)+加速度)得到的數(shù)據(jù)范圍是(-180~180),也就是說,0表示正北,90表示正東,180/-180表示正南,-90表示正西。而第二種方式(直接通過方向感應(yīng)器)數(shù)據(jù)范圍是(0~360)360/0表示正北,90表示正東,180表示正南,270表示正西。
values[1]? 表示X軸的角度:俯仰角?? 即由靜止?fàn)顟B(tài)開始,前后翻轉(zhuǎn)
values[2]? 表示Y軸的角度:翻轉(zhuǎn)角? 即由靜止?fàn)顟B(tài)開始,左右翻轉(zhuǎn)
可見統(tǒng)一獲取方向的方法是必須的,因?yàn)樘幚磉@些數(shù)據(jù)的算法可能針對(duì)第一種獲取方式,那么當(dāng)用在第二種方式時(shí),移植性就不好了。
看下面的方法
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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.
通常我們并不需要獲取這個(gè)函數(shù)的返回值,這個(gè)方法會(huì)根據(jù)參數(shù)R[]的數(shù)據(jù)填充values[]而后者就是我們想要的。
那么R表示什么呢?又將怎么獲取呢?
R[] 是一個(gè)旋轉(zhuǎn)矩陣,用來保存磁場(chǎng)和加速度的數(shù)據(jù),大家可以理解未加工的方向數(shù)據(jù)吧
R通過下面的靜態(tài)方法獲取,這個(gè)方法也是用來填充R[]
public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
?
解釋以下參數(shù),第一個(gè)就是我們需要填充的R數(shù)組,大小是9
??????????????????????????? 第二個(gè)是是一個(gè)轉(zhuǎn)換矩陣,將磁場(chǎng)數(shù)據(jù)轉(zhuǎn)換進(jìn)實(shí)際的重力坐標(biāo)中 一般默認(rèn)情況下可以設(shè)置為null
??????????????????????????? 第三個(gè)是一個(gè)大小為3的數(shù)組,表示從加速度感應(yīng)器獲取來的數(shù)據(jù)? 在onSensorChanged中
??????????????? ????????????第四個(gè)是一個(gè)大小為3的數(shù)組,表示從磁場(chǎng)感應(yīng)器獲取來的數(shù)據(jù)??? 在onSensorChanged中
?
好了基本邏輯就是這樣的,下面給大家演示一個(gè)簡單的測(cè)試方向的例子,可以時(shí)刻監(jiān)聽用戶的方向
?
/*
?* @author octobershiner
?* 2011 07 28
?* SE.HIT
?* 一個(gè)演示通過磁場(chǎng)和加速度兩個(gè)感應(yīng)器獲取方向數(shù)據(jù)的例子
?* */?
?
?
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;?
??? //需要兩個(gè)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);?
??????? //更新顯示數(shù)據(jù)的方法?
??????? calculateOrientation();?
?
??? }?
??? //再次強(qiáng)調(diào):注意activity暫停的時(shí)候釋放?
??? 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);?
?
????????? // 要經(jīng)過一次數(shù)據(jù)格式的轉(zhuǎn)換,轉(zhuǎn)換為度?
????????? 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, "西北");?
????????? }?
??????? }?
?????
?
}?
?
?? 實(shí)訓(xùn)的時(shí)間非常緊張,抽時(shí)間寫總結(jié)感覺很累,但是感覺收獲很多,如果有時(shí)間的話,也想給大家分享第二種方法,和這種比起來簡單很多,其實(shí)大家可以完全參考上篇文章中的代碼:http://www.2cto.com/kf/201111/110232.html
只要把其中的兩個(gè)Sensor。TYPE_ACCELEROMETER改成Sensor.TYPE_ORIENTATIO就好了,但是今天分享的方法大家最好掌握,這應(yīng)該是未來android的標(biāo)準(zhǔn)。
?
?
?? Sensor感應(yīng)器應(yīng)該就先暫時(shí)介紹到這里吧,該看一下進(jìn)程線程的東西了,其實(shí)hardware包中還有個(gè)非常重要的類,Camera攝像頭,相信大家也聽過android掃描器,很強(qiáng)大。以后有時(shí)間和大家分享吧。
?? 接下來的安排 應(yīng)該是 線程activity然后是geocode
?? 話說我也沒有個(gè)指導(dǎo)老師,一個(gè)人對(duì)著SDK研究這些,有些累阿~求高人指點(diǎn)。
總結(jié)
以上是生活随笔為你收集整理的Android Sensor感应器介绍,获取用户移动方向,指南针原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。