April 19, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Create Your Own Android Live Wallpaper

  • January 25, 2012
  • By Shane Conder & Lauren Darcey
  • Send Email »
  • More Articles »

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:

<service
android:name="com.mamlambo.livewallpaper.stars.StarsWallpaperService"
android:label="@string/wallpaper_name"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>

<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/stars_wallpaper" />
</service>

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:

<?xml version="1.0" encoding="utf-8"?>
<wallpaper 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.


Tags: Android, Android development



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel