Create Your Own Android Live Wallpaper

One of the compelling user features of the Android platform is the ability to customize many aspects of the user experience. Among these is the ability to customize the home screen wallpaper with something other than a static image. A “live” wallpaper can contain images, animations, interactive features or whatever you, the developer, desires.

Live wallpapers were introduced in Android 2.1 (API Level 7). The live wallpaper we’re going to build in this tutorial is a horizontally animated star field. The star field effect uses fairly simple graphics and is not the focus of the article. We’ve implemented the effect using RenderScript, which means we’re targeting Android 3 (API Level 11) and later.

We’re also going to show how Android being an open platform can be extremely helpful to developers by leveraging some open source code helper files written by the Android team.

The code to use with this tutorial is available as an open source code project.

Getting Started with Android Live Wallpapers

Live wallpapers can be part of any existing Android app or simply standalone. Essentially, a live wallpaper is a special type of Android Service (android.service.wallpaper), and it includes a service tag entry in the Android manifest file. Additionally, a resource XML file is used to define how the wallpaper will appear in the system list. We’ll create a new application project from scratch, but you could just as easily add a live wallpaper to an existing app.

Start by creating a new project called StarsLiveWallpaper. In the manifest file, add a uses-feature for android.software.live_wallpaper. Set the minSdkVersion to 11 (the minimum API level required for using RenderScript). Now add a service entry for the live wallpaper we’ll create, such as:

    xml/stars_wallpaper" /> 

This service entry defines the wallpaper’s service information and the appropriate intent filter. Now create a new wallpaper configuration file called stars_wallpaper.xml, located in the res/xml directory. It will look like the following:

 xmlns:android="http://schemas.android.com/apk/res/android" android:thumbnail="@drawable/ic_launcher" android:description="@string/wallpaper_desc" />

You’ll need to create the appropriate string resources for use by these files (e.g. @string/wallpaper_name and @string/wallpaper_desc) as well. For the thumbnail, we’ve simply used the launcher icon resource, but you could also supply a custom drawable icon.

Creating the Service and Engine for Android Live Wallpapers

A live wallpaper is simply an implementation of WallpaperService. A WallpaperService implementation handles a single call, onCreateEngine(), and returns a valid instance of a WallpaperService.Engine. It’s the Engine class that does the work of drawing to the given Surface.

This is where some work has already been done for us. Check out the Android Open Source Project, where you’ll find some live wallpaper samples built using RenderScript. The Android team made a couple of helper files called RenderScriptScene and RenderScriptWallpaper. RenderScriptWallpaper, a WallpaperService, handles creating the WallpaperService.Engine class. It’s a template class that takes the RenderScriptScene class.

Sound complex? All we have to do is create a scene extending from RenderScriptScene, then return that from a super simple implementation of RenderScriptWallpaper. First, we need to get the source to these files. We’ve put them in the code download for this tutorial, but we originally got them from the git repository:

git clone https://android.googlesource.com/platform/packages/wallpapers/Basic.git

Then from the com.android.wallpaper package grab RenderScriptScene.java and RenderScriptWallpaper.java.

Now we can create our scene, StarsRS.java:

public class StarsRS extends RenderScriptScene { public static final int STAR_COUNT = 2500; private ScriptC_stars mScript;

public StarsRS(int width, int height) {
super(width, height);
}

@Override
protected ScriptC createScript() {
ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(
getRS());
pfb.setVaryingColor(true);
getRS().bindProgramFragment(pfb.create());

ScriptField_Star stars = new ScriptField_Star(mRS, STAR_COUNT);
Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
smb.addVertexAllocation(stars.getAllocation());
smb.addIndexSetType(Mesh.Primitive.POINT);
Mesh sm = smb.create();

mScript = new ScriptC_stars(mRS, getResources(), R.raw.stars);
mScript.set_starMesh(sm);
mScript.bind_star(stars);
mScript.invoke_initStars();
return mScript;
}

}

Here, we load the RenderScript that we created (found in stars.rs in the source code download, the graphics details of which are beyond the scope of this tutorial) and configure it. The script is then returned for use by RenderScriptWallpaper.

Next, we need to implement our wallpaper service:

public class StarsWallpaperService extends RenderScriptWallpaper { protected StarsRS createScene(int width, int height) { return new StarsRS(width, height); } } 

We did say it was a super simple implementation, right?

Although that’s all there is to creating the wallpaper, we’ve added a StarsLiveWallpaperActivity class as well as StarsTestView, used on that Activity. These additions provide a launcher icon and activity that will both show what the wallpaper will look like as well as provide a simple button to launch into the wallpaper settings. You can find the source code to these two classes in the sample code download.

26 thoughts on “Create Your Own Android Live Wallpaper

  1. Perfectly indited subject material, thanks for selective information. “The earth was made round so we would not see too far down the road.” by Karen Blixen.

    Like

  2. It’s actually a nice and useful piece of info. I am glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.

    Like

  3. I got what you intend, thankyou for putting up.Woh I am glad to find this website through google. “Spare no expense to make everything as economical as possible.” by Samuel Goldwyn.

    Like

  4. Just a smiling visitor here to share the love (:, btw outstanding design. “Justice is always violent to the party offending, for every man is innocent in his own eyes.” by Daniel Defoe.

    Like

  5. In between my breakfast and coffee break, I took a break from work and stumbled on this post. I found myself immediately intrigued by your proposition that it was that way. Good luck maintaining that in light of the newest models refuting such.

    Like

  6. I have learn some good stuff here. Definitely worth bookmarking for revisiting. I wonder how so much attempt you place to make this sort of excellent informative website.

    Like

  7. Right after study a handful with the content material inside your internet site now, and that i genuinely such as your method of blogging. I bookmarked it to my bookmark internet internet site list and are checking back soon. Pls look into my website as effectively and tell me what you believe.

    Like

  8. I truly enjoy studying on this website, it has got excellent posts. “Beware lest in your anxiety to avoid war you obtain a master.” by Demosthenes.

    Like

  9. Greetings! I’ve been reading your web site for a long time now and finally got the courage to go ahead and give you a shout out from Huffman Tx! Just wanted to mention keep up the great work!

    Like

  10. i bookmarked you in my browser admin thank you so much i will likely be trying to find your up coming posts

    Like

  11. Excellent goods from you, man. I have keep in mind your stuff prior to and you’re just too fantastic. I really like what you’ve acquired here, really like what you are saying and the way in which through which you assert it. You’re making it entertaining and you still care for to stay it sensible. I can’t wait to learn much more from you. This is really a wonderful website.

    Like

  12. Heya are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any html coding expertise to make your own blog? Any help would be really appreciated!

    Like

  13. Thanks for some other magnificent post. Where else may just anybody get that type of information in such a perfect means of writing? I have a presentation subsequent week, and I’m on the look for such information.

    Like

  14. Wohh just what I was looking for, regards for posting . “Arguments are like fire-arms which a man may keep at home but should not carry about with him.” by Samuel Butler.

    Like

  15. I haven’t checked in here for some time since I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend 🙂

    Like

  16. I was reading through some of your content on this site and I conceive this website is real informative! Retain posting.

    Like

  17. Merely wanna input on few general things, The website design is perfect, the subject matter is very fantastic : D.

    Like

  18. I’ve been absent for some time, but now I remember why I used to love this website. Thanks, I’ll try and check back more often. How frequently you update your site?

    Like

  19. Amazing blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your theme. Thanks a lot

    Like

  20. Keep up the great work, I read few articles on this web site and I think that your website is very interesting and holds circles of wonderful information.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.