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.

Advertisements
Comments
  1. Zane Sutter says:

    I dugg some of you post as I thought they were extremely helpful very helpful

    Like

  2. e-tupakka suomesta says:

    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

  3. Imelda Pinkos says:

    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

  4. game change says:

    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

  5. music licensing deals says:

    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

  6. broadjam scam says:

    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

  7. cheap vacation ideas says:

    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

  8. Sexcam says:

    I went over this internet site and I believe you have a lot of good info, saved to fav (:.

    Like

  9. hybrid cars for sale says:

    Very interesting information!Perfect just what I was looking for!

    Like

  10. scrapebox list says:

    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

  11. music publishing license says:

    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

  12. basketball tv philippines schedule channel says:

    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

  13. kore dizileri izle says:

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

    Like

  14. Raul Poska says:

    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

  15. warts incubation period says:

    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

  16. accelerated nursing programs says:

    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

  17. four micro onde says:

    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

  18. eliminating credit card bills says:

    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

  19. Georgann says:

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

    Like

  20. music mastering equipment says:

    There is no longer an excuse for such poor grammer. Try again hot shot!

    Like

  21. guaranteed facebook fans says:

    A rolling stone gathers no moss

    Like

  22. Eva Angelina says:

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

    Like

  23. coach credit card holder says:

    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

  24. 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

  25. URL Indexer says:

    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

  26. Hi there, You’ve done a great job. I’ll definitely digg it and personally recommend to my friends.
    I’m confident they’ll be benefited from this web site.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s