android如何让动态壁纸动态壁纸运行?
1.首先,我使用Eclipse创建一个标准的Android项目。这里我们用cn.com.android123.cwj作为包名,然后转到项目的/res/文件夹,删除布局文件夹。当然里面的main.xml也会被删除。对于动态壁纸,传统的布局文件是不必要的。
2.类似于AppWidget,我们可以添加动态壁纸的设置界面。我们在/res/文件夹中创建一个名为xml的新文件夹,以及一个名为livewallpaper.xml的新的utf8编码的xml文件,其内容如下
& lt?xml版本="1.0 "编码="utf-8 "?& gt
& lt壁纸xmlns:Android = " . com . Android 123 . cwj . live wallpaper settings "
Android:thumbnail = " @ drawable/icon "/& gt;
这里可以看到上面的节点叫做壁纸,设置的界面是cn . com . Android 123 . cwj . livewallpapersettings的活动,添加动态壁纸时显示文件/res/drawable/icon。同时,我们将为LiveWallpaperSettings的布局创建一个xml文件,命名为livewallpaper_settings.xml,内容是
& lt?xml版本="1.0 "编码="utf-8 "?& gt
& ltpreference screen xmlns:Android = "/apk/RES/Android "
Android:title = " @ string/live wallpaper _ settings "
Android:key = " live wallpaper _ settings " & gt;
& lt列表首选项
Android:key = " live wallpaper _ test pattern "
Android:title= "title "
Android:summary= "简单描述"
Android:entries = " @ array/live wallpaper _ test pattern _ names "
Android:entry values = " @ array/live wallpaper _ test pattern _ prefix "/& gt;
& ltcheckbox preference Android:key = " live wallpaper _ movement "
Android:summary= "动态描述"
Android:title= "动态标题"
Android:summaryOn= "动态测试"
Android:summary off = " static test "/& gt;
& lt/preference screen & gt;
3.创建一个名为LiveWallpaper的类作为动态壁纸的主类,并从WallpaperService的父类继承。在这里,我们可以像编写标准的Android服务一样开发它。
4.新的类LiveWallpaperSettings继承了PreferenceActivity来实现我们的设置接口。代码如下。
公共类LiveWallpaperSettings扩展PreferenceActivity实现
SharedPreferences。onsharepreferencechangelistener {
@覆盖
受保护的void onCreate(捆绑冰柱){
super . oncreate(icicle);
getPreferenceManager()。setSharedPreferencesName(
现场壁纸。共享_首选项_名称);
addPreferencesFromResource(r . XML . live wallpaper _ settings);
getPreferenceManager()。getSharedPreferences()
。registersonsharedpreferencechangelistener(this);
}
@覆盖
受保护的void onResume() {
super . on resume();
}
@覆盖
受保护的void onDestroy() {
getPreferenceManager()。getSharedPreferences()
。unregistronsharepreferencechangelistener(this);
super . on destroy();
}
SharedPreferenceChanged上的公共void(shared preferences shared preferences,
字符串密钥){
}
}
同时,以下代码仍然添加到androidmanifest.xml中
& lt活动Android:label = " @ string/live wallpaper _ settings "
android:name= "。live壁纸设置"
Android:Theme = " @ Android:style/主题。灯光.壁纸设置"
android:exported="true "
Android:icon = " @ drawable/icon " & gt;
& lt/activity & gt;
5.因为Android动态壁纸是在2.1的API级别为7之后才添加的,所以我们设置了minSDK,需要设备支持动态壁纸,所以我们将其添加到androidmanifest.xml中
& ltuses-SDK Android:minSdkVersion = " 7 "/& gt;
& ltuses-feature Android:name = " Android . software . live _ wallpaper "/& gt;
6.对于ListPreference中使用的数组和代码中涉及的颜色数组,我们在/res/values/文件夹中创建一个名为testpatterns.xml的文件,其内容如下
& lt?xml版本="1.0 "编码="utf-8 "?& gt
& ltresources xmlns:xliff = " urn:oasis:names:TC:xliff:document:1.2 " & gt;
& ltstring-array name = " live wallpaper _ test pattern _ names " & gt;
& ltitem & gt"彩条16x 9 " & lt;/item & gt;
& ltitem & gt"色条4x3 " & lt/item & gt;
& ltitem & gt“EBU色条”& lt/item & gt;
& lt/string-array & gt;
& ltstring-array name = " live wallpaper _ test pattern _ prefix " >
& ltitem & gt" smpte " & lt/item & gt;
& ltitem & gt“酒吧”& lt/item & gt;
& ltitem & gt“ebu”& lt;/item & gt;
& lt/string-array & gt;
& ltinteger-array name="smptecolors " >
& ltitem & gt0xFF696969 & lt/item & gt;
& ltitem & gt0x ffc 1c 1c 1 & lt;/item & gt;
& ltitem & gt0x ffc 1c 100 & lt;/item & gt;
& ltitem & gt0 xff 00 c 1c 1 & lt;/item & gt;
& ltitem & gt0 xff 00 c 100 & lt;/item & gt;
& ltitem & gt0x ffc 100 c 1 & lt;/item & gt;
& ltitem & gt0x ffc 10000 & lt;/item & gt;
& ltitem & gt0x ff 0000 c 1 & lt;/item & gt;
& ltitem & gt0xFF696969 & lt/item & gt;
& ltitem & gt0xFF00FFFF & lt/item & gt;
& ltitem & gt0xFFFFFF00 & lt/item & gt;
& ltitem & gt0xFF052550 & lt/item & gt;
& ltitem & gt0xFF36056D & lt/item & gt;
& ltitem & gt0xFF0000FF & lt/item & gt;
& ltitem & gt0xFFFF0000 & lt/item & gt;
& ltitem & gt0x ffc 1c 1c 1 & lt;/item & gt;
& ltitem & gt0 xff 2 B2 B2B & lt;/item & gt;
& ltitem & gt0xFF050505 & lt/item & gt;
& ltitem & gt0xFFFFFFFF & lt/item & gt;
& ltitem & gt0xFF050505 & lt/item & gt;
& ltitem & gt0xFF000000 & lt/item & gt;
& ltitem & gt0xFF050505 & lt/item & gt;
& ltitem & gt0 xff 0a 0a 0a 0a & lt;/item & gt;
& ltitem & gt0xFF050505 & lt/item & gt;
& ltitem & gt0 xff 0 d0 d0 d & lt;/item & gt;
& ltitem & gt0xFF050505 & lt/item & gt;
& ltitem & gt0 xff 2 B2 B2B & lt;/item & gt;
& lt/integer-array & gt;
& ltinteger-array name="barscolors " >
& ltitem & gt0x ffc 0 c 0 c 0 & lt;/item & gt;
& ltitem & gt0x ffc 0 c 000 & lt;/item & gt;
& ltitem & gt0 xff 00 c0c 0 & lt;/item & gt;
& ltitem & gt0x ff 00 c 000 & lt;/item & gt;
& ltitem & gt0x ffc 000 c 0 & lt;/item & gt;
& ltitem & gt0xFFC00000 & lt/item & gt;
& ltitem & gt0x ff 0000 c 0 & lt;/item & gt;
& ltitem & gt0x ff 0000 c 0 & lt;/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& ltitem & gt0x ffc 000 c 0 & lt;/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& ltitem & gt0 xff 00 c0c 0 & lt;/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& ltitem & gt0x ffc 0 c 0 c 0 & lt;/item & gt;
& ltitem & gt0x ff 00214C & lt;/item & gt;
& ltitem & gt0xFFFFFFFF & lt/item & gt;
& ltitem & gt0xFF32006A & lt/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& ltitem & gt0xFF090909 & lt/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& ltitem & gt0x ff 1D 1D 1D & lt;/item & gt;
& ltitem & gt0x ff 131313 & lt;/item & gt;
& lt/integer-array & gt;
& ltinteger-array name="ebucolors " >
& ltitem & gt0xFFBFBFBF & lt/item & gt;
& ltitem & gt0xFFBFBF00 & lt/item & gt;
& ltitem & gt0xFF00BFBF & lt/item & gt;
& ltitem & gt0x ff 00 BF 00 & lt;/item & gt;
& ltitem & gt0xFFBF00BF & lt/item & gt;
& ltitem & gt0xFFBF0000 & lt/item & gt;
& ltitem & gt0xFF0000BF & lt/item & gt;
& ltitem & gt0xFF000000 & lt/item & gt;
& lt/integer-array & gt;
& lt/resources & gt;
重印