Glow Live Wallpaper

Building a Custom Android Live Wallpaper

Android live wallpapers are a great way to become familiar with web development for the Android platform. They do not require any interactivity, or user input, and so most of the application logic is usually based on predefined sequences or sculpted randomization.

One technique that we will be using is the prehistoric pong bounce. This is where we increment a position with a speed until it reaches a wall, then we reverse the speed, and continue to the next wall.

There are many resources on Android live wallpapers, including Creating Live Wallpapers on Android by Mobile Tuts and A Bare-bones Live Wallpaper Template for Android by Tech Republic. To get some background, or a second opinion, don’t hesitate to look around.

Most of the code below is the same as other Android live wallpaper templates. The responsibility of GlowService is to create a GlowEngine instance. The responsibility of GlowEngine is to handle draw calls.

To start, we will write a skeleton class for the wallpaper service:

Unlike other examples, we are going to delegate the draw method to a completely different class, so we need to consider how to write it. In order to add some degree of animation to the wallpaper, we are going to use a simple radial gradient

What we’ve done is make a simple radial gradient paint, whose center moves and bounces off the walls, much like classic screen savers. The technique is very simple, when the center gets close to the walls, then we reverse the direction it travels, and keep it in the GlowDrawable object. At this point, we might want to change things, like pick different colors, a blue perhaps, or we can animate between orange and blue, or make the color based on the X or Y coordinate, the possibilities are endless.

In order for our app to display in the “live wallpaper” section of the settings, now we need to make sure that the android.service.wallpaper.WallpaperService intent is properly bound in the Manifest.

As you can see, we need a file called “res/xml/wallpaper.xml” with more metadata about the wallpaper. This must look something like the following

Having all of our sources ready, we can try it out! Now that you’ve made a live wallpaper, you might try being a little adventurous, and replace RadialGradient with something else, perhaps LinearGradient or some other Shader. Another option would be to use shapes other than rect (our entire canvas is a rect). We could have used multiple rects and have them bounce off the sides with the technique we used for the center of the RadialGradient.

We have covered how to setup a WallpaperService and how to instantiate Engines which render the live wallpaper. We have shown how to setup the XML files required for Android to recognize it as a live wallpaper. We would also like to remind that you can enjoy learning Android app development!

Like this post? Please share it! Then follow us on Twitter – @thorntech – and sign up for our email list below for future updates.