Android方向傳感器的使用方法
在應(yīng)用程序中使用SensorManager.getOrientation()來獲得原始數(shù)據(jù)。
public static float[] getOrientation (float[] R, float[] values)
第一個參數(shù)是R用來保存磁場和加速度的數(shù)據(jù),通過該函數(shù)獲取方位角。
第二個參數(shù)是函數(shù)輸出,數(shù)據(jù)自動填充。
- values[0]:方向角,但用(磁場+加速度)得到的數(shù)據(jù)范圍是(-180~180),也就是說,0表示正北,90表示正東,180/-180表示正南,-90表示正西。而直接通過方向感應(yīng)器數(shù)據(jù)范圍是(0~359)360/0表示正北,90表示正東,180表示正南,270表示正西。
- values[1]:pitch 傾斜角即由靜止?fàn)顟B(tài)開始,前后翻轉(zhuǎn),手機(jī)頂部往上抬起(0~-90),手機(jī)尾部往上抬起(0~90)
- values[2]:roll 旋轉(zhuǎn)角 即由靜止?fàn)顟B(tài)開始,左右翻轉(zhuǎn),手機(jī)左側(cè)抬起(0~90),手機(jī)右側(cè)抬起(0~-90)
通過函數(shù)getRotationMatrix獲取R
public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
注冊監(jiān)聽
sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME);
主要代碼
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
private Sensor acc_sensor;
private Sensor mag_sensor;
//加速度傳感器數(shù)據(jù)
float accValues[] = new float[3];
//地磁傳感器數(shù)據(jù)
float magValues[] = new float[3];
//旋轉(zhuǎn)矩陣,用來保存磁場和加速度的數(shù)據(jù)
float r[] = new float[9];
//模擬方向傳感器的數(shù)據(jù)(原始數(shù)據(jù)為弧度)
float values[] = new float[3];
TextView showTV = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show_change=(TextView) findViewById(R.id.show_change);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
acc_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mag_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
// 注冊監(jiān)聽:
sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 回調(diào)方法
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
accValues = event.values.clone();
}
else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
magValues = event.values.clone();
}
/**
* r:要填充的旋轉(zhuǎn)數(shù)組
* I: 將磁場數(shù)據(jù)轉(zhuǎn)換進(jìn)實際的重力坐標(biāo)中,一般默認(rèn)情況下可以設(shè)置為null
* gravity: 加速度傳感器數(shù)據(jù)
* geomagnetic:地磁傳感器數(shù)據(jù)
*/
SensorManager.getRotationMatrix(r, null, accValues, magValues);
/**
* R:旋轉(zhuǎn)數(shù)組
* values:模擬方向傳感器的數(shù)據(jù)
*/
SensorManager.getOrientation(r, values);
//將弧度轉(zhuǎn)化為角度后輸出
StringBuffer buff = new StringBuffer();
for(float value : values){
value=(float) Math.toDegrees(value);
buff.append(value + " ");
}
showTV.setText(buff.toString());
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)計步傳感器功能
- Android開發(fā)獲取傳感器數(shù)據(jù)的方法示例【加速度傳感器,磁場傳感器,光線傳感器,方向傳感器】
- Android編程基于距離傳感器控制手機(jī)屏幕熄滅的方法詳解
- Android開發(fā)中方向傳感器定義與用法詳解【附指南針實現(xiàn)方法】
- Android開發(fā)中的重力傳感器用法實例詳解
- Android 獲取傳感器列表整理及簡單實例
- Android亮屏速度分析總結(jié)
- Android四大組件之Activity詳解
- Android四大組件之Service詳解
- Android傳感器SensorEventListener之加速度傳感器
相關(guān)文章
android讀寫sd卡操作寫入數(shù)據(jù)讀取數(shù)據(jù)示例
這篇文章主要介紹了android讀寫sd卡操作,示例實現(xiàn)了寫入數(shù)據(jù)讀取數(shù)據(jù)的功能,大家參考使用吧2014-01-01
關(guān)于OkHttp中response.body().string()的用法解析
這篇文章主要介紹了關(guān)于OkHttp中response.body().string()的用法解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Android 2.3.7.r1 camera錄像過程中按menu菜單鍵時會停止錄像
android GB版本的camera錄像過程中按“菜單”鍵會停止錄像,改成錄像時按menu鍵不做處理,具體修改方法如下,感興趣的朋友可以參考下哈2013-06-06
Android App后臺服務(wù)報告工作狀態(tài)實例
這篇文章主要介紹了Android App后臺服務(wù)報告工作狀態(tài)實例,使用LocalBroadcastManager發(fā)送和接收狀態(tài),需要的朋友可以參考下2014-06-06
Android實現(xiàn)軟件列表的點擊啟動另外一個程序功能【附demo源碼下載】
這篇文章主要介紹了Android實現(xiàn)軟件列表的點擊啟動另外一個程序功能,涉及Android針對應(yīng)用程序的讀取、加載、啟動等操作相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android Jetpack架構(gòu)中ViewModel接口暴露的不合理探究
這篇文章主要介紹了Android Jetpack架構(gòu)組件 ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù)。感興趣可以來學(xué)習(xí)一下2022-07-07
Android自定義StickinessView粘性滑動效果
這篇文章主要為大家詳細(xì)介紹了Android自定義StickinessView粘性滑動效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

