編輯:Android 開發教學
步驟 1:建立一個名為 Compass 的專案,並把一張指南針圖片放進 res/drawable-hdpi 目錄。

步驟 2:設計應用介面,main.xml
<?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>
步驟 3:MainActivity.java
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.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
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) { }
}
}
步驟 4:AndroidManifest.xml
<?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>
整個實作的關鍵有兩處:一是透過 getSystemService(Context.SENSOR_SERVICE) 取得 SensorManager,再向它索取 TYPE_ORIENTATION 型別的 Sensor;二是把監聽器的註冊與反註冊分別放在 onResume() 與 onPause(),這樣應用不在前台時就不會白白消耗感測資源。至於指針的轉動,則用 RotateAnimation 以「上一角度 → 當前角度」做插值,記得把 predegree 更新為 -degree,才能讓指針方向與實際方位一致。
Android Fragment完全解析:碎片的用途與螢幕適配
Android早期主要用Activity呈現畫面,但在平板與大螢幕裝置上,單一Activity往往難以兼顧靈活性。Fragment的出現,讓畫面可以拆成多個可重複使用的區
Android 簡明開發教程四:Android 應用的基本概念
Android 平台最顯著的特點之一是「低耦合」。Activity 是 Android 應用最基本的 UI 模組,若拿 Windows Form 應用來類比,它相當於 W
Android 監聽應用安裝與卸載事件
Android 會對應用安裝與卸載發送系統廣播。接收器建立 BroadcastReceiver,攔截 PACKAGE_ADDED 與 PACKAGE_REMOVED。註冊
Android 雙機 ADB 除錯與 Root 注意事項
開發者常需要讓同一支 Android 手機同時連接兩台電腦,例如一台透過 USB 除錯,另一台透過網路 ADB 連線。網路 ADB 設定部分客製化 ROM 提供「無線 A