Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
本文實(shí)例講述了Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法。分享給大家供大家參考,具體如下:
FETC項(xiàng)目指導(dǎo)老師提出了新的需求,想要在游戲地圖中表現(xiàn)出用戶用戶當(dāng)期移動(dòng)的方向,再用GPS的話顯然很不靠譜,所以想到了android強(qiáng)大的感應(yīng)器。。。
很多移動(dòng)設(shè)備都內(nèi)置了感應(yīng)器,android通過(guò)Sensor和SensorManager類(lèi)抽象了這些感應(yīng)器,通過(guò)這些類(lèi)可以使用android設(shè)備的傳感器
一 介紹Sensor類(lèi)
SDK只有一句介紹“Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.”,表示一個(gè)感應(yīng)器的類(lèi),可以使用getSensorList方法(此方法屬于接下來(lái)要講的SensorManager)獲得所有可用的感應(yīng)器,該方法返回的是一個(gè)List<Sensor>
下面的列表顯示了,Sensor所提供的所有服務(wù)
Constants
int TYPE_ACCELEROMETER A constant describing an accelerometer sensor type.
//三軸加速度感應(yīng)器 返回三個(gè)坐標(biāo)軸的加速度 單位m/s2
int TYPE_ALL A constant describing all sensor types.
//用于列出所有感應(yīng)器
int TYPE_GRAVITY A constant describing a gravity sensor type.
//重力感應(yīng)器
int TYPE_GYROSCOPE A constant describing a gyroscope sensor type
//陀螺儀 可判斷方向 返回三個(gè)坐標(biāo)軸上的角度
int TYPE_LIGHT A constant describing an light sensor type.
//光線感應(yīng)器 單位 lux 勒克斯
int TYPE_LINEAR_ACCELERATION A constant describing a linear acceleration sensor type.
//線性加速度
int TYPE_MAGNETIC_FIELD A constant describing a magnetic field sensor type.
//磁場(chǎng)感應(yīng) 返回三個(gè)坐標(biāo)軸的數(shù)值 微特斯拉
int TYPE_ORIENTATION This constant is deprecated. use SensorManager.getOrientation() instead.
//方向感應(yīng)器 已過(guò)時(shí) 可以使用方法獲得
int TYPE_PRESSURE A constant describing a pressure sensor type
//壓力感應(yīng)器 單位 千帕斯卡
int TYPE_PROXIMITY A constant describing an proximity sensor type.
//距離傳感器
int TYPE_ROTATION_VECTOR A constant describing a rotation vector sensor type.
//翻轉(zhuǎn)傳感器
int TYPE_TEMPERATURE A constant describing a temperature sensor type
//溫度傳感器 單位 攝氏度
此類(lèi)中包含的方法都是get型的 用來(lái)獲取所選sensor的一些屬性,sensor類(lèi)一般不需要new而是通過(guò)SensorManager的方法獲得
二 介紹SensorManager類(lèi)
SDK解釋?zhuān)骸癝ensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off. ”
SensorManager 允許你訪問(wèn)設(shè)備的感應(yīng)器。通過(guò)傳入?yún)?shù)SENSOR_SERVICE參數(shù)調(diào)用Context.getSystemService方法可以獲得一個(gè)sensor的實(shí)例。永遠(yuǎn)記得確保當(dāng)你不需要的時(shí)候,特別是Activity暫定的時(shí)候,要關(guān)閉感應(yīng)器。忽略這一點(diǎn)肯能導(dǎo)致幾個(gè)小時(shí)就耗盡電池,注意當(dāng)屏幕關(guān)閉時(shí),系統(tǒng)不會(huì)自動(dòng)關(guān)閉感應(yīng)器。
三 常用的感應(yīng)器
(1) 加速度感應(yīng)器
可以通過(guò)這個(gè)感應(yīng)器獲得三個(gè)浮點(diǎn)型
x-axis
y-axis
z-axis
可參閱《android 高級(jí)編程2》中的一個(gè)插圖分析次數(shù)據(jù)
X Y Z分別對(duì)應(yīng)values[0]到[2]
X表示左右移動(dòng)的加速度
Y表示前后移動(dòng)的加速度
Z表示垂直方向的加速度 (測(cè)試時(shí)發(fā)現(xiàn),將手機(jī)置于水平桌面穩(wěn)定后 XY均為0 Z的值為9.4 約等于重力加速度,依次可以做一個(gè)簡(jiǎn)單的算法實(shí)現(xiàn)重力測(cè)力計(jì),有時(shí)間會(huì)給大家一個(gè)例子)
下面先看一個(gè)基本的獲取加速的demo,希望大家好好注意代碼中的注釋
/* * @author octobershiner * 2011 07 27 * SE.HIT * 一個(gè)演示android加速度感應(yīng)器的例子 * */ package uni.sensor; import java.util.Iterator; import java.util.List; 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 SensorDemoActivity extends Activity { /** Called when the activity is first created. */ //設(shè)置LOG標(biāo)簽 private static final String TAG = "sensor"; private SensorManager sm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //創(chuàng)建一個(gè)SensorManager來(lái)獲取系統(tǒng)的傳感器服務(wù) sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); //選取加速度感應(yīng)器 int sensorType = Sensor.TYPE_ACCELEROMETER; /* * 最常用的一個(gè)方法 注冊(cè)事件 * 參數(shù)1 :SensorEventListener監(jiān)聽(tīng)器 * 參數(shù)2 :Sensor 一個(gè)服務(wù)可能有多個(gè)Sensor實(shí)現(xiàn),此處調(diào)用getDefaultSensor獲取默認(rèn)的Sensor * 參數(shù)3 :模式 可選數(shù)據(jù)變化的刷新頻率 * */ sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL); } /* * SensorEventListener接口的實(shí)現(xiàn),需要實(shí)現(xiàn)兩個(gè)方法 * 方法1 onSensorChanged 當(dāng)數(shù)據(jù)變化的時(shí)候被觸發(fā)調(diào)用 * 方法2 onAccuracyChanged 當(dāng)獲得數(shù)據(jù)的精度發(fā)生變化的時(shí)候被調(diào)用,比如突然無(wú)法獲得數(shù)據(jù)時(shí) * */ final SensorEventListener myAccelerometerListener = new SensorEventListener(){ //復(fù)寫(xiě)onSensorChanged方法 public void onSensorChanged(SensorEvent sensorEvent){ if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ Log.i(TAG,"onSensorChanged"); //圖解中已經(jīng)解釋三個(gè)值的含義 float X_lateral = sensorEvent.values[0]; float Y_longitudinal = sensorEvent.values[1]; float Z_vertical = sensorEvent.values[2]; Log.i(TAG,"\n heading "+X_lateral); Log.i(TAG,"\n pitch "+Y_longitudinal); Log.i(TAG,"\n roll "+Z_vertical); } } //復(fù)寫(xiě)onAccuracyChanged方法 public void onAccuracyChanged(Sensor sensor , int accuracy){ Log.i(TAG, "onAccuracyChanged"); } }; public void onPause(){ /* * 很關(guān)鍵的部分:注意,說(shuō)明文檔中提到,即使activity不可見(jiàn)的時(shí)候,感應(yīng)器依然會(huì)繼續(xù)的工作,測(cè)試的時(shí)候可以發(fā)現(xiàn),沒(méi)有正常的刷新頻率 * 也會(huì)非常高,所以一定要在onPause方法中關(guān)閉觸發(fā)器,否則講耗費(fèi)用戶大量電量,很不負(fù)責(zé)。 * */ sm.unregisterListener(myAccelerometerListener); super.onPause(); } }
測(cè)試的時(shí)候會(huì)發(fā)現(xiàn)刷新的特別快,這就引出一個(gè)問(wèn)題,如果真的要呈現(xiàn)在UI中的話,就會(huì)不斷的繪制界面,非常耗費(fèi)資源,所以《android高級(jí)編程》中給出了一個(gè)方案就是引入新的線程來(lái)刷新界面,明天有時(shí)間的話,盡量把把例子給大家。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)的重力感應(yīng)示例代碼
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- Android編程之重力感應(yīng)用法分析
- Android 重力傳感器在游戲開(kāi)發(fā)中的應(yīng)用
- Android 物理游戲之重力系統(tǒng)開(kāi)發(fā)示例代碼
- Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說(shuō)明
- Android 傳感器--光照傳感器詳解及使用
- Android利用傳感器實(shí)現(xiàn)微信搖一搖功能
- Android編程中光線傳感器的調(diào)用方法詳解
- Android開(kāi)發(fā)獲取重力加速度和磁場(chǎng)強(qiáng)度的方法
相關(guān)文章
詳解Android平臺(tái)上讀寫(xiě)NFC標(biāo)簽
NFC,即Near Field Communication,近距離無(wú)線通訊技術(shù),是一種短距離的(通常<=4cm或更短)高頻(13.56M Hz)無(wú)線通信技術(shù),可以讓消費(fèi)者簡(jiǎn)單直觀地交換信息、訪問(wèn)內(nèi)容與服務(wù)。2017-01-01Android?實(shí)現(xiàn)APP可切換多語(yǔ)言步驟詳解
如果是單獨(dú)給app加上國(guó)際化,其實(shí)很容易,創(chuàng)建對(duì)應(yīng)的國(guó)家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語(yǔ)言,需要的朋友可以參考下2023-11-11Android uses-permission權(quán)限列表中文注釋版
Android有一個(gè)精心設(shè)計(jì)的安全模型。每一個(gè)應(yīng)用都有其自己Linux用戶和群組,在單獨(dú)的進(jìn)程和VM上運(yùn)行,不能影響到其他應(yīng)用2014-05-05基于Vert.x和RxJava 2構(gòu)建通用的爬蟲(chóng)框架的示例
這篇文章主要介紹了基于Vert.x和RxJava 2構(gòu)建通用的爬蟲(chóng)框架的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02結(jié)合Windows窗口深入分析Android窗口的實(shí)現(xiàn)
在Android中,窗口是一個(gè)基本的圖形用戶界面元素,它提供了一個(gè)屏幕區(qū)域來(lái)放置應(yīng)用程序的用戶界面元素。窗口可以是全屏的,也可以是一個(gè)小的對(duì)話框。每個(gè)窗口都有一個(gè)特定的主題和樣式,可以根據(jù)應(yīng)用程序的需求進(jìn)行自定義2023-04-04