Namespaces
Variants
Actions
Revision as of 08:53, 8 December 2011 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Barra de progresso, usando Canvas, em Java ME

Jump to: navigation, search
Dados do artigo

Artigo
Tradução:
Última alteração feita por hamishwillee em 08 Dec 2011

Uma barra de progresso simples para ser usada em Canvas quando longas operações estiverem sendo feitas. Você pode ver um. Aqui você pode ter uma pré-visualização. Emulador barra de progresso em Canvas

J2me loading bar.png

Nós vamos iniciar definindo algumas variáveis de instância, que usaremos dentro do nosso código

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;

Agora o construtor da barra, que tem os argumentos:

  • tamanho da barra (width(largura) e height(altura))
  • padding(espaçamento) entre os quadrados
  • número de quadrados para ser usados.
  • cor dos quadrados
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;
}

O método Paint é bastante simples:

public void paint(Graphics g)
{
g.setColor(color);
 
for(int i = 0; i < currentSquares; i++)
{
g.fillRect(i * (squareWidth + padding), 0, squareWidth, height);
}
}

Finalmente, a animação lógica, onde usaremos um contador, e exibir o método start() e stop() para controla a animação da barra.

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);
}
133 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved