歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android動態壁紙 Live Wallpaper

Android動態壁紙 Live Wallpaper

日期:2017/3/1 10:23:46   编辑:Linux編程
動態壁紙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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:Android="http://schemas.android.com/apk/res/android"
  3. package="com.njue.livewallpaper"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk android:minSdkVersion="7" />
  7. <application
  8. android:icon="@drawable/icon"
  9. android:label="@string/app_name" >
  10. <service
  11. android:label="@string/app_name"
  12. android:permission="android.permission.BIND_WALLPAPER"
  13. android:name=".LiveWallpaper">
  14. <intent-filter>
  15. <action android:name="android.service.wallpaper.WallpaperService" />
  16. </intent-filter>
  17. <meta-data android:name="android.service.wallpaper"
  18. android:resource="@xml/livewallpaper" />
  19. </service>
  20. <activity android:name=".LiveWallpaperSettings"
  21. android:label="@string/wallpaper_settings"
  22. android:theme="@android:style/Theme.Light.WallpaperSettings"
  23. android:exported="true">
  24. </activity>
  25. </application>
  26. </manifest>

/res/XML/liveWallpaper.XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <wallpaper
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:thumbnail="@drawable/floewr1"
  5. android:description="@string/description"
  6. android:settingsActivity="com.njue.livewallpaper.LiveWallpaperSettings"
  7. />

參數設置界面的布局代碼settings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:title="@string/settings">
  5. <ListPreference
  6. android:key="@string/leaf1Count"
  7. android:title="@string/settings_title"
  8. android:summary="@string/settings_summary"
  9. android:entries="@array/entries"
  10. android:entryValues="@array/values" />
  11. <ListPreference
  12. android:key="@string/flower1Count"
  13. android:title="@string/settings_title1"
  14. android:summary="@string/settings_summary1"
  15. android:entries="@array/entries"
  16. android:entryValues="@array/values" />
  17. <ListPreference
  18. android:key="@string/flower2Count"
  19. android:title="@string/settings_title2"
  20. android:summary="@string/settings_summary2"
  21. android:entries="@array/entries"
  22. android:entryValues="@array/values" />
  23. <EditTextPreference
  24. android:key="@string/inputText"
  25. android:title="輸入你喜歡的文字"
  26. android:summary="點擊輸入"
  27. android:dialogTitle="輸入文字設置"
  28. />
  29. <EditTextPreference
  30. android:key="@string/wordCount"
  31. android:title="一列顯示的字符數"
  32. android:summary="請輸入數字"
  33. android:dialogTitle="一列顯示的字符數(請輸入數字)"
  34. android:digits="0123456789"
  35. />
  36. <PreferenceCategory
  37. android:title="恢復默認設置"
  38. >
  39. <CheckBoxPreference
  40. android:key="@string/reset"
  41. android:title="恢復默認設置"
  42. android:summaryOn="恢復默認設置"
  43. android:summaryOff="恢復默認設置"
  44. android:defaultValue="false"
  45. ></CheckBoxPreference>
  46. </PreferenceCategory>
  47. </PreferenceScreen>

這是一個我自己編寫的簡單動態壁紙效果圖:

想了解具體實現細節的同學,可以下載本文工程。

下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/4月/30日/Android動態壁紙 Live Wallpaper/

Copyright © Linux教程網 All Rights Reserved