Download the source code: http://www.rajeeshcv.com/download/LiveAquariumWallpaper.zip
Android package: http://www.rajeeshcv.com/download/LiveAquariumWallpaper.apk (I have only tested this in the SDK simulator and haven’t considered all the screen sizes, so may find some UI glitches)
Few weeks ago I started learning Android programming , so this article is an outcome of that out-side office study :).
Here I will be explaining – how to create a live wallpaper which looks like an aquarium with fishes swimming across the screen. The fish animation is done using sprite technique.
Courtesy :
- Fish sprite used here is from a code project article - http://www.codeproject.com/KB/GDI-plus/LovelyGoldFishDeskPet.aspx
- Creating animation using sprites - http://www.droidnova.com/2d-sprite-animation-in-android,471.html
Lets get started….
Starts by creating new Android project in eclipse (I am not familiar with any other IDEs for Android development :) ). Now create a class for your live wallpaper service, I called it as
AquariumWallpaperService, then instantiate the
AquariumWallpaperEngine. This engine is responsible for creating the actual
Aquarium class which does all the rendering logic. It also controls the flow of
Aquarium based Surface callbacks
Below is the code for
AquariumWallpaperService
public class AquariumWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new AquariumWallpaperEngine();
}
class AquariumWallpaperEngine extends Engine{
private Aquarium _aquarium;
public AquariumWallpaperEngine() {
this._aquarium = new Aquarium();
this._aquarium.initialize(getBaseContext(), getSurfaceHolder());
}
@Override
public void onVisibilityChanged(boolean visible) {
if(visible){
this._aquarium.render();
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
this._aquarium.start();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this._aquarium.stop();
}
}
}
Aquarium class wraps all the rendering logic, as well as creating the fishes. This also starts a thread which is responsible for updating the view.
Read more...