LWUIT显示漂亮的loading界面的两种方法

强烈推荐此blog作者的博客

 

原文出处:http://blog.csdn.net/pjw100/archive/2009/11/23/4854740.aspx

 

 

我们加载某一个Form页面时,如果这个页面内容较多,加载需要一定的时间,那我们就希望做一个等待的画面,比如"某某正在加载,请等 待...",在这个画面中以动态效果来说为最好,用户也知道需要等待很短的时间。我做等待界面有两种方法:

首先是方法一,这种方法属于文字等待,就是在界面上画一串文字,"..."是以动态的形式显示,代码如下:


  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */   
  5. package  com.thinkrace.icredit;  
  6. import  com.sun.lwuit.Font;  
  7. import  com.sun.lwuit.Form;  
  8. import  com.sun.lwuit.Graphics;  
  9. import  com.sun.lwuit.Image;  
  10. import  com.sun.lwuit.Label;  
  11. import  com.sun.lwuit.layouts.BorderLayout;  
  12. import  java.io.IOException;  
  13. import  java.util.Random;  
  14. import  java.util.Timer;  
  15. import  java.util.TimerTask;  
  16. /**  
  17.  *  
  18.  * @author Administrator  
  19.  */   
  20. public   class  SplashForm  extends  Form  implements  Runnable {  
  21.     private  Timer timer =  new  Timer();  
  22.     private   long  displayTime =  1000 ;  
  23.     private  StringBuffer loading =  new  StringBuffer( "Saleslion is loading" );  
  24.     public  SplashForm() {  
  25.         Thread t = new  Thread( this );  
  26.         t.start();  
  27.         this .setLayout( new  BorderLayout());         
  28.         this .show();  
  29.     }  
  30.     public   void  paint(Graphics g) {  
  31.         try  {  
  32.             Image wait = Image.createImage("/logo.png" );  
  33.             //绘制logo   
  34.             g.drawImage(wait, (getWidth() - wait.getWidth()) / 2 , (getHeight() - wait.getHeight()-  70 ) /  2 );  
  35.             g.setColor(0xffffff );  
  36.             Font fnt = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);  
  37.             g.setFont(fnt);  
  38.             int  wordWidth = fnt.stringWidth( "Saleslion is loading..." );  
  39.             //绘制等待字符串   
  40.             g.drawString(loading.toString(), (getWidth() - wordWidth) / 2 , (getHeight() + wait.getHeight() - 60 ) /  2 );  
  41.         } catch  (IOException ex) {  
  42.             ex.printStackTrace();  
  43.         }  
  44.     }  
  45.     private   void  disappear() {  
  46.         timer.cancel();  
  47.         try  {  
  48.             new  LoginForm();  
  49.         } catch  (IOException ex) {  
  50.             ex.printStackTrace();  
  51.         }  
  52.     }  
  53.     //调用TimerTask,时间到了后,调用 disappear(),当前画面消失,跳至另一个Form   
  54.     protected   void  showNotify() {  
  55.         timer.schedule(new  TimerTask() {  
  56.             public   void  run() {  
  57.                 disappear();  
  58.             }  
  59.         }, displayTime);  
  60.     }  
  61.     //设置字符串   
  62.     public   void  setLoading() {  
  63.         if  (loading.toString().indexOf( "..." ) >  0 ) {  
  64.             loading.delete(loading.length() - 3 , loading.length());  
  65.         }  
  66.     }  
  67.     public   void  run() {  
  68.         while  ( true ) {  
  69.             try  {  
  70.                 //调用线程来绘制字符串   
  71.                 Thread.sleep(500 );  
  72.                 setLoading();  
  73.                 loading.append("." );  
  74.             } catch  (Exception e) {  
  75.             }  
  76.             repaint();  
  77.         }  
  78.     }  
  79. }  

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.thinkrace.icredit; import com.sun.lwuit.Font; import com.sun.lwuit.Form; import com.sun.lwuit.Graphics; import com.sun.lwuit.Image; import com.sun.lwuit.Label; import com.sun.lwuit.layouts.BorderLayout; import java.io.IOException; import java.util.Random; import java.util.Timer; import java.util.TimerTask; /** * * @author Administrator */ public class SplashForm extends Form implements Runnable { private Timer timer = new Timer(); private long displayTime = 1000; private StringBuffer loading = new StringBuffer("Saleslion is loading"); public SplashForm() { Thread t = new Thread(this); t.start(); this.setLayout(new BorderLayout()); this.show(); } public void paint(Graphics g) { try { Image wait = Image.createImage("/logo.png"); //绘制logo g.drawImage(wait, (getWidth() - wait.getWidth()) / 2, (getHeight() - wait.getHeight()- 70) / 2); g.setColor(0xffffff); Font fnt = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE); g.setFont(fnt); int wordWidth = fnt.stringWidth("Saleslion is loading..."); //绘制等待字符串 g.drawString(loading.toString(), (getWidth() - wordWidth) / 2, (getHeight() + wait.getHeight() -60) / 2); } catch (IOException ex) { ex.printStackTrace(); } } private void disappear() { timer.cancel(); try { new LoginForm(); } catch (IOException ex) { ex.printStackTrace(); } } //调用TimerTask,时间到了后,调用disappear(),当前画面消失,跳至另一个Form protected void showNotify() { timer.schedule(new TimerTask() { public void run() { disappear(); } }, displayTime); } //设置字符串 public void setLoading() { if (loading.toString().indexOf("...") > 0) { loading.delete(loading.length() - 3, loading.length()); } } public void run() { while (true) { try { //调用线程来绘制字符串 Thread.sleep(500); setLoading(); loading.append("."); } catch (Exception e) { } repaint(); } } }

以上代码比较简单,也不做多的解释,它是基于Form的。

但是这并不是我想要的效果,假如在一个九宫格中,我点击某一格时,我希望出现一个loading画面显示正在加载这一项,但是以遮罩的形式显示(就 像web开发里面弹出的遮罩层对话框一样),也就是说,弹出loading时,我仍然能够见到原来的九宫格画面。我要的效果如下图:

如果要实现这种形式的loading画面,只有通过Dialog类来实现。

制作这种Dialog有几个小问题需要解决:

1.lwuit中如何显示gif动画

2.Dialog全透明

3.Dialog自动释放

一直没有实现这个效果,关键是问题1,但是在上一节 我已经解决了,下面就看关键代码,代码仍然很简单:


  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */   
  5. package  com.thinkrace.UCHome.ui;  
  6. import  com.sun.lwuit.Dialog;  
  7. import  com.sun.lwuit.Display;  
  8. import  com.sun.lwuit.Image;  
  9. import  com.sun.lwuit.Label;  
  10. import  com.sun.lwuit.util.Resources;  
  11. import  java.io.IOException;  
  12. /**  
  13.  *  
  14.  * @author Administrator  
  15.  */   
  16. public   class  LoadingDialog  extends  Dialog {  
  17.     public  LoadingDialog() {  
  18.         try  {  
  19.             //设置对话框全透明   
  20.             for  ( int  i =  0 ; i < getComponentCount(); i++) {  
  21.                 getComponentAt(i).getStyle().setBgTransparency(0 );  
  22.             }  
  23.             Image icon = Resources.open("/resources.res" ).getImage( "loading.gif" );  
  24.             Label l = new  Label(icon);  
  25.             l.getStyle().setBgTransparency(0 );  
  26.             addComponent(l);  
  27.             int  w = Display.getInstance().getDisplayWidth();  
  28.             int  h = Display.getInstance().getDisplayHeight();  
  29.             int  top = (h - icon.getHeight()) /  2  -  10 ;  
  30.             int  left = (w - icon.getWidth()) /  2  -  10 ;  
  31.               
  32.             setTimeout(3000 );  
  33.             show(top,top,left,left,false );  
  34.               
  35.         } catch  (IOException ex) {  
  36.             ex.printStackTrace();  
  37.         }  
  38.     }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值