After a MIDP 1.0 utility to rotate images now time has come for some image fun

When writing mobile applications, it’s always cool to add some effects or transitions. But, while for example FlashLite has a nice builtin support for them, with J2ME you have to hand-code even the simplest movement (and this is the main reason why most J2ME apps are all but attractive).
So, here’s a first class that you can use to add an “explode” effect to images in a straightforward way. How to do it? Here we come:
- Download the ExplodingImage.java source code and put it straight in your project
- Instantiate an ExplodingImage this way:
//get your Image
Image sourceImage = Image.createImage("/image.png");
//and then use it in ExplodingImage constructor
ExplodingImage image = new ExplodingImage(sourceImage , 5, 8, 8); |
The ExplodingImage constructor accepts the following arguments:
- An Image instance
- An int representing the “level” for the exploding effect, that is the strength of the effect itself (higher the level, stronger the effect).
- The last 2 int arguments represent the horizontal and vertical pieces of the exploded image.
- Start the explode effect with the explode() method, that will accept the effect duration as argument:
- To paint it, simply use its paint() method, very similary to the Graphics drawImage() one. For example, in a Canvas paint() method, you can do something like this:
protected void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
image.paint(g, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
} |
To give the effect a “smooth” animation, you should paint it quite frequently (let’s say, not once per second :)). So, always using Canvas, a sample code could be like this:
public void run()
{
while(true)
{
repaint();
try
{
synchronized(this)
{
wait(50L);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
} |
- To test if the effect has ended, you can simply access your ExplodingImage ended instance variable:
if(image.ended)
{
//effect-end related code
} |
- And you’re done! See it in action here: J2ME image explode effect in action
Sample source code is available here:
ds 7:55 am on December 11, 2008 Permalink
Thanks! Very useful!
Have just given it a go and it works like a charm.
The only thing is I seem to be able to get much smaller images when I save from photoshop.
Have you any idea if the LZW compression is working properly? The file size I am getting now is just a tad bit smaller in bytes than I have total pixels. On the other hand photoshop, or php for that matter reduces this to 1/5.
Thanks again!
Sajid 10:44 am on August 12, 2011 Permalink
Hey hi, How to use this J2ME Animated GIF encoder in J2ME Application, please tel me.
Thanks