編輯:Android 開發實例
行動裝置之所以能做計步、自動旋轉、第一人稱遊戲,靠的都是內建的各種感測器。Android 把這些硬體抽象成 SensorManager,開發者只要註冊一個 SensorListener,就能持續收到感測數值,完全不必理會底層驅動。
以下這份來自 IBM developerWorks 的範例(IBMEyes)同時監聽兩種感測器:SENSOR_ACCELEROMETER(加速度)與 SENSOR_ORIENTATION(方向),並把 XYZ 三軸數值即時顯示在畫面上。
/*
* IBMEyes.java
* sample code for IBM Developerworks Article
* Author: W. Frank Ableson
*/
package com.msi.ibm.eyes;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.hardware.SensorManager;
import android.hardware.SensorListener;
public class IBMEyes extends Activity implements SensorListener {
final String tag = "IBMEyes";
SensorManager sm = null;
TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
}
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
xViewO.setText("Orientation X: " + values[0]);
yViewO.setText("Orientation Y: " + values[1]);
zViewO.setText("Orientation Z: " + values[2]);
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + values[0]);
yViewA.setText("Accel Y: " + values[1]);
zViewA.setText("Accel Z: " + values[2]);
}
}
}
public void onAccuracyChanged(int sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
}
這份程式碼有三個值得學習的設計。第一,onSensorChanged() 內部用 synchronized (this) 包住,因為感測器回調可能來自多條執行緒,同時註冊兩種感測器時更需要注意。第二,註冊時用位元或運算子把兩種感測器合併成一個遮罩一次註冊,比呼叫兩次 registerListener 更乾淨。第三,也是最重要的一點:在 onResume() 註冊、在 onStop() 取消註冊。感測器是耗電大戶,若忘了在畫面離開時 unregisterListener(),電量會被背景中的監聽迅速吃光,這是新手最常犯的錯誤。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.msi.ibm.eyes;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int xbox=0x7f050000;
public static final int xboxo=0x7f050003;
public static final int ybox=0x7f050001;
public static final int yboxo=0x7f050004;
public static final int zbox=0x7f050002;
public static final int zboxo=0x7f050005;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
從 R.java 可以看到,版面裡預留了六個 TextView(xbox/ybox/zbox 與各自的 o 版本),分別顯示加速度與方向的 XYZ 值。R.java 是 aapt 自動產生的檔案,開頭那句 DO NOT MODIFY 是認真的——手動修改會在下一次建置時被覆蓋。
補充說明:SENSOR_ORIENTATION 這個感測器已在較新版本中被標示為不建議使用,正式專案請改用 TYPE_ROTATION_VECTOR 或 TYPE_ACCELEROMETER 搭配 TYPE_MAGNETIC_FIELD 自行計算方位角。另外 SensorListener 介面同樣已被 SensorEventListener 取代,後者能額外提供精確度變化的詳細資訊與更精細的取樣週期控制。
Android 音頻錄製與音頻視覺化開發案例
Android 提供多種音頻處理方式,從呼叫系統內建錄音程式,到自行使用 MediaRecorder、AudioRecord 進行即時聲波分析,都能延伸出不同的應用。本文
Android MediaPlayer 使用指南:播放控制、進度條與快轉倒轉
要在 App 裡播放音訊或影片,Android 提供了多種途徑,其中最直接的就是 MediaPlayer。它屬於系統內建的媒體播放服務,透過這個類別,你能以幾行程式碼完成
Android 教學:依系列圖片繪製動畫的三種做法
本文要說明如何依據一系列圖片來繪製動畫。根據素材的不同,主要可以分成三種做法,以下逐一介紹。一、使用系統提供的 Animation 類這種方式最簡單,用自帶的方法即可。其
Android MediaPlayer 多媒體播放完全解析
Android 提供了多種控制音訊與影片播放的方式,無論是播放本機檔案還是串流媒體都能勝任,而其中最常被使用的,莫過於 MediaPlayer 這個類別。 Android