简体   繁体   中英

Android Splash Screen before black screen

I would like to display a splash screen while everything is initializing in the onCreate() method, yet components that I need to draw things to the screen are also initializing, therefore there's a black screen when I start the app and after the onCreate() method has completed then only is the first screen drawn. Instead of having the black screen I'd like a splash screen.

Here's my code in the onCreate method:

@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!");
        }

    }

How should I implement a splash screen, to avoid the black screen at the beginning?

//avoid time consuming work in UI thread but if that is related to UI itself then do as below

@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);
        }

    }

Create a XML layout for this splash screen and set it as content view right after your super.onCreate():

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

That should be enough. It would display this splash screen until your setContentView(renderView) is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM