編輯:Android 開發實例
ApiDemo 裡藏著一個相當經典的指南針範例,路徑是 com.example.android.apis.graphics 底下的 Compass.java。它的原理並不複雜:用 SensorListener 取得方向感測器的角度,再依這個角度不斷重繪 View。網路上能找到的相關程式碼,大致都是從這份 Demo 改出來的。
整個繪製流程可以濃縮成幾個動作。先在 onResume() 註冊方向感測器,並在 onStop() 註銷,避免浪費電力:
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mListener,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mListener);
super.onStop();
}
感測器一有變化,回呼裡就把新的角度存起來並要求重繪:
public void onSensorChanged(int sensor, float[] values) {
mValues = values;
if (mView != null) {
mView.invalidate();
}
}
而 onDraw() 做的事就是「把畫布轉到中心,再依角度旋轉,然後畫出指針」:
canvas.drawColor(Color.WHITE);
canvas.translate(cx, cy);
if (mValues != null) {
canvas.rotate(-mValues[0]);
}
canvas.drawPath(mPath, mPaint);
這裡的 mPath 是在建構子中先用 moveTo 與 lineTo 拼出的一個楔形(菱形指針),再靠 canvas.rotate() 讓它跟著方位轉。注意角度要取負值,因為感測器角度與視覺上的旋轉方向相反。
基於同樣的原理,我實作了一個可以獨立使用的小指南針。設計上有個堅持:讓 View 保持相對獨立、標準化,也就是同時支援程式碼 new 出來、也能在 XML 版面檔裡直接引用。要做到這件事,就必須提供帶 AttributeSet 參數的建構子:
public class CompassView extends View {
private Bitmap[] mBitmapArray = new Bitmap[4];
private float[] mValues;
private int[] mBitmapWidth = new int[4];
private int[] mBitmapHeight = new int[4];
public CompassView(Context context) {
this(context, null);
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
setBitMapArray(context, 0, R.drawable.panel);
setBitMapArray(context, 1, R.drawable.compass_digree);
}
/** 將角度值傳入 View,觸發重繪 */
public void setValue(float[] value) {
this.mValues = value;
}
}
繪製時的關鍵是「轉過去、畫完、再轉回來」,讓背景盤面與指針各自獨立旋轉:
canvas.rotate(-mValues[0]); canvas.drawBitmap(mBitmapArray[0], -mBitmapWidth[0] / 2, -mBitmapHeight[0] / 2, null); canvas.rotate(360 + mValues[0]); canvas.drawBitmap(mBitmapArray[1], -mBitmapWidth[1] / 2, -mBitmapHeight[1] / 2, null);
用 -width / 2 與 -height / 2 當作繪製座標,是為了讓圖片以中心為原點對齊——因為前面已經把畫布平移到正中央了。這一步沒算對,指針就會偏心。
由於提供了 AttributeSet 建構子,自訂 View 可以直接寫進 XML,用完整套件名稱當作標籤:
<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">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.yaomei.view.CompassView
android:id="@+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
另外別忘了在 AndroidManifest.xml 對應的 Activity 上加上 android:screenOrientation="landscape",讓車載畫面固定橫屏。
Activity 的責任是取得感測器資料、轉換成方向文字,再交給 View 重繪:
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
mView.setValue(values);
// 依角度區間判斷方位:0 北、90 西、180 南、270 東,其餘為偏角
tv.setText(text);
if (mView != null) {
mView.invalidate();
}
}
}
這裡用 synchronized 包住,是因為感測器回呼與 UI 繪製可能在不同時機發生。另外 Activity 的 onCreate() 中還加了 FLAG_KEEP_SCREEN_ON,避免導航途中螢幕自動關閉。
這整套架構最有價值的地方在於資料來源可以替換。手機上的指南針靠的是 SensorListener(在新版 API 中已被 SensorEventListener 取代),但若要做成車載用途,完全可以改由 GPS 或車輛 CAN _bus 傳來的方位角驅動,只要呼叫 setValue() 並重繪即可,View 本身一行都不用改。
這正是把 View 標準化的好處:畫面與資料解耦之後,同一個元件能服務完全不同的情境。
轉自:https://www.cnblogs.com/TerryBlog/archive/2010/11/12/1875657.html
封裝 SQLite 分頁表格控件 GVTable:表格區與分頁列的完整實作
前一篇談了 SQLite 的分頁讀取,但那個版本只是把資料印在文字方塊裡。這次往前一步:把「分頁 + 表格呈現」封裝成一個可重複使用的自訂控件。 執行結果: 為什
Android 本地化:讓應用適配不同語言環境
為什麼需要本地化?Android 應用可能會在不同地區、不同語言環境的裝置上執行。為了讓使用者更容易理解,應用中的文字、數字格式與其他資源最好能依語言環境調整。建立區域資
Android手機衛士(十六):事件傳遞與 CheckBox 點擊衝突
當點擊 SettingItemView 時,事件會由父層配置接收,再傳遞給自訂元件。若點擊位置在 CheckBox,事件可能被 CheckBox 消耗,導致自訂元件無法更
Android 啟動畫面實作:進度提示與背景初始化的雙線程設計
大型 App 啟動時往往需要讀設定檔、建立資料庫連線或拉取遠端資源,這段時間若讓使用者盯著空白畫面,感受會非常糟。做一個載入畫面、配上會動的進度提示,是最便宜也最有效的體