明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

J2ME中創(chuàng)建Splash打開界面

[摘要]很顯然,最簡(jiǎn)單的建立Splash啟動(dòng)界面的方法是用Alert ,在應(yīng)用程序啟動(dòng)的時(shí)候用Alert 顯示啟動(dòng)信息和圖片一段時(shí)間直到應(yīng)用程序啟動(dòng)完成.因此用Alert 做啟動(dòng)界面是非常簡(jiǎn)單的,具體實(shí)現(xiàn)方...
很顯然,最簡(jiǎn)單的建立Splash啟動(dòng)界面的方法是用Alert ,在應(yīng)用程序啟動(dòng)的時(shí)候用Alert 顯示啟動(dòng)信息和圖片一段時(shí)間直到應(yīng)用程序啟動(dòng)完成.因此用Alert 做啟動(dòng)界面是非常簡(jiǎn)單的,具體實(shí)現(xiàn)方法可參考如下代碼:



public void showSplashScreen(
                     Display d, Displayable next ){
    Image logo = null;
    
    try {
      logo = Image.createImage(
                              "/images/logo.png" );
    }
    catch( IOException e ){
    }
    
    Alert a = new Alert( "Time Tracker",
           "Copyright 2001 by Nobody, Inc.",
            logo, null );
    a.setTimeout( Alert.FOREVER );
    display.setCurrent( a, next );
}




不過使用Alert 也許不能滿足您靈活的需要,比如需要用任何按鈕取消Splash , 或者顯示一個(gè)簡(jiǎn)單的動(dòng)畫 .這個(gè)時(shí)候,我們可以用Canvas 來代替Alert .請(qǐng)參見如下代碼:



import java.util.*;
import javax.microedition.lcdui.*;

public class SplashScreen extends Canvas {
    private Display     display;
    private Displayable next;
    private Timer       timer = new Timer();

    public SplashScreen(
               Display display, Displayable next ){
        this.display = display;
        this.next    = next;

        display.setCurrent( this );
    }

    protected void keyPressed( int keyCode ){
        dismiss();
    }

    protected void paint( Graphics g ){
        // do your drawing here
    }

    protected void pointerPressed( int x, int y ){
        dismiss();
    }

    protected void showNotify(){
        timer.schedule( new CountDown(), 5000 );
    }

    private void dismiss(){
        timer.cancel();
        display.setCurrent( next );
    }

    private class CountDown extends TimerTask {
        public void run(){
            dismiss();
        }
    }
}




       為了顯示這個(gè)Splash界面,為他建立一個(gè)實(shí)例 當(dāng)你建立一個(gè)SplashScreen的實(shí)例,把它傳遞給MIDlet的顯示類, 你也可以在Splash界面釋放的時(shí)候把Display激活.



public void showSplashScreen(
               Display display, Displayable next ){
    new SplashScreen( display, next );
}


這個(gè)例子,無論是用戶按下了任意鍵或者點(diǎn)擊了屏幕(如果設(shè)備支持觸摸屏的的化),如果什么事件也沒有發(fā)生,Splash屏幕將在第一次顯示5秒后消失.



       我們?cè)诔绦蛑惺裁磿r(shí)候顯示Splash呢?你也許會(huì)想到在MIDlet的startApp方法的最后一行顯示Splash,但請(qǐng)記住,startApp方法在一次進(jìn)程中有可能被調(diào)用多次,構(gòu)造函數(shù)中也不是一個(gè)顯示Splash的好地方,因?yàn)镸IDP的規(guī)范中沒有保證在構(gòu)造的時(shí)候初始化Display 對(duì)象 .所以startApp 是可以顯示Splash的地方,不過一定要注意,只能夠顯示一次. 同時(shí)startApp中也是可以安全獲取到Display對(duì)象 , 所以結(jié)合以上兩點(diǎn),見如下代碼:



import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class MyMIDlet extends MIDlet
                      implements CommandListener {

    private Display display;
    private Command exitCommand = new Command(
                         "Exit", Command.EXIT, 1 );

    public MyMIDlet(){
    }

    protected void destroyApp(
      boolean unconditional )
               throws MIDletStateChangeException {
        exitMIDlet();
    }

    protected void pauseApp(){
    }

    protected void startApp()
                throws MIDletStateChangeException {
        if( display == null ){
                            // first time called...
            initMIDlet();
        }
    }

    private void initMIDlet(){
        display = Display.getDisplay( this );
        new SplashScreen(
                       display, new TrivialForm() );
    }

    public void exitMIDlet(){
        notifyDestroyed();
    }

    public void commandAction(
                        Command c, Displayable d ){
        exitMIDlet();
    }

    // A trivial UI screen

    class TrivialForm extends Form {
        TrivialForm(){
            super( "MyMIDlet" );
            addCommand( exitCommand );
            setCommandListener( MyMIDlet.this );
        }
    }
}




在MIDlet程序中,并非每次客戶啟動(dòng)程序都要顯示Splash , 你應(yīng)該在用戶第一次啟動(dòng)MIDlet的時(shí)候顯示Splash , 下次用戶再啟動(dòng)的時(shí)候就不要再顯示了. 為此,你可以用記錄管理系統(tǒng)(RMS) 存儲(chǔ)程序是否已經(jīng)被調(diào)用的標(biāo)識(shí), 只有當(dāng)RMS中沒有該標(biāo)識(shí)的時(shí)候才顯示Splash ,當(dāng)顯示完Splash 后你就把標(biāo)識(shí)寫入RMS,這樣就不會(huì)重復(fù)調(diào)用了.