J2ME自己画界面-进度条

参照http://blog.csdn.net/alikeboy/archive/2005/03/07/314018.aspx 使用Canvas绘画Gauge

 

J2ME api 文档对进度条的描述是:

Gauge : 分为  interactive  / non-interactive

non-interactive gauge的四个状态: continuous-idle, incremental-idle, continuous-running , incremental-updating

a non-interactive gauge may be used to give user feeback during a long-running operation.If the application can observe the progress of the operation as it proceeds to an endpoint known in advance, then the application should use a non-interactive Gauge with a definite range .If, on the other hand, the application is downloading a file of unknown size, it should use a non-interactive Gauge with indefinite range. Ideally, the application should call setValue(INCREMENTAL_UPDATING) periodically, perhaps each time its input buffer has filled. This will give the user an indication of the rate at which progress is occurring.

Gauge的方法列表:

Gauge(String label, boolean interactive ,int maxValue, int initalValue)

void addCommand(Command cmd)

int getMaxValue()

int getValue()

boolean isInteractive()

void setDefaultCommand(Command cmd)

void setItemCommandListener(ItemCommandListener l)

void setLabel(String label)

void setLayout(int layout)

void setMaxValue(int maxValue)

void setPreferredSize(int width, int height)

void setValue(int value)

判断:Gauge码应该是一CommandListenerProgressObserver

 

Gauge 详细设计内容:

 

Field :      int  GAUGE_MAX 

           Command stopCmd  //在交互式进度条情况下,开关进度条

           boolean stopped

           boolean stoppable //判断是否可以暂停进度条

           int current //目前的值

      Cancelable cancel; //用于回调,暂未实现

 

Method :  protected Gauge()

             public static getInstance()

             public reset()

             public updateProgress(Object parm1)

             public boolean isStopped()

             public void setStoppend(boolean stopped)

             public void setPrompt(String prompt)

             public void commandAction (Command command, Displayable displayable)

             public void show()

             pubic void exit()

             public void setMaxValue()

             public void setValue()

             pulib voivd setCancelableObjec(Cancel co);           

 

 

看看我的最终代码,暂时实现在屏幕底部绘画一个进度条的功能

*******************************************************************************

Observable.java

*******************************************************************************

package mvc.mobile.util;

 

import java.util.Vector;

 

/**

 * <p>Title: </p>

 *

 * <p>Description: 最简单的Observable实现</p>

 *

 * <p>Copyright: Copyright (c) 2009</p>

 *

 * <p>Company: </p>

 *

 * @author lipei

 * @version 1.0

 */

public class Observable {

  private boolean iChanged = false;

  private Vector iObs ;

  public Observable() {

    //创建目标时候就新建一个观察者列表,注意这个列表需要同步

    iObs = new Vector();

  }

  /**

   * 添加观察者到观察者列表中

   */

  public synchronized void addObserver(Observer aObserver){

    if(aObserver == null )

      return ;

    if(!iObs.contains(aObserver))

    iObs.addElement(aObserver);

  }

  /**

   * 删除一个观察者

   */

  public synchronized void deleteObserver(Observer aObserver){

   iObs.removeElement(aObserver);

  }

  /**

   * 通知操作,即被观察者发生变化,通知对应的观察者进行事先设定的操作,不传参数的通知方法

   */

  public void notifyObserver(){

   notifyObservers(null);

  }

  public void notifyObservers(Object aArg){

 

   synchronized(this){

     if(!iChanged)

        return ;

      clearChanged();

   }

   for(int i= iObs.size()-1; i>=0 ;i--)

     ((Observer)iObs.elementAt(i)).update(this,aArg);

  }

  protected synchronized void clearChanged(){

    iChanged = false;

  }

  protected synchronized void setChanged(){

    iChanged = true ;

  }

  public static void main(String[] args) {

    Observable observable = new Observable();

 

  }

}

*******************************************************************************

Observer.java

*******************************************************************************package mvc.mobile.util;

 

/**

 * <p>Title: </p>

 *

 * <p>Description:Observer interface for j2me</p>

 *

 * <p>Copyright: Copyright (c) 2009</p>

 *

 * <p>Company: </p>

 *

 * @author not attributable

 * @version 1.0

 */

public interface Observer {

  void update(Observable iTarget, Object iInterests);

}

*******************************************************************************

ProgressObserver.java

package mvc.mobile.util.ui;

 

import mvc.mobile.util.*;

import javax.microedition.lcdui.Display;

 

/**

 * <p>Title:进度条 </p>

 *

 * <p>Description:优点 </p>

 * 1.低耦合

 * 2.支持可中断任务

 * 3.进度条自我绘制功能

 * <p>Copyright: Copyright (c) 2009</p>

 *

 * <p>Company: </p>

 *

 * @author not attributable

 * @version 1.0

 */

public interface ProgressObserver

    extends Observer {

    /**

     * 将进度条复位

     */

     void reset();

    /**

     * 将进度条设置最大

     */

     void setMax();

    /**

     * 自我绘制

     */

    void show(Display display);

    /**

     * 退出

     */

    void exit();

    /**

     * 更新进度条

     */

    void update(Observable aBgThread, Object param);

//    public void setCancelObject(Cancelable aCo);

 

//    boolean isStoppable();

//    void setStoppable(boolean aStoppable);

    boolean isStopped();

    void setStopped(boolean aStopped);

 

}

*******************************************************************************

Gauge.java

*******************************************************************************

package mvc.mobile.util.ui;

 

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.Display;

import mvc.mobile.util.Observable;

import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.Graphics;

import mvc.mobile.util.ui.setting.Platform;

 

/**

 * <p>Title: </p>

 *

 * <p>Description: </p>

 *

 * <p>Copyright: Copyright (c) 2009</p>

 *

 * <p>Company: </p>

 *

 * @author lipei

 * @version 1.0

 */

public class Gauge extends Item implements ProgressObserver

//    , CommandListener

{

  private static int GAUGE_MAX =8;

  private static int GAUGE_DEFAULT = 8;

//  private Canvas iCanvas ;

//  private Command iStopCommand ;

  private static boolean iStopped;

//  private boolean iStoppable ;

  private static int iCurrent =0;

//  private Cancelable iCancel ;

  private static Gauge iInstance =null;

 

  public Gauge() {

    super(Platform.WIDTH /2 - 30,Platform.HEIGHT - 20,60, 16);

//    iStopCommand = new Command("Stop",Command.STOP,10);

 

  }

 

  public static void main(String[] args) {

    Gauge gauge = new Gauge();

  }

  public static Gauge getInstance(){

    if(iInstance==null)

      iInstance = new Gauge();

    return iInstance;

  }

  public void reset(){

    iCurrent = 0 ;

    iStopped= false;

 

  }

 

  public void setMax() {

 

  }

 

  public void show(Display display) {

 

  }

 

  public void exit() {

  }

 

  public void update( Observable aBgThread, Object param) {

 

      iCurrent = (iCurrent + 1) % (GAUGE_DEFAULT + 1);

  }

 

 

 

  public boolean isStopped() {

    return iStopped;

  }

 

  public void setStopped(boolean aStopped) {

   iStopped = aStopped ;

  }

 

 

  public void paint(Graphics graphics) {

    System.out.println("paint gauge");

    graphics.setColor(0xFFFFFF);

    graphics.fillRect(position[X],position[Y],position[WIDTH],position[HEIGHT]);

    graphics.setColor(0x1E90FF);

    graphics.fillRect(position[X],position[Y],iCurrent*position[WIDTH]/GAUGE_DEFAULT,position[HEIGHT]);

  }

 

  public void onClick(int keyCode) {

  }

 

 

 

}

 

*******************************************************************************

TaskManager.java

*******************************************************************************

package mvc.mobile.util.ui.timer;

 

import java.util.TimerTask;

import java.util.Timer;

 

/**

 *@(#)TimerTaskManager.java

 * 版权声明 , 版权所有 违者必究

 * 版本号  1.0

 * 功能描述:

 *    定时器的启动,停止功能

 *

 *修订记录:

 *1)更改者:not attributable

 *  时 间:2009--

 *  描 述:创建

 */

 

public class TimerTaskManager {

    private static TimerTaskManager instance = null;

    private static  Timer timer = null ;

    public static TimerTaskManager getInstance(){

      if (null == instance)

            instance  = new TimerTaskManager();

         return instance ;

    }

    public static Timer getTimer(){

       if(null == timer)

           timer = new Timer();

       return timer ;

    }

    public static TimerTask add(Runnable runnable ,long period){

      TimerTask  task = new RunnableTask(runnable);

      long delay = period ;

      getTimer().schedule(task,delay,period);

      return task ;

    }

 

    static class RunnableTask  extends TimerTask implements Runnable{

        private Runnable runnable ;

        public RunnableTask(Runnable runnable){

          this.runnable = runnable ;

        }

        public void run() {

            runnable.run() ;

        }

 

    }

    public void close(){

       if(timer!= null){

         timer.cancel();

         timer = null;

       }

    }

}

最后在启动代码里面添加:

    Gauge sGauge = Gauge.getInstance();

    sGauge.reset();

   

    BackgroundThread sBgTask = new BackgroundThread(sGauge, this,

        Display.getDisplay(midlet)) {

      public boolean runTask() {

        setChanged();

        notifyObservers(null);

        TextBoxCanvas.this.repaint();

//        try {

//          Thread.sleep(3000);

//        }

//        catch (Exception e) {

//          e.printStackTrace();

//          return false;

//        }

 

       return true;

      }

 

    };

sBgTask.addObserver(sGauge);

TimerTaskManager.getInstance().add(sBgTask, 1000);

 

程序截图:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值