BlackBerry Java 进度条编写-包含win xp进度条

进度条代码分享,三种进度条:

  1. 百分比进度条
  2. 一般进度条
  3. 模仿windows xp进度条
  • 百分比进度条 - 用于装载数据等待,可以设置进度条时间片和时间片进度

 

 

  • 一般进度条 - 普通进度条,可以设置总共时间进度

 

 

 

  • winxp 进度条 - xp滚动条,可以无限制的滚动,比如在rpc任务时,数据没有返回前使用此滚动条。当数据返回后移除滚动条。

 

 

 

 

源代码如下:

-------------------------------------------------------------------------

类 ProgressBarField.java

 

import com.rim.client.RIMConstants;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;

public class ProgressBarField extends Field{
 
 // timed progress bar with label
 public static final int PROGRESS = 1;
 
 // timed progress bar with percent label
 public static final int PERCENT = 2;
 
 // unlimited progerss bar with label, can be used for progress dialog
 public static final int UNLIMITED = 3;
 
 public static final int INTERVAL = 100;
 
 public static final int NORMAL_CELL_WIDTH = 4;
 public static final int CELL_SPACE = 2;
 public static final int OFFSET = 2;
 public static final int EDGE_WIDTH = 3;

 Font textFont;
 int textColor;
 int edgeColor;
 int barBackgroundColor;
 int progerssBarColor;
 int interval;
 
 int m_style;
 int m_width, m_height;
 String m_text;
 
 int totalSeconds, progressInc;
 int m_currentProgress, totalProgress;
 int cellWidth, cellHeight;
 int sliderWidth;
   
 int posX, cellY, drawX;
    int cellWidthOffset;
   
 boolean stop = false;
 boolean m_pause = false;
 
 public ProgressBarField(int width, int height, String label, int totalSeconds, int interval, int style){
  super();
  m_width = width;
  m_height = height;
  m_style = style;
  m_text = label;
  this.totalSeconds = totalSeconds;
  this.interval = interval;
  init();
 }
 public ProgressBarField(int width, int height, String label, int style){
  super();
  m_width = width;
  m_height = height;
  m_style = style;
  m_text = label;
  totalSeconds = 1000000;
  interval = 1000;
  init();
 }
 public void set(int totalSeconds, int progress) {
  if(totalSeconds != 0) {
   this.totalSeconds = totalSeconds;
  }
 
  this.progressInc = progress;

  init();
 }
 public void setPattern(Font textFont, int textColor,
   int edgeColor, int barBackgroundColor, int progressBarColor){
  this.textFont = textFont;
  this.textColor = textColor;
  this.edgeColor = edgeColor;
  this.barBackgroundColor = barBackgroundColor;
  this.progerssBarColor = progressBarColor;
 }
 
 private void init(){
  textFont = Font.getDefault();
  textColor = RIMConstants.PROGRESS_BAR_TEXT_COLOR;
  edgeColor = RIMConstants.PROGRESS_BAR_EDGE_COLOR;
  barBackgroundColor = RIMConstants.BG_COLOR;
  progerssBarColor = RIMConstants.PROGRESS_BAR_COLOR;
  
  totalProgress = m_width-2*EDGE_WIDTH;
        switch(m_style){
        case PROGRESS:
        case PERCENT:
         progressInc = totalProgress/(totalSeconds/interval);
         cellWidth = Math.max(progressInc, NORMAL_CELL_WIDTH);
            cellHeight = m_height - 2*EDGE_WIDTH - 2*OFFSET;
        break;
        case UNLIMITED:
         cellWidth = Math.max(totalProgress/20, NORMAL_CELL_WIDTH);
            cellHeight = m_height - 2*EDGE_WIDTH - 2*OFFSET;
         sliderWidth = m_width/3;
         progressInc = cellWidth+CELL_SPACE;
         posX = -sliderWidth-EDGE_WIDTH;
            cellY = (m_height-cellHeight)/2;
            cellWidthOffset = 0;
            drawX = EDGE_WIDTH+OFFSET;
        break;
        }
 }
 public int getPreferredWidth() {
  return m_width;
 }
 public int getPreferredHeight() {
  return m_height;
 }
 protected void layout(int width, int height) {
  setExtent(m_width, m_height);
 }
   
    private void paintProgress(Graphics g, int drawX, int progressColor, int cellWidthOffset){
     g.setColor(progressColor);
     int numOfCell =  getCellNumber(cellWidthOffset);
  for (int i=0, cellX=drawX; i<numOfCell; i++){
   g.fillRect(cellX, (getHeight()-cellHeight)/2, cellWidth, cellHeight);
   cellX += cellWidth + CELL_SPACE;
  }
    }
   
    private int getCellNumber(int cellWidthOffset){
     return cellWidthOffset/(cellWidth+CELL_SPACE)+(cellWidthOffset%(cellWidth+CELL_SPACE))/cellWidth;
    }
 
 protected void paint(Graphics g) {
  
  g.setColor(edgeColor);
  g.fillRect(0, 0, getWidth(), getHeight());
  
  g.setColor(barBackgroundColor);
  g.fillRect(EDGE_WIDTH, EDGE_WIDTH, getWidth()-2*EDGE_WIDTH, getHeight()-2*EDGE_WIDTH);

  switch(m_style){
  case PROGRESS:
   paintProgress(g, EDGE_WIDTH+CELL_SPACE, progerssBarColor, m_currentProgress);
   g.setColor(textColor);
   g.drawText(m_text, (m_width-textFont.getAdvance(m_text))/2, (m_height-textFont.getHeight())/2);
   break;
  case PERCENT:
   paintProgress(g, EDGE_WIDTH+CELL_SPACE, progerssBarColor, m_currentProgress);
   String percent = m_text+m_currentProgress*100/totalProgress+"%";
   g.setColor(textColor);
   g.drawText(percent, (m_width-textFont.getAdvance(percent))/2, (m_height-textFont.getHeight())/2);
   break;
  case UNLIMITED:
   paintProgress(g, drawX, progerssBarColor, cellWidthOffset);
   g.setColor(textColor);
   g.drawText(m_text, (m_width-textFont.getAdvance(m_text))/2, (m_height-textFont.getHeight())/2);
  }
 }
 
 public int getCurrentProgress(){
  return m_currentProgress;
 }
 
 public void setCurrentProgress(int progress){
  m_currentProgress = progress;
 }
 
 public void increamentProgress(){

  switch(m_style){
  case PROGRESS:
  case PERCENT:
   while (m_currentProgress < totalProgress){
    m_currentProgress += progressInc;
    if (m_currentProgress > totalProgress){
     m_currentProgress = totalProgress;
    }
    try{
     Thread.sleep(interval);
    }catch(Exception ex){}
          invalidate();
          if (m_pause){
     synchronized(this){
      try{
             wait();
            }catch(Exception ex){}
     }
          }
   }
   break;
  case UNLIMITED:
   while (!stop){
    posX += progressInc;
    if (posX + sliderWidth + EDGE_WIDTH  < sliderWidth + cellWidth){
     cellWidthOffset += cellWidth+CELL_SPACE;
     drawX = EDGE_WIDTH+OFFSET;
    }else if (posX + sliderWidth +EDGE_WIDTH > sliderWidth){
     if (posX+sliderWidth <= m_width-EDGE_WIDTH){
      drawX = posX;
     }else{
      if (sliderWidth-(posX+sliderWidth-m_width)-EDGE_WIDTH < cellWidth){
       posX = -sliderWidth-EDGE_WIDTH;
       cellWidthOffset = 0;
       drawX = EDGE_WIDTH+OFFSET;
      }else{
       cellWidthOffset = sliderWidth-(posX+sliderWidth-m_width)-EDGE_WIDTH - OFFSET;
       drawX = m_width-EDGE_WIDTH - OFFSET -((getCellNumber(cellWidthOffset)-1)*(cellWidth+CELL_SPACE)+cellWidth);
      }
     }
    }
    
    try{
     Thread.sleep(interval);
    }catch(Exception ex){}
    invalidate();
    if (m_pause){
     synchronized(this){
      try{
             wait();
            }catch(Exception ex){}
     }
          }
   }
  }
 }
 
 public void pauseUpdate(){
  m_pause = true;
 }
 
 public void resumeUpdate(){
  m_pause = false;
  synchronized(this){
   notify();
  }
 }
 
 public void stopUpdate(){
  stop = true;
 }
}

 

-------------------------------------------------------------------------------------------

类 ProgressThread.java

 

public class ProgressThread extends Thread{

 private ProgressBarField progressField;
 private boolean running;
 
 public ProgressThread(ProgressBarField progressField){
  this.progressField = progressField;
 }
 
 public void startThread(){
  running = true;
  start();
 }
 
 public void run(){
  while (running){
   progressField.increamentProgress();
  }
 }
 
 public void stop(){
  running = false;
  try{
   this.join();
  }catch(Exception ex){}
 }
 
}

 

-------------------------------------------------------------------

 

类 SpacerField.java

 

import com.rim.client.RIMConstants;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class SpacerField extends Field {
    int m_width, m_height;
    public SpacerField(int width, int height) {
        m_width = width;
        m_height = height;
    }
    protected void layout(int width, int height) {
        // TODO Auto-generated method stub
        this.setExtent(m_width, m_height);
    }
    public int getPreferredWidth() {
        return m_width;
    }
    public int getPreferredHeight() {
        return m_height;
    }

    protected void paint(Graphics graphics) {
        // TODO Auto-generated method stub
     int color = graphics.getColor();
     graphics.setColor(RIMConstants.BG_COLOR);
     graphics.fillRect(0, 0, m_width, m_height);
     graphics.setColor(color);
    }

}

-------------------------------------------------------------------------

 

类 SplashScreen.java

 

import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;

import com.rim.client.*;

public class SplashScreen extends MainScreen{
 
 public static final String SPLASH_IMAGE_URL = "splash_image.png";
 public static final Font TEXT_FONT = Font.getDefault().derive(Font.ANTIALIAS_LOW_RES);
 
 public static final int TOTAL_TIME = 10000;
 
 Field m_title;
 BitmapField m_splashImage;
    ProgressBarField m_progressBar;
    ProgressThread m_progressThread;
 
    public SplashScreen() {
     super(Manager.NO_VERTICAL_SCROLL|Manager.NO_HORIZONTAL_SCROLL);
     
     m_title =  new LabelField("BlackBerry 黑莓", Field.FIELD_VCENTER|Field.FIELD_HCENTER);
  m_splashImage = new BitmapField(Bitmap.getBitmapResource(SPLASH_IMAGE_URL), Field.FIELD_HCENTER|Field.FIELD_VCENTER);
  int imageRegionHeight = (Display.getHeight() - m_title.getPreferredHeight()-getPaddingBottom())*2/3;
  
  setTitle(m_title);
  add(new SpacerField(Display.getWidth(), (imageRegionHeight - m_splashImage.getBitmapHeight())/2));
  HorizontalFieldManager imageManager = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
  
  int topSpacerW = (Display.getWidth()-getPaddingLeft()-getPaddingRight()-m_splashImage.getBitmapWidth())/2;
  imageManager.add(new SpacerField(topSpacerW, m_splashImage.getBitmapHeight()));
  imageManager.add(m_splashImage);
  imageManager.add(new SpacerField((Display.getWidth()-getPaddingLeft()-getPaddingRight()-m_splashImage.getBitmapWidth()-topSpacerW), m_splashImage.getBitmapHeight()));
  add(imageManager);
  add(new SpacerField(Display.getWidth(), (imageRegionHeight - m_splashImage.getBitmapHeight())/2));
  
  int barWidth = (Display.getWidth()-getPaddingLeft()-getPaddingRight())*2/3;
  int barHeight = TEXT_FONT.getHeight()+2*ProgressBarField.OFFSET+2*ProgressBarField.EDGE_WIDTH;
  
  m_progressBar = new ProgressBarField(barWidth, barHeight, "", TOTAL_TIME, 50, ProgressBarField.UNLIMITED);
  m_progressBar.setPattern(TEXT_FONT, RIMConstants.PROGRESS_BAR_TEXT_COLOR,
    RIMConstants.PROGRESS_BAR_EDGE_COLOR, RIMConstants.BG_COLOR, RIMConstants.PROGRESS_BAR_COLOR);
  
  int vSpacerW = Display.getWidth()-getPaddingLeft()-getPaddingRight();
  int vSpacerH = (Display.getHeight()-m_title.getPreferredHeight()-getPaddingBottom()-imageRegionHeight-barHeight)/2;
  add(new SpacerField(vSpacerW, vSpacerH));

  int hSpacerW = (Display.getWidth()-barWidth)/2;
  int hSpacerH = barHeight;
  HorizontalFieldManager barManager = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
  barManager.add(new SpacerField(hSpacerW, hSpacerH));
     barManager.add(m_progressBar);
     barManager.add(new SpacerField(Display.getWidth()-getPaddingLeft()-getPaddingRight()-hSpacerW-barWidth, hSpacerH));
     add(barManager);
     add(new SpacerField(vSpacerW, vSpacerH));
    
     m_progressThread = new ProgressThread(m_progressBar);
    }
 
    public void startSplash(){
  m_progressThread.startThread();
    }
   
    public void stopSplash(){
     m_progressThread.stop();
    }
}

 

--------------------------------------------------------------

类 RIMConstants.java

 

public class RIMConstants {
    public static final int BG_COLOR = 0x000000;
    public static final int ITEM_BG_COLOR = 0x595a5c;
    public static final int ITEM_HL_COLOR = 0xe19f00;
    public static final int ITEM_EDGE_COLOR = 0x000000;
    public static final int ITEM_TEXT_COLOR = 0x000000;
   
    public static final int PROGRESS_BAR_EDGE_COLOR = 0x595a5c;
    public static final int PROGRESS_BAR_COLOR = 0xe19f00;
    public static final int PROGRESS_BAR_TEXT_COLOR = 0xffffff;
 
}

---------------------------------------------------------------

类 RIMClient.java

 

import net.rim.device.api.ui.UiApplication;
import com.rim.ui.*;

public class RIMClient extends UiApplication {
 
 private SplashScreen splashScreen;
 
 public static void main(String[] args) {
  RIMClient client = new RIMClient();
  client.enterEventDispatcher();
 }
 
 public RIMClient() {
  
  splashScreen = new SplashScreen();
  splashScreen.startSplash();
  this.pushScreen(splashScreen);
 } 
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值