Home How to create Live Wallpaper

postheadericon How to create a Live Wallpaper

One of the new Android 2.1 features was the Live Wallpaper. This article describes the basis and provides a sample Live Wallpaper.

You start with subclassing the android.service.wallpaper.WallpaperService class and has one member, a android.os.Handler. This handler is used for scheduling the 'work' and is discussed in more detail later.

public class SampleLiveWallpaperService extends WallpaperService {
private Handler handler = null;

public Engine onCreateEngine() {
return new SampleLiveWallpaperEngine();
}
}

The onCreateEngine method must be implemented. We will do this later. Now, we need an instance of the android.service.wallpaper.WallpaperService.Engine class. It's defined as an inner class of the android.service.wallpaper.WallpaperService class. We will do the same by creating an inner class SampleLiveWallpaperService extending the Engine class. We override the onDestroy and onVisibilityChanged methods to act on changes of the visibility of the Live Wallpaper. Furthermore, we create a Runnable as a member of the engine which will do the actual work. The engine also has a boolean member holding the visibility status.

private class SampleLiveWallpaperEngine extends Engine {
private boolean visible = false;

private final Runnable runnableSomething = new Runnable() {
public void run() {
drawSomething();
}
};
public void onDestroy() {
super.onDestroy();

handler.removeCallbacks(runnableSomething);
}
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);

this.visible = visible;

if (visible) {
drawSomething();
}
else {
handler.removeCallbacks(runnableSomething);
}

}

The onCreateEngine method can be implemented now. We return an new instance of the SampleLiveWallpaperEngine class.

public Engine onCreateEngine() {
return new SampleLiveWallpaperEngine();
}

We need to make use of some kind of scheduling of the work. Otherwise other processes wouldn't get any processing time. We do this with the android.os.Handler class. With the Handler.postDelayed(runnableSomething, 25) call we put the unit of work on the message queue. It is scheduled to run after 25 milliseconds. You can play around with the delay for the most optimal behavior. The drawing is performed by retrieving the canvas and drawing something to it. The canvas is retrieved via the android.view.SurfaceHolder class. First, we lock the canvas by calling SurfaceHolder.lockCanvas() and if we are done with our drawing we unlock it by calling SurfaceHolder.unlockCanvasAndPost(Canvas canvas). We need to pass the same canvas retrieved by the SurfaceHolder.lockCanvas() call.

private void drawSomething() {
final SurfaceHolder holder = getSurfaceHolder();

Canvas canvas = null;

try {
canvas = holder.lockCanvas();

if (canvas != null) {
canvas.drawARGB(200, colors[0], colors[1], colors[2]);
}

updateColors(colors);
}
finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}

// Reschedule the next redraw.
handler.removeCallbacks(runnableSomething);

if (visible) {
// Play around with the delay for an optimal result.
handler.postDelayed(runnableSomething, 25);
}
}

Within the AndroidManifest.xml file we need to set a permission (android:permission = "android.permission.BIND_WALLPAPER") and list it as a feature (<uses-feature android:name = "android.software.live_wallpaper" />) before you can use the Live Wallpaper. The full source code with a working sample can be downloaded below and is created for Android 2.1 and above. Have fun and don't forget to like, plus or retweet if you like this blog.

The Live Wallpaper is used within the following apps:
Bubbles·Live Wallpaper
Hearts Live Wallpaper
Lissajous Live Wallpaper
PCB·Live Wallpaper
Rose Petals·Live Wallpaper
Spyro·Live Wallpaper
Stars·Live Wallpaper

Attachments:
Download this file (SampleLiveWallpaper.zip)SampleLiveWallpaper.zip13 Kb