編輯:關於 Android 程式設計
Android平台的API並沒有為換膚提供一套現成的簡便機制,這大概與國外軟體更看重功能和易用、換膚文化並不流行有關。既然系統不直接支援,就只能靠自己去鑽研。
所謂換膚,可以視為動態替換各類資源——文字、顏色、字型大小、圖片、版面配置檔等等。用程式語言動態設定確實做得到,比如呼叫View的setBackgroundResource、setTextSize、setTextColor等方法。但我們顯然不可能在每個Activity裡逐一為頁面上的所有控制項呼叫這些方法來換膚,那樣寫出來的程式碼既難以維護、難以擴充,也背離了「UI與程式碼分離」的原則(Android開發正是用xml檔來佈局UI)。
一般來說,皮膚資源會放在主程式apk之外提供,一方面能縮小主程式體積,另一方面也方便隨時推出新皮膚擴充。
簡單講,軟體皮膚涵蓋圖示、字型、版面配置、互動風格等,換膚就是把這些資源中的一部分或全部替換掉。
主流應用程式的換膚方式
國內有大量軟體支援皮膚定製,這也是它與國外軟體的一大差異:國外用戶更在意社交、郵件等功能,國內用戶則更看重音樂、小說、皮膚等體驗。在動手寫這個換膚系列之前,我也參考了前人的一些總結。由於這些知識點比較零散,我對其進行了整理、優化與擴充。當然,因為精力有限,部分換膚方式我只實現了主體功能,若有疏漏或不足之處,歡迎大家指正或自行擴充。
文中提到的幾種主流實現方式,後續文章會逐一附上程式碼說明,這裡先做整體概述。目前主流的換膚從功能上大致可分為以下幾種。
1) 軟體內建多套皮膚,用戶無法自行增刪或修改:
自由度最低,實現起來也相對簡單。如果你的程式和資源都不大,可以直接把足夠的皮膚資源放進主程式apk。典型應用:平板電腦Apad上QQ空間的換膚功能,實際上只是改變了Activity的背景或這部分資源。
2) 官方提供皮膚供下載,用戶可下載使用:
用戶能挑選自己喜歡的皮膚,有些玩家甚至會破解定製方法,自己做皮膚使用或分享給網友。典型應用:墨跡天氣下載的皮膚是一個zip壓縮檔,套用時把資源釋放到應用目錄下,換膚時新資源覆蓋舊資源,載入時直接從手機儲存空間讀取圖片,命名與程式內資源保持一致,找不到時退回系統預設。这种做法直接讀取外部資源檔,在執行階段透過程式碼替換介面背景。優點是皮膚格式定義隨意,zip或自訂格式都行,只要程式能解析;缺點是需要讀取並解析檔案,效率上會打折扣。
3) 皮膚資源存在於主程式之類的APK中,也就是實現了APK的拆分,類似瀏覽器與外掛的關係。當考量到應用需要擴充時,就該採用這種方式,而且它不只體現在換膚的APK上。典型應用:手機QQ的換膚實現。QQ的皮膚是一個無界面APK,其資源命名與主程式一致,透過主程式與皮膚程式共享進程來存取皮膚資源,執行時由程式碼明確指定皮膚。缺點是每個Activity都要加入判斷皮膚的複雜邏輯;優點是效率較快,且讓應用具備良好的擴充性、降低耦合度,其他類似的擴充功能都能用這種方式實現。
4) 官方提供皮膚製作工具或方法,用戶可自製皮膚:
大致分為兩種情況:
a. 應用主列表為一個GridView,用戶在設定中選擇背景顏色和按鈕風格,直接替換即可。
b. 另一種是提供帶向導的可視化工具,用戶只需找些圖片、改改字型就能完成替換,還能上傳自製皮膚供他人下載。這類皮膚通常打包為.zip格式,副檔名可依公司需求自訂——例如墨跡天氣的皮膚副檔名是mja,搜狗輸入法是sga,本質上都是zip,後續套用流程與第二種方式類似。優點是能提升用戶參與感、自由度更高,讓用戶依喜好定製軟體皮膚。
5) 改寫SDK的Resource類:
這裡提供一些思路:在Android系統中,資源主要指圖片和MP3類型的檔案,也涵蓋使用者UI包含的所有元素。Google在設計Android時,將UI介面與邏輯程式碼分開:介面透過XML描述,具體邏輯則由程式碼實現;前端負責HTML與CSS的設計與架構,後端則專注於JSP與Java的執行。
資源存取在Android效能架構中處於什麼地位?開發時經常會用到Framework提供的Framework.jar與Framework-res.apk,以及與核心資源相關的「Resource Manager」檔案系統。
APK本身就是一種簡單的檔案格式,也是一個壓縮檔。解壓後可以看到APK的原始資料Meta INF、Manifest以及RES目錄;部分含圖片資源的應用,釋放時也會用到Layout。
安裝時,系統會把檔案取出、解壓後放到Dalvik Cache中,該目錄下有許多dex檔,用戶開啟應用時系統會自動載入對應的類。載入過程中,系統若需存取APK就要先解壓,這通常會拖慢效率;而把dex檔放進Dalvik Cache,則能大幅提升載入效率。
每個進程都有一份Framework的共享類和共享資源,但實體記憶體中的系統級資源只有一份。Framework類和資源是唯讀的,加上Android設計之初並沒有硬碟的虛擬記憶體與換入換出機制,因此節省記憶體空間是極其重要的工作。
用應用程式內建資源實現換膚
透過內建資源換膚的典型應用是QQ空間。適用場景為:應用體積不大、頁面較少、風格相對簡單,通常只需實現部分資源或背景的更換。
實現思路如下:
1. 把幾套皮膚放進res/drawable目錄,用SharedPreferences記錄當前皮膚的資源id,程式啟動時載入Activity背景。
2. 核心邏輯集中在皮膚管理器SkinManager類中,將皮膚資源的ID加入集合,由該類統一調度換膚——初始化皮膚、取得當前皮膚標識,以及替換具體對應資源。
接下來看效果圖:


內建皮膚的實現相對簡單,下面直接上程式碼:
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.tony.skindemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name="com.tony.skindemo.SkinDemoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
版面配置檔main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:textColor="#ff00ff" android:text="程序皮膚更換" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <RadioGroup android:id="@+id/skin_options" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:layout_weight="1" android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風格1" /> <RadioButton android:layout_weight="1" android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風格2" /> <RadioButton android:layout_weight="1" android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風格3" /> <RadioButton android:layout_weight="1" android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風格4" /> <RadioButton android:layout_weight="1" android:id="@+id/radioButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風格5" /> </RadioGroup> </LinearLayout>
程式主Activity
package com.tony.skindemo; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class SkinDemoActivity extends Activity { private SkinSettingManager mSettingManager; private RadioButton radioButton1; private RadioButton radioButton2; private RadioButton radioButton3; private RadioButton radioButton4; private RadioButton radioButton5; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 取消標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 完成窗體的全屏顯示 // 取消掉狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); // 初始化皮膚 mSettingManager = new SkinSettingManager(this); mSettingManager.initSkins(); //通過單選按鈕設置皮膚(可自定義更換的方式,如導航欄,也可以加上預覽功能,此處不再實現) radioButton1 = (RadioButton) findViewById(R.id.radioButton1); radioButton2 = (RadioButton) findViewById(R.id.radioButton2); radioButton3 = (RadioButton) findViewById(R.id.radioButton3); radioButton4 = (RadioButton) findViewById(R.id.radioButton4); radioButton5 = (RadioButton) findViewById(R.id.radioButton5); RadioGroup radioGroup = (RadioGroup) findViewById(R.id.skin_options); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton1: mSettingManager.changeSkin(1); break; case R.id.radioButton2: mSettingManager.changeSkin(2); break; case R.id.radioButton3: mSettingManager.changeSkin(3); break; case R.id.radioButton4: mSettingManager.changeSkin(4); break; case R.id.radioButton5: mSettingManager.changeSkin(5); break; default: break; } } }); } // 這裡為了簡單實現,實現換膚 public boolean onTouchEvent(MotionEvent event) { mSettingManager.toggleSkins(); return super.onTouchEvent(event); } } 皮膚管理器:
import android.app.Activity; import android.content.SharedPreferences; /** * 皮膚管理器 * @author tony * */ public class SkinSettingManager { public final static String SKIN_PREF = "skinSetting"; public SharedPreferences skinSettingPreference; private int[] skinResources = { R.drawable.default_wallpaper, R.drawable.wallpaper_c,R.drawable.wallpaper_d,R.drawable.wallpaper_f, R.drawable.wallpaper_g }; private Activity mActivity; public SkinSettingManager(Activity activity) { this.mActivity = activity; skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 3); } /** * 獲取當前程序的皮膚序號 * * @return */ public int getSkinType() { String key = "skin_type"; return skinSettingPreference.getInt(key, 0); } /** * 把皮膚序號寫到全局設置裡去 * * @param j */ public void setSkinType(int j) { SharedPreferences.Editor editor = skinSettingPreference.edit(); String key = "skin_type"; editor.putInt(key, j); editor.commit(); } /** * 獲取當前皮膚的背景圖資源id * * @return */ public int getCurrentSkinRes() { int skinLen = skinResources.length; int getSkinLen = getSkinType(); if(getSkinLen >= skinLen){ getSkinLen = 0; } return skinResources[getSkinLen]; } public void toggleSkins(){ int skinType = getSkinType(); if(skinType == skinResources.length - 1){ skinType = 0; }else{ skinType ++; } setSkinType(skinType); mActivity.getWindow().setBackgroundDrawable(null); try { mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); } catch (Throwable e) { e.printStackTrace(); } } /** * 用於初始化皮膚 */ public void initSkins(){ mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); } /** * 隨即切換一個背景皮膚 */ public void changeSkin(int id) { setSkinType(id); mActivity.getWindow().setBackgroundDrawable(null); try { mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); } catch (Throwable e) { e.printStackTrace(); } } } 這樣,透過程式內建皮膚的基本功能就完成了。若想在自己的應用中實現,仍需留意以下幾點(實現起來並不複雜,此處不再寫出具體實現):
1. 實現多個Activity的更換皮膚,需要自訂一個繼承自Application的MyApplication類,並加入activity集合屬性,用於儲存應用所有的Activity;再修改SkinManager,換膚時從Application取出該集合并遍歷替換。
2. 優化用戶體驗:透過導航欄進入更換皮膚介面,並加入預覽功能,確認修改配置後才真正完成換膚。
3. 加入style.theme等資源,實現更複雜的皮膚更換,具體做法與更換背景相同。
Android 中 Context 的深入理解與源碼剖析
本文以 Android 4.0 源碼為基準,探討 Context 的本質,以及 Application、Activity、Service 的例項化流程與其與 Contex
用 Design Support Library 實現 QQ 風格側滑與滑動菜單導航
2015 年 Google I/O 大會發布了更完善的 Material Design 規范,並推出了 Android Design Support Library。這個
SlidingMenu 側滑選單入門:四種導入方式與左右雙菜單實戰
側滑選單幾乎是 App 的標配,有些應用甚至左右兩側都放了菜單。開源庫 SlidingMenu(jfeinstein10/SlidingMenu)可以把這件事做得非常省事
釘釘簽到定位能不能改?新版本的實際限制說明
釘釘透過每日簽後來確認成員的所在位置,因此「定位能不能改」一直是熱門問題。答案在版本更迭之後已經變了,這裡把情況講清楚。 現況:新版已經堵住了這條路 本文撰寫時的釘