开发者

Switching between Views in Android

In my android game I want to display a screen when it loads up with my logo saying I built the game, then after a few seconds I want this to switch to the menu screen, and开发者_JS百科 then after a game is started, the screen changes to the game screen for the action to start.

I looked in to ViewFlipper but I have since concluded this is not right for the job. My question therefore is how does one go about controlling what screen/View is displayed programatically? Also, at the end of a level I want the screen to switch to a 'Stage Cleared' view before loading back in to the action.

Many thanks


Use a different Activity for each screen you want to display. For example, use an Activity to display your splash screen and once you're done showing that, start your main menu Activity from within your splash screen Activity.


Edit:

public class SplashActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_activity);
    }

    public void onResume() {
        super.onResume();
        self = this; // We need a reference to this Activity below.
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(5000); // Sleep for 5 seconds
                    Intent i = new Intent(self, MainMenuActivity.class);
                    self.startActivity(i);
                    self.finish();
                } catch (InterruptedException e) {
                    // Catch exception
                }
            }
        }.start();
    }
}

This starts SplashActivity for 5 seconds and then starts MainMenuActivity and finishes itself. The splash screen is initialized through setContentView(R.layout.splash_activity);; the layout should dictate how your logo is drawn. This also means the splash screen doesn't animate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜