×
Namespaces

Variants
Actions

Simple Java ME Canvas loading bar

From Nokia Developer Wiki
Jump to: navigation, search

This is an example of a simple canvas based loading bar which you can use as a splash screen for you application.

Article Metadata
Article
Created: muiruri (31 Aug 2009)
Last edited: hamishwillee (09 Aug 2013)

First we create the canvas for the application and get the width and height for the screen

height = getHeight();
width = getWidth();

We then have a factor for the incrementing the loading

factor = width / 4;

We also have a Timer Object

Timer timer = new Timer();

This code comes in the paint method of the canvas

g.setColor(0x00ffccff);
g.fillRect(0, 0, width, height);
g.setColor(0x00ffffff);
g.fillRect(25, height / 2, width - 50, 2);
g.setColor(0x003399ff);
g.drawString("Application name", 100, (height / 2) - 30, Graphics.TOP | Graphics.HCENTER);
g.fillRect(25, height / 2, current, 2);
g.drawString("Loading...", 100, (height / 2) + 20, Graphics.BOTTOM | Graphics.HCENTER);

We then create a class inside the canvas class which will extend the TimerTask class to handle the animation

     private class Draw extends TimerTask {
public void run() {
current = current + factor;
if (current > width - 50) {
current = 0;
repaint();
} else {
repaint();
}
}
}

In the constructor of the midlet, we schedule the timer Object to run

repaint();
timer.schedule(new Draw(), 2000, 2000);

We now need to create an object of the canvas in the midlet and display it.

This page was last modified on 9 August 2013, at 08:15.
106 page views in the last 30 days.
×