I created a custom animated bitmap field, like this:
//thread class
public class AnimatedImageField extends BitmapField implements Runnable
{
private Thread thread = null;
private boolean animate=true;
private int interval = 0;
private int index=0;
private Bitmap bitmap = null;
private int frameno = 0;
private int fieldHeight=0;
private int fieldWidth=0;
private Bitmap finalbitmap = null;
private int imgHeight = 0;
private int imgWidth= 0;
public AnimatedImageField(int fieldwidth, int fieldheight, Bitmap bitmap, int frameno, int interval, long style)
{
super(bitmap, style);
this.interval=interval;
this.bitmap=bitmap;
this.frameno=frameno;
imgHeight = bitmap.getHeight();
int imgwidth = bitmap.getWidth();
imgWidth=imgwidth/frameno;
this.fieldWidth = fieldwidth;
this.fieldHeight = fieldheight;
}
public AnimatedImageField(Bitmap bitmap, int frameno, int interval, long style)
{
super(bitmap, style);
this.interval=interval;
this.bitmap=bitmap;
this.frameno=frameno;
imgHeight = bitmap.getHeight();
int imgwidth = bitmap.getWidth();
imgWidth=imgwidth/frameno;
fieldWidth = imgWidth;
fieldHeight = imgHeight;
}
protected void paint(Graphics graphics){
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0,this.getPreferredWidth(), this.getPreferredHeight());
//System.out.println("animate:"+animate);
if ( animate )
graphics.drawBitmap((fieldWidth-imgWidth)/2, (fieldHeight-imgHeight-50)/2,
imgWidth, bitmap.getHeight(), bitmap , imgWidth*index, 0);
else
graphics.drawBitmap((fieldWidth-finalbitmap.getWidth())/2, (fieldHeight-finalbitmap.getHeight()-50)/2,
finalbitmap.getWidth(), finalbitmap.getHeight(), finalbitmap , 0, 0);
}
public int getPreferredWidth()
{
return fieldWidth;
}
public int getPreferredHeight()
{
return fieldHeight;
}
protected void layout(int arg0, int arg1)
{
setExtent(getPreferredWidth(), getPreferredHeight());
}
public void startAnimation(){
//System.out.println("startAnimation");
animate=true;
thread = new Thread(this);
thread.start();
}
public void updateLayout(int height, int width){
//System.out.println("updateLayout:height:"+height);
this.fieldHeight=height;
this.fieldWidth=width;
super.updateLayout();
}
public void stopAnimation(Bitmap bitmap){
//System.out.println("stopAnimation");
this.finalbitmap=bitmap;
animate=false;
}
public void stopAnimation(){
//System.out.println("stopAnimation");
animate=false;
}
public void run(){
while(animate){
//System.out.println("run:animate:"+animate);
try{ Thread.sleep(interval);}catch(Exception e){}
if ( index+1>=frameno )
index=0;
else
index++;
invalidate();
}
}
}
call from :
//loading
Bitmap load_icon = Bitmap.getBitmapResource("loading.png");
AnimatedImageField spinner = new AnimatedImageField(Display.getWidth(), Display.getHeight(), load_icon,
12, 100, Field.FIELD_HCENTER | Field.FOCUSABLE
| Field.FIELD_VCENTER);
spinner.startAnimation();
add(spinner);