How does android make dynamic wallpaper run?

For Android 2. 1, the addition of dynamic wallpaper adds a better dynamic effect to the Android desktop. How to develop an Android dynamic desktop? The following Android 123 gives you a detailed step to create your own dynamic wallpaper.

1. First, I use Eclipse to create a standard Android project. Here we use cn.com.android123.cwj as the package name, then go to the /res/ folder of the project and delete the layout folder. Of course, the main.xml in it will also be deleted. For dynamic wallpaper, the traditional layout file is unnecessary.

2. Similar to AppWidget, we can add the setting interface of dynamic wallpaper. We create a new folder named xml in the /res/ folder, and a new utf8-encoded xml file named livewallpaper.xml, with the following contents.

& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt

& lt wallpaper xmlns: Android = ".com.android123.cwj.livewallpaper settings"

Android:thumbnail = " @ drawable/icon "/& gt;

Here, you can see that the node above is called wallpaper, and the set interface is the activity of cn.com.android123.cwj.livewallpapersettings. When adding dynamic wallpaper, the file /res/drawable/icon will be displayed. At the same time, we will create an xml file for the layout of LiveWallpaperSettings, named livewallpaper_settings.xml, and the content is

& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt

& ltpreference screen xmlns:Android = "/apk/RES/Android "

Android:title = " @ string/live wallpaper _ settings "

Android:key = " live wallpaper _ settings " & gt;

& lt list preferences

Android:key = " live wallpaper _ test pattern "

Android:title= "title "

Android:summary= "simple description"

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= "dynamic description"

Android:title= "dynamic title"

Android:summaryOn= "dynamic test"

Android:summary off = " static test "/& gt;

& lt/preference screen & gt;

3. Create a class named LiveWallpaper as the main class of dynamic wallpaper and inherit it from the parent class of WallpaperService. Here, we can develop it just like writing a standard Android service.

4. The new class LiveWallpaperSettings inherits PreferenceActivity to implement our setting interface. The code is as follows.

Implementation of public class LiveWallpaperSettings extending PreferenceActivity

SharedPreferences。 onsharepreferencechangelistener {

@ Overlay

Protected void onCreate {

super . oncreate(icicle);

getPreferenceManager()。 setSharedPreferencesName(

Live wallpaper. Share _ Preference _ Name);

addPreferencesFromResource(r . XML . live wallpaper _ settings);

getPreferenceManager()。 getSharedPreferences()

. registersonsharedpreferencechangelistener(this);

}

@ Overlay

Protected void onResume() {

super . on resume();

}

@ Overlay

Protected void onDestroy() {

getPreferenceManager()。 getSharedPreferences()

. unregistronsharepreferencechangelistener(this);

super . on destroy();

}

Common void (SharedPreferenceChanged preferences,

String key) {

}

}

Meanwhile, the following code is still added to androidmanifest.xml

& lt Active Android: label = "@ string/livewallpaper _ settings"

android:name= "。 Live wallpaper settings "

Android:Theme = "@ Android:style/ theme. Lighting. Wallpaper Settings "

android:exported="true "

Android:icon = " @ drawable/icon " & gt;

& lt/activity & gt;

5. Because the Android dynamic wallpaper was added after the API level of 2. 1 was 7, we set the minSDK, which required the device to support the dynamic wallpaper, so we added it to androidmanifest.xml

& ltuses-SDK Android:minSdkVersion = " 7 "/& gt;

& ltuses-feature Android:name = " Android . software . live _ wallpaper "/& gt;

6. For the array used in the ListPreference and the color array involved in the code, we create a file named testpatterns.xml in the /res/values/ folder, and its contents are as follows.

& lt? Xml version =" 1.0 "encoding ="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 "color bar16x9" < /item & gt;

< item & gt "color bar 4x3" & lt/item & gt;;

< item > "ebu color bar" ;

& lt/string-array & gt;

& ltstring-array name = " live wallpaper _ test pattern _ prefix " >

& ltitem & gt" smpte " & lt/item & gt;

< item > "bar" ;

& 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 13 13 13 & lt; /item & gt;

& ltitem & gt0x ffc 000 c 0 & lt; /item & gt;

& ltitem & gt0x ff 13 13 13 & lt; /item & gt;

& ltitem & gt0 xff 00 c0c 0 & lt; /item & gt;

& ltitem & gt0x ff 13 13 13 & lt; /item & gt;

& ltitem & gt0x ffc 0 c 0 c 0 & lt; /item & gt;

& ltitem & gt0x ff 002 14C & lt; /item & gt;

& ltitem & gt0xFFFFFFFF & lt/item & gt;

& ltitem & gt0xFF32006A & lt/item & gt;

& ltitem & gt0x ff 13 13 13 & lt; /item & gt;

& ltitem & gt0xFF090909 & lt/item & gt;

& ltitem & gt0x ff 13 13 13 & lt; /item & gt;

& ltitem & gt0x ff 1D 1D 1D & lt; /item & gt;

& ltitem & gt0x ff 13 13 13 & 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;

reprint