Starting with Android 2.1 (API Level 7), users can now enjoy live wallpapers — richer, animated, interactive backgrounds — on their home screens. A live wallpaper is very similar to a normal Android application and has access to all the facilities of the platform: SGL (2D drawing), OpenGL (3D drawing), GPS, accelerometers, network access, etc. The live wallpapers included on Nexus One demonstrate the use of some of these APIs to create fun and interesting user experiences. For instance, the Grass wallpaper uses the phone’s location to compute sunrise and sunset times in order to display the appropriate sky.

Creating your own live wallpaper is easy, especially if you have had previous experience with SurfaceView or Canvas. To learn how to create a live wallpaper, you should check out the CubeLiveWallpaper sample code.

More after the break.

In terms of implementation, a live wallpaper is very similar to a regular Android service. The only difference is the addition of a new method, onCreateEngine(), whose goal is to create a WallpaperService.Engine. The engine is responsible for handling the lifecycle and drawing of a wallpaper. The system provides a surface on which you can draw, just like you would with a SurfaceView. Drawing a wallpaper can be very expensive so you should optimize your code as much as possible to avoid using too much CPU, not only for battery life but also to avoid slowing down the rest of the system. That is also why the most important part of the lifecycle of a wallpaper is when it becomes invisible. When invisible, such as when the user launches an application that covers the home screen, a wallpaper must stop all activity.

Trackback: Android Developer Website

Be Sociable, Share!
  • Archives

  • Categories