黑屏前的Android Splash Screen

时间:2012-06-10 09:09:35

标签: java android splash

我希望在onCreate()方法中初始化所有内容时显示启动画面,但是我需要将内容绘制到屏幕上的组件也在初始化,因此当我启动应用程序时和之后有一个黑屏onCreate()方法已经完成,然后才会绘制第一个屏幕。而不是黑屏,我喜欢闪屏。

这是我在onCreate方法中的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Acquire a wakeLock to prevent the phone from sleeping
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");

        // Setup all the Game Engine components 
        gameEngineLog = new WSLog("WSGameEngine");
        gameEngineLog.setLogType(this.gameEngineLogType);
        gameLog = new WSLog(this.gameLogTAG);
        gameLog.setLogType(this.gameLogType);
        io = new FileIO(this, getAssets());
        audio = new Audio(this);
        wsScreen = new WSScreen(this, this.screenResizeType, this.customTopYGap, this.customLeftXGap, this.gameScreenWidth, this.gameScreenHeight);
        graphics = new Graphics(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended());
        renderView = new RenderView(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended(), FPS, maxFrameskippes);
        input = new Input(this, renderView, logGameEngineInputLog);
        setContentView(renderView);

        if(useOfAnalytics == true) {
            getGameEngineLog().w(classTAG, "Analytics has been enabled");
            analytics = new Analytics(this);
        }

        // Check that the developer has initialized the assets
        if(this.assets == null) {
            this.gameEngineLog.w(classTAG, "The assets for the game haven't been defined!");
        }

    }

我应该如何实现启动画面,以避免开始时出现黑屏?

2 个答案:

答案 0 :(得分:3)

//避免在UI线程中耗费大量时间,但如果这与UI本身有关,请按以下步骤进行操作

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


            setContentView(R.layout.first_splash_lauoy);

            new Handler().postDelayed(new Runnable() {

                public void run() {
                  //Your time consuming work 
                  //with spiiner(if needed) & setContentView(<finalView>)
                  }, 1000);
        }

    }

答案 1 :(得分:3)

为此初始屏幕创建XML布局,并在super.onCreate()之后立即将其设置为内容视图:

super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);

那应该够了。它将显示此启动屏幕,直到调用setContentView(renderView)。