動態壁紙Live Wallpaper架構
組件類
說明
WallpaperService
實現動態壁紙的服務程序
WallpaperService.Engine
描繪動態壁紙的引擎
PreferenceActivity
動態壁紙的參數設置窗體
動態壁紙Live Wallpaper應用程序必須實現動態壁紙的服務程序WallpaperService和描繪動態壁紙的引擎WallpaperService.Engine,當你需要設置動態壁紙的參數來改變動畫的屬性時,必須提供設置參數的窗體。此時才需要實現動態壁紙的參數設置窗體PreferenceActivity。
咱們先來說一次簡單的步驟:
(1)建一個類繼承WallpaperService,比如說為LiveWallpaper.java
(2)然後在AndrodManifest.XML文件的<service>標簽內定義動態壁紙的服務程序LiveWallpaper.java和動態壁紙的資源來源“/res/XML/liveWallpaper.XML”
(3)還需要增加一個<Activity>標簽來設置動態壁紙參數設置程序HelloLiveWallpaperSetting.java,當然這個要去繼承PreferenceActivity
AndrodManifest.XML
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:Android="http://schemas.android.com/apk/res/android"
- package="com.njue.livewallpaper"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk android:minSdkVersion="7" />
-
- <application
- android:icon="@drawable/icon"
- android:label="@string/app_name" >
- <service
- android:label="@string/app_name"
- android:permission="android.permission.BIND_WALLPAPER"
- android:name=".LiveWallpaper">
- <intent-filter>
- <action android:name="android.service.wallpaper.WallpaperService" />
- </intent-filter>
- <meta-data android:name="android.service.wallpaper"
- android:resource="@xml/livewallpaper" />
- </service>
- <activity android:name=".LiveWallpaperSettings"
- android:label="@string/wallpaper_settings"
- android:theme="@android:style/Theme.Light.WallpaperSettings"
- android:exported="true">
- </activity>
- </application>
- </manifest>
/res/XML/liveWallpaper.XML
- <?xml version="1.0" encoding="UTF-8"?>
- <wallpaper
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:thumbnail="@drawable/floewr1"
- android:description="@string/description"
- android:settingsActivity="com.njue.livewallpaper.LiveWallpaperSettings"
- />
參數設置界面的布局代碼settings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:title="@string/settings">
- <ListPreference
- android:key="@string/leaf1Count"
- android:title="@string/settings_title"
- android:summary="@string/settings_summary"
- android:entries="@array/entries"
- android:entryValues="@array/values" />
- <ListPreference
- android:key="@string/flower1Count"
- android:title="@string/settings_title1"
- android:summary="@string/settings_summary1"
- android:entries="@array/entries"
- android:entryValues="@array/values" />
- <ListPreference
- android:key="@string/flower2Count"
- android:title="@string/settings_title2"
- android:summary="@string/settings_summary2"
- android:entries="@array/entries"
- android:entryValues="@array/values" />
- <EditTextPreference
- android:key="@string/inputText"
- android:title="輸入你喜歡的文字"
- android:summary="點擊輸入"
- android:dialogTitle="輸入文字設置"
-
- />
- <EditTextPreference
- android:key="@string/wordCount"
- android:title="一列顯示的字符數"
- android:summary="請輸入數字"
- android:dialogTitle="一列顯示的字符數(請輸入數字)"
- android:digits="0123456789"
- />
- <PreferenceCategory
- android:title="恢復默認設置"
- >
- <CheckBoxPreference
- android:key="@string/reset"
- android:title="恢復默認設置"
- android:summaryOn="恢復默認設置"
- android:summaryOff="恢復默認設置"
- android:defaultValue="false"
- ></CheckBoxPreference>
- </PreferenceCategory>
-
- </PreferenceScreen>
這是一個我自己編寫的簡單動態壁紙效果圖:
想了解具體實現細節的同學,可以下載本文工程。
下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/4月/30日/Android動態壁紙 Live Wallpaper/