Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 車載指南針自製教學:從 ApiDemo 到自訂 CompassView

編輯: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() 讓它跟著方位轉。注意角度要取負值,因為感測器角度與視覺上的旋轉方向相反。

自己動手做一個:CompassView

基於同樣的原理,我實作了一個可以獨立使用的小指南針。設計上有個堅持:讓 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

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved