Going Full Screen

Before we dive headfirst into drawing our first shapes with the Android APIs, let's fix something else. Up until this point, all our activities have shown their title bars. The notification bar was visible as well. We'd like to immerse our players a little bit more by getting rid of those. We can do that with two simple calls:

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(windowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.F LAG_FULLSCREEN);

The first call gets rid of the activity's title bar. To make the activity go full-screen and thus eliminate the notification bar as well, we call the second method. Note that we have to call these methods before we set the content view of our activity.

Listing 4-11 shows you a very simple test activity that demonstrates how to go fullscreen.

Listing 4-11. FullScreenTest.java; Making Our Activity Go Full-Screen package com.badlogic.androidgames;

import android.os.Bundle; import android.view.Window; import android.view.WindowManager;

public class FullScreenTest extends SingleTouchTest {

^Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(windowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState);

What's happening here? We simply derive from the TouchTest class we created earlier and override the onCreate() method. In the onCreate() method, we enable full-screen mode and then call the onCreate() method of the superclass (in this case the TouchTest activity), which will set up all the rest of the activity. Note again that we have to call those two methods before we set the content view. Hence, the superclass onCreate() method is called after we execute these two methods.

We also fixed the orientation of the activity to portrait mode in the manifest file. You didn't forget to add <activity> elements in the manifest file for each test we wrote, right? From now on we'll always fix it to either portrait or landscape mode, since we don't want a changing coordinate system all the time.

By deriving from TouchTest, we have a fully working example with which we can explore the coordinate system we are going to draw in. The activity will show you the coordinates at which you touch the screen, as in the old TouchTest example. The difference is that this time we are full-screen, which means that the maximum coordinates of our touch events are equal to the screen resolution (minus one in each dimension, as we start at [0,0]). For a Nexus One, the coordinate system would span the coordinates (0,0) to (479,799) in portrait mode (for a total of 480x800 pixels).

While it may seem that the screen is redrawn continuously, it actually is not. Remember from our TouchTest class that we update the TextView every time a touch event is processed. This in turn makes the TextView redraw itself. If we don't touch the screen, the TextView will not redraw itself. For a game, we need to be able to redraw the screen as often as possible, preferably within our main loop thread. We'll start off easy, though, and begin with continuous rendering in the UI thread.

0 0

Post a comment