星期五, 6月 29, 2012

Sensor (三)

Android應用程式學習筆記

Motion Sensors

Android平台提供好幾個感應器讓我們監測裝置的運動,這些感應器中有些是以硬體為基礎感應產生數據,有些是透過硬體感應器得到的數據再經由軟體運算產生數據。

運動感測器主要用來監測裝置的運動,比如傾斜、震動、旋轉、擺動。這運動我們可以看成兩類,第一類運動通常是使用者直接輸入的反射(例如使用在遊戲中操控汽車,或是使用者在遊戲中控制球),第二類是裝置在自然環境下的反射(例如裝置與你在車中一起移動)。由運動感應器通常無法用來監測裝置位置,但是可以和其他感應器一起使用,例如地磁場感應器,決定裝置的位置。


所有的運動感應器對於每一個SensorEvent都是回傳多維度的陣列數據,通常都是float型態的陣列。下表總結了在Android平台可用的運動感應器。



SensorSensor event dataDescriptionUnits of measure
TYPE_ACCELEROMETERSensorEvent.values[0]Acceleration force along the x axis (including gravity).m/s2
SensorEvent.values[1]Acceleration force along the y axis (including gravity).
SensorEvent.values[2]Acceleration force along the z axis (including gravity).
TYPE_GRAVITYSensorEvent.values[0]Force of gravity along the x axis.m/s2
SensorEvent.values[1]Force of gravity along the y axis.
SensorEvent.values[2]Force of gravity along the z axis.
TYPE_GYROSCOPESensorEvent.values[0]Rate of rotation around the x axis.rad/s
SensorEvent.values[1]Rate of rotation around the y axis.
SensorEvent.values[2]Rate of rotation around the z axis.
TYPE_LINEAR_ACCELERATIONSensorEvent.values[0]Acceleration force along the x axis (excluding gravity).m/s2
SensorEvent.values[1]Acceleration force along the y axis (excluding gravity).
SensorEvent.values[2]Acceleration force along the z axis (excluding gravity).
TYPE_ROTATION_VECTORSensorEvent.values[0]Rotation vector component along the x axis (x * sin(θ/2)).Unitless
SensorEvent.values[1]Rotation vector component along the y axis (y * sin(θ/2)).
SensorEvent.values[2]Rotation vector component along the z axis (z * sin(θ/2)).
SensorEvent.values[3]Scalar component of the rotation vector ((cos(θ/2)).1


在運動偵測和監測上,旋轉向量感應器與重力感應器是最常使用的感應器。旋轉向量感應器應用範圍很廣,比如偵測手勢、監測角度的變化、監測方向的變化。


Android Open Source Project Sensors


Android open source project(AOSP)提供三種基於軟體的感應器,重力感應器、線性加速感應器、旋轉向量感應器,這些感應器在Android 4.0中更新且現在利用裝置上的陀螺儀改善穩定性和效能,如果你想試著使用這些感應器,可以使用getVendor()和getVersion()方法便是它們。用vendor和vesion號碼辨識它們是有必要的,因為Android系統將這些感應器是為次要的感應器。例如,如果你的裝置製造商有提供它們自己的重力感應器,那麼AOSP的重力感應器就會顯示為次要的感應器。這三個感應器接依賴陀螺儀,所以如果裝置沒有陀螺儀,這些感應器就不會顯示,也不能使用。


沒有留言:

張貼留言