Since Android 2.1 has been added to the dynamic wallpaper, a lot of cow fork ah, beautiful wallpaper endless, looks like the old-fashioned Android phone can finally dazzle a bit.
Dynamic Wallpaper Basics
Production methods, online too much, although the basic is copied, in fact, from the development of the sample, I also put the main points in mind, each write new to the old project open look, what memory ...
XML file for the specified dynamic wallpaper in Res/xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<wallpaper xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:settingsactivity= "Xxx.yyy.LiveWallpaperSettings"
Android:thumbnail= "@drawable/icon"/>
Here is a description of your own icon and set activity.
There is also a set activity of the XML file, is the ordinary Preferencescreen, omitted.
To create a wallpaperservice subclass, you need to oncreateengine the method to return a drawing in a engine,engine, just like with Surfaceview.
public class Livewallpaper extends Wallpaperservice {
public static final String shared_prefs_name = "Setting_file_name";
@Override
public void OnCreate () {
Super.oncreate ();
}
@Override
public void OnDestroy () {
Super.ondestroy ();
}
@Override
Public Engine Oncreateengine () {
return new Testpatternengine ();
}
Class Testpatternengine extends Engine implements
Sharedpreferences.onsharedpreferencechangelistener {
Private final Handler Mhandler = new Handler ();
Private final Runnable Mdrawpattern = new Runnable () {
public void Run () {
Drawframe ();
}
};
Private Boolean mvisible;
Private Sharedpreferences mpreferences;
Testpatternengine () {
Mpreferences = LiveWallpaper.this.getSharedPreferences (
Shared_prefs_name, 0);
Mpreferences.registeronsharedpreferencechangelistener (this);
Onsharedpreferencechanged (mpreferences, NULL);
}
public void onsharedpreferencechanged (Sharedpreferences prefs,
String key) {
}
@Override
public void OnCreate (Surfaceholder surfaceholder) {
Super.oncreate (Surfaceholder);
}
@Override
public void OnDestroy () {
Super.ondestroy ();
Mhandler.removecallbacks (Mdrawpattern);
}
@Override
public void Onvisibilitychanged (Boolean visible) {
mvisible = visible;
if (visible) {
Drawframe ();
} else {
Mhandler.removecallbacks (Mdrawpattern);
}
}
@Override
public void onsurfacechanged (surfaceholder holder, int format,
int width, int height) {
super.onsurfacechanged (holder, format, width, height);
Drawframe ();
}
@Override
public void onsurfacecreated (Surfaceholder holder) {
super.onsurfacecreated (holder);
}
@Override
public void onsurfacedestroyed (Surfaceholder holder) {
super.onsurfacedestroyed (holder);
Mvisible = false;
Mhandler.removecallbacks (Mdrawpattern);
}
@Override
public void onoffsetschanged (float xoffset, float yoffset, float xstep,
float ystep, int xpixels, int ypixels) {
Drawframe ();
}
void Drawframe () {
Final Surfaceholder holder = Getsurfaceholder ();
Canvas c = null;
try {
c = Holder.lockcanvas ();
if (c!= null) {
Draw Something
}
finally {
if (c!= null)
Holder.unlockcanvasandpost (c);
}
Mhandler.removecallbacks (Mdrawpattern);
if (mvisible) {
Mhandler.postdelayed (Mdrawpattern, 1000/25);
}
}
}
}
There is also the implementation of the set activity:
public class Livewallpapersettings extends preferenceactivity
Implements Sharedpreferences.onsharedpreferencechangelistener {
@Override
protected void OnCreate (Bundle icicle) {
Super.oncreate (Icicle);
Getpreferencemanager (). Setsharedpreferencesname (Livewallpaper.shared_prefs_name);
Addpreferencesfromresource (r.xml.livewallpaper_settings);
Getpreferencemanager (). Getsharedpreferences ()
. Registeronsharedpreferencechangelistener (this);
}
@Override
protected void Onresume () {
Super.onresume ();
}
@Override
protected void OnDestroy () {
Getpreferencemanager (). Getsharedpreferences ()
. Unregisteronsharedpreferencechangelistener (this);
Super.ondestroy ();
}
public void onsharedpreferencechanged (
Sharedpreferences sharedpreferences, String key) {
}
}
Androidmanifest.xml must be included in the:
<service android:name=. Livewallpaper "android:label=" @string/app_name " android:icon=" @drawable/icon "
<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:label= "@string/livewallpaper_settings"
android: Name= ". Livewallpapersettings "
android:theme=" @android: style/theme.light.wallpapersettings
android:exported= "true"
android:icon= "@drawable/icon"
</activity>
<uses-sdk android:minsdkversion= "7"/>
<uses-feature android:name= "android.software.live_ Wallpaper "/>
"Uses-feature android:name=" Android.software.live_wallpaper "" This sentence should be used with caution! Google's electronic market will recognize this sentence, and then some can be used to filter out the machine, such as My broken machine, is not supported, upgrade to 2.1 supposedly can be used. I saw a lot of famous dynamic wallpaper on the market, did not add this sentence ~ ~
Also, if you want to use OpenGL es in a dynamic wallpaper, refer to this article: one or two things about using OpenGL es in Android.
Join AdMob Ads
Above all is nonsense, I want to say this time the topic is how to add AdMob advertisement in the Wallpaper setting interface, the thing that completes very much, always want to mean, add an advertisement is impossible thing.
Unlike the general activity, it is not possible to join the AdMob directly, even a layout is not, and directly added to the wallpaper is very bad, who also do not want a good background plug an ad. So the general dynamic wallpaper ads are added to the settings interface, so it is really not easy.
Generally there are two ways to AdMob Adview as a preference, directly add XML.
public class Admobpreference extends preference {
public Admobpreference ( Context: {
super (context, NULL);
}
& nbsp
Public admobpreference (context, AttributeSet attrs) {
Super (context, attrs);
}
@Override
protected View Oncreateview (viewgroup parent) {
//override the AdMob ad instead of a regular preference display< br> Layoutinflater inflater = (layoutinflater) getcontext (). Getsystemservice (Context.layout_inflater_service);
return inflater.inflate (r.layout.admob_preference, NULL);
}
}
Relative admob_preference configuration:
<?xml version= "1.0" encoding= "Utf-8"
<linearlayout xmlns:android= "http://schemas.android.com" /apk/res/android "
android:id=" @+id/ad_layout
android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"
<com.google.ads.adview
xmlns:ads= "http://schemas.android.com/apk/lib/com.google.ads"
android:id= "@+ Id/ad "
android:layout_width=" Fill_parent
android: layout_height= "Wrap_content"
ads.adsize= "BANNER"
ads:adunitid= "a14e51ca560d266"
ads: Loadadoncreate= "true"
/>
</linearlayout
Android Adview missing required XML attribute "Adsize"
Here is still some knowledge, online check there will be countless "adview missing required XML attribute ' adsize '" problem, is said to be the problem after 4.1.0, must write this (will be "http:// Schemas.android.com/apk/lib/com.google.ads "as a namespace, but not to join their own program in order to not make mistakes, at least we say so, unfortunately I do not, how to write all have this wrong, spent two hours to try a variety of writing, Or not!! Finally it was written in the code: (
However, it seems that the light is still not good, adview around there are white, looks like a preferenceactivity in the padding, not to go off, and finally use the way of the tab:
public class Settingstabactivity extends Tabactivity {
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.table_layout);
Tabhost tabhost = Gettabhost (); The Activity Tabhost
Tabhost.tabspec spec; Resusable Tabspec for each tab
Intent Intent; Reusable Intent for each tab
Create an Intent to the regular live wallpaper preferences activity
Intent = new Intent (). SetClass (this, livepapersettings.class);
Initialize a tabspec and set the intent
Spec = Tabhost.newtabspec ("Tabtitle"). SetContent (Intent);
Spec.setindicator ("Tabtitle");
Tabhost.addtab (spec);
Tabhost.setcurrenttab (0);
}
The corresponding layout:
<?xml version= "1.0" encoding= "Utf-8"?>
<tabhost xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:ads= "Http://schemas.android.com/apk/lib/com.google.ads"
Android:id= "@android: Id/tabhost" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<linearlayout android:orientation= "Vertical"
Android:id= "@+id/main_layout"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<com.google.ads.adview android:id= "@+id/ad"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Ads:backgroundcolor= "#000000"
Ads:primarytextcolor= "#FFFFFF"
Ads:secondarytextcolor= "#CCCCCC"
Ads.adsize= "BANNER"
Ads:adunitid= "a14e51ca560d266"
Ads:loadadoncreate= "true"/>
<tabwidget android:id= "@android: Id/tabs"
Android:layout_width= "Fill_parent" android:layout_height= "1DP"
android:visibility= "Invisible"/>
<framelayout android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"
android:padding= "1DP"/>
</LinearLayout>
</TabHost>
This is probably the case.
By the way, there's a two-hour problem that has been wasted, for the live wallpaper that have been followed, you have modified the setting activity, directly covering the installation is ineffective, point configuration or the presence of previous activity, found this before I did not know hundreds of times to revise the upload, all quickly hang up, Google how to consider?!