Android动态壁纸 Live Wallpaper


   动态壁纸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.   
  7.     <uses-sdk android:minSdkVersion="7" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/icon"  
  11.         android:label="@string/app_name" >  
  12.             <service  
  13.          android:label="@string/app_name"  
  14.          android:permission="android.permission.BIND_WALLPAPER"   
  15.          android:name=".LiveWallpaper">  
  16.          <intent-filter>  
  17.              <action android:name="android.service.wallpaper.WallpaperService" />  
  18.          </intent-filter>  
  19.          <meta-data android:name="android.service.wallpaper"   
  20.                     android:resource="@xml/livewallpaper" />              
  21.        </service>  
  22.         <activity android:name=".LiveWallpaperSettings"   
  23.         android:label="@string/wallpaper_settings"   
  24.         android:theme="@android:style/Theme.Light.WallpaperSettings"  
  25.         android:exported="true">  
  26.     </activity>  
  27.     </application>  
  28. </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.      />  
  30.         <EditTextPreference  
  31.    android:key="@string/wordCount"  
  32.    android:title="一列显示的字符数"  
  33.    android:summary="请输入数字"  
  34.    android:dialogTitle="一列显示的字符数(请输入数字)"  
  35.    android:digits="0123456789"   
  36.    />  
  37.    <PreferenceCategory  
  38.    android:title="恢复默认设置"  
  39.    >  
  40.    <CheckBoxPreference  
  41.    android:key="@string/reset"  
  42.    android:title="恢复默认设置"  
  43.    android:summaryOn="恢复默认设置"  
  44.    android:summaryOff="恢复默认设置"  
  45.    android:defaultValue="false"  
  46.    ></CheckBoxPreference>  
  47.    </PreferenceCategory>    
  48.   
  49. </PreferenceScreen>  

这是一个我自己编写的简单动态壁纸效果图:

想了解具体实现细节的同学,可以下载本文工程。

下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/4月/30日/Android动态壁纸 Live Wallpaper/

相关内容

    暂无相关文章