Hi eveybody! I've written a custom widget to play GIF animated images by extending the Picture widget and the Sprite
class. This is my GifSprite:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kalmeo.kuix.util;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import org.kalmeo.kuix.widget.GIFPicture;
/**
*
* @author Administrator
*/
public class GifSprite extends Sprite implements Runnable{
GifDecoder decoder;
GIFPicture widget = null;
boolean animated = false;
public GifSprite(String imageUrl, GIFPicture w) throws Exception{
super(Image.createImage(imageUrl));
this.widget = w;
decoder = new GifDecoder();
if (decoder != null) {
int err = decoder.read(imageUrl);
if (err == 0) {
setImage(decoder.getImage(), decoder.getImage().getWidth(),decoder.getImage().getHeight() );
} // end if
} // end if
}
public GifSprite(Image image, int frameWidth, int frameHeight) {
super(image, frameWidth, frameHeight);
}
public GifSprite(Image image) {
super(image);
}
public void animate(){
if(animated)
return;
animated = true;
new Thread(this).start();
}
public void stopAnimation(){
animated = false;
// setImage(decoder.getFrame(0), decoder.getFrame(0).getWidth(), decoder.getFrame(0).getHeight());
// widget.invalidateAppearance();
}
public void run() {
int t;
while(animated){
if(decoder == null)
continue;
int n = decoder.getFrameCount();
for (int i = 0; i < n; i++) {
t = decoder.getDelay(i);
setImage(decoder.getFrame(i), decoder.getFrame(i).getWidth(), decoder.getFrame(i).getHeight());
widget.invalidateAppearance();
try {
Thread.sleep(t);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
And I then extend the Picture widget by replace the original sprite by mine and made the following modifications:
public GIFPicture setSource(String source) {
if ((source == null) || (source.length() == 0)) {
return this;
}
String imgsrc=new StringBuffer(KuixConstants.DEFAULT_IMG_RES_FOLDER).append(source).toString();
try {
sprite = new GifSprite(imgsrc, this);
} catch (Exception ex) {
ex.printStackTrace();
}
return this;
}
.......
.......
public void setVisible(boolean v){
super.setVisible(v);
if(!v){
action = false;
sprite.stopAnimation();
}else{
action = true;
sprite.animate();
}
}
And at the end of the doLayout() method I added the following line:
sprite.animate();
Also I clean up and call removeALL() method of all my screen when they are not more displayed and that their
Frame is removed from the FrameHandler list (This is just to free the memory).
Despite all this the Gif plays well and the application works fine on the emulator but freezes on real device. From one run to another some times it doesn't froze at the same point. But if I replace the GifPicture in the screen by a common Picture widget, evrything
works ok.
So please tell me what am I doing wrong or is there a tip that I don't know I should use?
P.S: I'm using two phone to test my applications : NOKIA 6500 and NOKIA E51-1. I tried a SonyEcricsson S312 but the Gif doesn't play on it.
Regards!

