編輯:關於 Android 程式設計
指南針是入門感應器開發最經典的範例。原理並不複雜:讀取方向感應器(Orientation Sensor)回報的角度值,再用旋轉動畫即時轉動指針圖片,讓它永遠指向北方。
新增一個名為 Compass 的專案,把指南針圖片放進 res/drawable-hdpi 目錄。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/compass"
android:id="@+id/imageView"
/>
</LinearLayout>
public class MainActivity extends Activity {
private ImageView imageView;
/** 感應器管理器 */
private SensorManager manager;
private SensorListener listener = new SensorListener();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageView);
imageView.setKeepScreenOn(true); // 保持螢幕不熄
// 取得系統服務 SENSOR_SERVICE,回傳 SensorManager
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
// 取得方向感應器
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
// 應用回到前台時註冊監聽器
manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
// 離開前台時務必取消註冊,避免耗電
manager.unregisterListener(listener);
super.onPause();
}
private final class SensorListener implements SensorEventListener {
private float predegree = 0;
@Override
public void onSensorChanged(SensorEvent event) {
/*
* values[0]: x 軸方向的角度(方位角)
* values[1]: y 軸方向的俯仰
* values[2]: z 軸方向的滾轉
*/
float degree = event.values[0];
RotateAnimation animation = new RotateAnimation(
predegree, degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(200);
imageView.startAnimation(animation);
predegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="cn.roco.sensor"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RELATIVE_TO_SELF, 0.5f 代表以圖片自身中心為旋轉軸,這是指南針動畫自然的關鍵。predegree = -degree 承接上一幀角度,讓動畫有連續感;實務上更好用的做法是處理「359° 跳到 1°」的跨界問題,把差值正規化到 -180~180 之間,指針才不會整圈狂轉。onResume 註冊、onPause 取消註冊,否則背景仍持續取樣會明顯耗電。TYPE_ACCELEROMETER 與 TYPE_MAGNETIC_FIELD 兩者透過 SensorManager.getRotationMatrixFromVector/getOrientation 組合成方位角,精度與相容性都更好。若要判斷是否有磁場感應器,也可用 hasSystemFeature(SensorManager.SENSOR_SERVICE) 相關檢查提前處理。
用 Android SDK 內建的 lint 工具檢查佈局錯誤與可疑寫法
現在的 ADT 與 Android Studio 已經能在佈局編輯器裡即時標出錯誤和警告,但命令列工具偶爾還是能派上用場——特別是批次檢查多個專案,或者你想在 CI 流程
Android 4.X SIM 卡資訊初始化過程詳解(Phone 與 UiccController 流程)
本文詳細解析 Android 4.X 中 SIM 卡資訊的初始化過程,供大家參考。 在 Phone 物件初始化的過程中,會載入 SIM 卡的部分資料,這些資訊會保存在 I
深入理解了 Tomcat 中的 Servlet 對象池機制
Servlet 在不實現 SingleThreadModel 的情況下,是以單個實例模式運行的。此時 Wrapper 容器只會透過反射實例化一個 Servlet 對象,對
基於 Socket 的 Android 聊天室:服務端與客戶端完整實現
一、整體思路 Socket 是網絡通信中最基礎也最常用的技術之一。通過 Socket 建立的可靠連接,能讓多個終端同時與服務器保持通信,最典型的應用就是多人聊天程序。 本