Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Launcher 開發:圖標默認加載流程與自訂背景

編輯:關於 Android 程式設計

改 Launcher 時,最常被問到的兩個問題是:「桌面第一次啟動時那些圖標是怎麼跑出來的?」以及「要怎麼幫圖標加上統一的背景?」。前者牽涉到一個 XML 設定檔與資料庫的初始化流程,後者則藏在圖標位圖的生成環節。本篇把這兩條線分別拉出來看。

一、桌面圖標的加載過程

Launcher 第一次啟動時,並不是「掃描所有 App 然後排出來」,而是先讀取一個預設的 XML 設定檔完成配置。

這個檔案位於 Launcher 專案底下:

\Launcher\res\xml\default_workspace.xml

它就是剛升級或剛安裝時,Launcher 首次顯示所依據的設定檔。裡面可以配置 App 捷徑、Widget、搜尋欄等元件的位置與內容。

負責解析這個檔案的,是 LauncherProvider.java 裡的 loadFavorites 方法:

private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ContentValues values = new ContentValues();

    if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));

    PackageManager packageManager = mContext.getPackageManager();
    int i = 0;
    try {
        XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        beginDocument(parser, TAG_FAVORITES);

        final int depth = parser.getDepth();
        final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
        LauncherModel model = LauncherAppState.getInstance().getModel();
        // ... 逐節點解析並寫入資料庫
    }
    // ...
    return i;
}

本質上就是「解析 XML 並寫入資料庫」的過程。LauncherProvider 是整個 Launcher 的資料來源——桌面有幾頁、每頁有哪些捷徑、Widget 佔幾格,全部存在它的資料庫裡。理解這一點很重要,因為後續「修改格數、調整背景大小、處理螢幕列數」等工作,都要在這層之上動手。

值得留意的是這個流程只在「首次」或「資料庫不存在」時觸發。之後每次開機,Launcher 讀的都是資料庫,XML 檔不再被解析。所以你改了 default_workspace.xml 卻看不到變化,往往就是因為資料庫已經存在——清掉資料或改動版本號才會重新初始化。

二、幫圖標加上默認背景

這是主題功能裡常見的一塊。圖標的取得與處理集中在 Utilities.java,而回傳應用圖標位圖的方法就是:

static Bitmap createIconBitmap(Bitmap icon, Context context) {
    int textureWidth = sIconTextureWidth;
    int textureHeight = sIconTextureHeight;
    int sourceWidth = icon.getWidth();
    int sourceHeight = icon.getHeight();

    if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
        // 圖標過大,直接裁切(解決 GB 升 ICS 的遷移問題)
        return Bitmap.createBitmap(icon,
                (sourceWidth - textureWidth) / 2,
                (sourceHeight - textureHeight) / 2,
                textureWidth, textureHeight);
    } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
        // 尺寸剛好,不必處理
        return icon;
    } else {
        // 圖標過小,放大繪製
        final Resources resources = context.getResources();
        return createIconBitmap(new BitmapDrawable(resources, icon), context);
    }
}

這個方法負責把各種尺寸的原始圖標,統一處理成 Launcher 需要的標準尺寸。要加背景,就在這裡插入一段合成邏輯:

// 在實際繪製 icon 之前,先鋪一層背景
Bitmap backBitmap = BitmapFactory.decodeResource(
        context.getResources(), R.drawable.apical_icon_bg);
int backWidth = backBitmap.getWidth();
int backHeight = backBitmap.getHeight();

if (backWidth != sIconWidth || backHeight != sIconHeight) {
    Matrix matrix = new Matrix();
    matrix.postScale((float) sIconWidth / backWidth, (float) sIconHeight / backHeight);
    canvas.drawBitmap(
            Bitmap.createBitmap(backBitmap, 0, 0, backWidth, backHeight, matrix, true),
            0.0f, 0.0f, null);
} else {
    canvas.drawBitmap(backBitmap, 0.0f, 0.0f, null);
}
// ... 接著才畫 icon 本身

這樣就指定了一張圖片作為所有圖標的背景。這裡的縮放判斷是必要的防禦:背景圖的實際尺寸未必與圖標標準尺寸一致,不先縮放就會出現「背景只蓋住左上角一部分」的窘狀。

三、實作上的幾個考量

背景圖應該預先快取。decodeResource 每次都從資源解碼一張位圖,而這個方法在桌面載入時會被呼叫非常多次。實務上應該把背景圖解碼一次後存成靜態成員,後續只做縮放與合成。

縮放後的位圖也值得快取。Bitmap.createBitmap 每次都配置一塊新記憶體,桌面圖標一多,反覆配置容易觸發 GC 抖動。

合成順序決定視覺結果。背景必須在圖標之前畫,且圖標本身若帶有透明區域,背景才會透出來。這也是為什麼 iOS 風格的圓角圖標在 Android 上需要背景襯托——直接貼上去會顯得單薄。

最後,改動這類系統應用時務必注意:Launcher 是使用者整個使用體驗的入口,任何改動都應該在改動前先確認回退路徑,並在多種解析度、多種主題的環境下實際驗證。

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