Android使用Sensor感應(yīng)器獲取用戶移動(dòng)方向(指南針原理)
本文實(shí)例講述了Android使用Sensor感應(yīng)器獲取用戶移動(dòng)方向的方法。分享給大家供大家參考,具體如下:
今天繼續(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獲取方向是通過磁場感應(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è)有意思的事情,也就是說使用第一種方式獲得方向(磁場+加速度)得到的數(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ù)的算法可能針對第一種獲取方式,那么當(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)矩陣,用來保存磁場和加速度的數(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)換矩陣,將磁場數(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ù)組,表示從磁場感應(yīng)器獲取來的數(shù)據(jù) 在onSensorChanged中
好了基本邏輯就是這樣的,下面給大家演示一個(gè)簡單的測試方向的例子,可以時(shí)刻監(jiān)聽用戶的方向
/*
* @author octobershiner
* 2011 07 28
* SE.HIT
* 一個(gè)演示通過磁場和加速度兩個(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í)大家可以完全參考上篇文章中的代碼《Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法》
只要把其中的兩個(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è)人對著SDK研究這些,有些累阿~求高人指點(diǎn)。
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測力計(jì)的功能
- Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
- android 傳感器(OnSensorChanged)使用介紹
- 解析如何在android中增加gsensor驅(qū)動(dòng)(MMA7660)
- Android利用方向傳感器獲得手機(jī)的相對角度實(shí)例說明
- Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- Android 傳感器--光照傳感器詳解及使用
- Android利用Sensor(傳感器)實(shí)現(xiàn)指南針小功能
相關(guān)文章
安裝android開發(fā)環(huán)境原始版(windows版)
安裝android開發(fā)環(huán)境原始版(windows版)的詳細(xì)步驟2013-03-03
在Flutter中制作翻轉(zhuǎn)卡片動(dòng)畫的完整實(shí)例代碼
最近Flutter的勢頭是越來越猛了,作為一個(gè)Android程序猿,我自然也是想要趕緊嘗試一把,這篇文章主要給大家介紹了關(guān)于在Flutter中制作翻轉(zhuǎn)卡片動(dòng)畫的相關(guān)資料,需要的朋友可以參考下2021-10-10
基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05
淺談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑
下面小編就為大家?guī)硪黄獪\談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
以一個(gè)著色游戲展開講解Android中區(qū)域圖像填色的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)區(qū)域圖像顏色填充的方法,文中以一個(gè)著色游戲?yàn)槔v解了邊界的填充等各種填色操作,需要的朋友可以參考下2016-02-02
Android提高之SurfaceView與多線程的混搭實(shí)例
這篇文章主要介紹了Android提高之SurfaceView與多線程的混搭,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android實(shí)現(xiàn)檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

