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.
Fish sprite used here is from a code project article
Lets get started…
Creating a 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 which 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
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.
Here is the code for
All the renderable object in the aquarium must implement an interface
Renderable, which has got one single method called
This interface helps to render an object other than a fish, like plants etc… in future. I have created another abstract class which has common functionalities like changing the position and direction of a fish after a particular interval and called it as
AquaticAnimal, this is because I could create specific fishes which just differ by it’s look by extending from this class.
The sprite animation is moved into a specific class
Now to the final bit, that is creating a fish. you might have noticed that I have created an object of
ClownFishin the Aquarium class.
ClownFish is just a derived class from
AquaticAnimal, with a specific sprite image. So if you have sprite images for a shark, you could simple extend a new class for a shark with that image.
Please feel free to go through the code, If I haven’t explained clearly. Hope this article helped some way or other.
Happy new year!!!
Full source code is available here