Categories: Java | Java ME | Code Examples | UI
This page was last modified 08:30, 30 April 2008.
J2ME Canvas Loading Bar
From Forum Nokia Wiki
A simple loading bar to be used on Canvas when doing long operations. You can see a live preview here: Canvas Loading bar emulator
We'll start defining some instance variables, that we'll use within our code:
public long stepInterval = 250L; public int width = 0; public int height = 0; int padding = 0; int color = 0x000000; int squares = 0; int squareWidth = 0; int currentSquares = 0; Timer stepTimer = null;
Now the bar constructor, with its arguments:
- bar size (width and height)
- padding between squares
- number of squares to be used
- squares color
public LoadingBar(int width, int height, int padding, int squares, int color) { this.width = width; this.height = height; this.squares = squares; this.color = color; this.padding = padding; this.squareWidth = (width - padding) / (squares) - padding; }
Paint method is quite straightforward:
public void paint(Graphics g) { g.setColor(color); for(int i = 0; i < currentSquares; i++) { g.fillRect(i * (squareWidth + padding), 0, squareWidth, height); } }
Finally, the animation logic, where we'll use a Timer, and expose start() and stop() methods to control Bar animation:
public void start() { stepTimer = new Timer(); stepTimer.schedule(new TimerTask() { public void run() { step(); } }, stepInterval, stepInterval ); } public void stop() { stepTimer.cancel(); } void step() { currentSquares = (currentSquares + 1) % (squares + 1); }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Displaying OK command directly on left softkey | simonru | Mobile Java General | 5 | 2005-09-07 03:17 |
| Showing message on status bar | korakotc | Python | 0 | 2006-03-06 15:36 |
| ListBox problem | chandrasekharan | General Symbian C++ | 11 | 2007-10-01 06:48 |
| canvas wont display | jonneymendoza | Mobile Java General | 1 | 2007-11-20 09:13 |
| Could someone make me a theme with this picture? | Freezer7Pro | General Discussion | 8 | 2007-10-07 09:04 |

