Android使用Sensor感應(yīng)器獲取用戶移動(dòng)方向(指南針原理)
本文實(shí)例講述了Android使用Sensor感應(yīng)器獲取用戶移動(dòng)方向的方法。分享給大家供大家參考,具體如下:
今天繼續(xù)給大家分享一下第二個(gè)重要的感應(yīng)器,其實(shí)獲取方向本應(yīng)該很簡(jiǎn)單的事情,在前面文章中看到有個(gè)TYPE_ORIENTATION 關(guān)鍵字,說(shuō)明可以直接獲取設(shè)備的移動(dòng)方向,但是最新版的SDK加上了這么一句話“TYPE_ORIENTATION This constant is deprecated. use SensorManager.getOrientation() instead. ”也就是說(shuō),這種方式已經(jīng)被取消,要開(kāi)發(fā)者使用 SensorManager.getOrientation()來(lái)獲取原來(lái)的數(shù)據(jù)。
實(shí)際上,android獲取方向是通過(guò)磁場(chǎng)感應(yīng)器和加速度感應(yīng)器共同獲得的,至于具體的算法SDK已經(jīng)封裝好了。也就是說(shuō)現(xiàn)在獲取用戶方向有兩種方式,一是官方推薦的,通過(guò)SensorManager.getOrientation()來(lái)獲取,這個(gè)方法表面看似容易(那是因?yàn)槟氵€沒(méi)看到他的參數(shù)。。一會(huì)再說(shuō)),但實(shí)際上需要用到兩個(gè)感應(yīng)器共同完成工作,特點(diǎn)是更加的準(zhǔn)確。第二種方法非常簡(jiǎn)單,就像前一篇文章獲取加速度一樣,直接得到三個(gè)軸上的數(shù)據(jù)。
額,從難一些的介紹吧,因?yàn)楫吘沟谝环N方法會(huì)是android未來(lái)的一個(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)過(guò)我的實(shí)驗(yàn),發(fā)現(xiàn)了一個(gè)有意思的事情,也就是說(shuō)使用第一種方式獲得方向(磁場(chǎng)+加速度)得到的數(shù)據(jù)范圍是(-180~180),也就是說(shuō),0表示正北,90表示正東,180/-180表示正南,-90表示正西。而第二種方式(直接通過(guò)方向感應(yīng)器)數(shù)據(jù)范圍是(0~360)360/0表示正北,90表示正東,180表示正南,270表示正西。
values[1] 表示X軸的角度:俯仰角 即由靜止?fàn)顟B(tài)開(kāi)始,前后翻轉(zhuǎn)
values[2] 表示Y軸的角度:翻轉(zhuǎn)角 即由靜止?fàn)顟B(tài)開(kāi)始,左右翻轉(zhuǎn)
可見(jià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)矩陣,用來(lái)保存磁場(chǎng)和加速度的數(shù)據(jù),大家可以理解未加工的方向數(shù)據(jù)吧
R通過(guò)下面的靜態(tài)方法獲取,這個(gè)方法也是用來(lái)填充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)器獲取來(lái)的數(shù)據(jù) 在onSensorChanged中
第四個(gè)是一個(gè)大小為3的數(shù)組,表示從磁場(chǎng)感應(yīng)器獲取來(lái)的數(shù)據(jù) 在onSensorChanged中
好了基本邏輯就是這樣的,下面給大家演示一個(gè)簡(jiǎn)單的測(cè)試方向的例子,可以時(shí)刻監(jiān)聽(tīng)用戶的方向
/* * @author octobershiner * 2011 07 28 * SE.HIT * 一個(gè)演示通過(guò)磁場(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)過(guò)一次數(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é)感覺(jué)很累,但是感覺(jué)收獲很多,如果有時(shí)間的話,也想給大家分享第二種方法,和這種比起來(lái)簡(jiǎn)單很多,其實(shí)大家可以完全參考上篇文章中的代碼《Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法》
只要把其中的兩個(gè)Sensor。TYPE_ACCELEROMETER改成 Sensor.TYPE_ORIENTATIO就好了,但是今天分享的方法大家最好掌握,這應(yīng)該是未來(lái)android的標(biāo)準(zhǔn)。
Sensor感應(yīng)器應(yīng)該就先暫時(shí)介紹到這里吧,該看一下進(jìn)程線程的東西了,其實(shí)hardware包中還有個(gè)非常重要的類,Camera攝像頭,相信大家也聽(tīng)過(guò)android掃描器,很強(qiáng)大。以后有時(shí)間和大家分享吧。
接下來(lái)的安排 應(yīng)該是 線程 activity然后是geocode
話說(shuō)我也沒(méi)有個(gè)指導(dǎo)老師,一個(gè)人對(duì)著SDK研究這些,有些累阿~求高人指點(diǎn)。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能
- Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
- android 傳感器(OnSensorChanged)使用介紹
- 解析如何在android中增加gsensor驅(qū)動(dòng)(MMA7660)
- Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說(shuō)明
- Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- Android 傳感器--光照傳感器詳解及使用
- Android利用Sensor(傳感器)實(shí)現(xiàn)指南針小功能
相關(guān)文章
Android進(jìn)程間通信(IPC)機(jī)制Binder簡(jiǎn)要介紹
本文主要介紹 Android進(jìn)程間通信(IPC)機(jī)制Binder簡(jiǎn)要介紹, 這里介紹了Binder機(jī)制如何實(shí)現(xiàn)進(jìn)程通信機(jī)制,有研究Android源碼的朋友可以看下2016-08-08Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片
這篇文章主要介紹了Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2015-07-07Android筆記之:深入ViewStub的應(yīng)用
本篇文章是對(duì)Android中ViewStub的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android中Intent傳遞對(duì)象的兩種方法Serializable,Parcelable
這篇文章主要介紹了Android中的傳遞有兩個(gè)方法,一個(gè)是Serializable,另一個(gè)是Parcelable,對(duì)intent傳遞對(duì)象的兩種方法感興趣的朋友一起學(xué)習(xí)吧2016-01-01android 復(fù)制 粘貼 剪切功能應(yīng)用
網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下2012-11-11Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法,結(jié)合實(shí)例形式分析了Android全局Context的獲取及Intent傳遞對(duì)象的具體操作方法,需要的朋友可以參考下2017-08-08Android自定義view實(shí)現(xiàn)帶header和footer的Layout
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)帶header和footer的Layout,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02Android編程實(shí)現(xiàn)定時(shí)發(fā)短信功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)定時(shí)發(fā)短信功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android定時(shí)發(fā)送短信功能的相關(guān)原理、實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2017-09-09