FileDocCategorySizeDatePackage
LunarView.javaAPI DocAndroid 1.5 API33083Wed May 06 22:41:08 BST 2009com.example.android.lunarlander

LunarView

public class LunarView extends android.view.SurfaceView implements SurfaceHolder.Callback
View that draws, takes keystrokes, etc. for a simple LunarLander game. Has a mode which RUNNING, PAUSED, etc. Has a x, y, dx, dy, ... capturing the current ship physics. All x/y etc. are measured with (0,0) at the lower left. updatePhysics() advances the physics based on realtime. draw() renders the ship, and does an invalidate() to prompt another draw() as soon as possible by the system.

Fields Summary
private android.content.Context
mContext
Handle to the application context, used to e.g. fetch Drawables.
private android.widget.TextView
mStatusText
Pointer to the text view to display "Paused.." etc.
private LunarThread
thread
The thread that actually draws the animation
Constructors Summary
public LunarView(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);

        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        // create thread only; it's started in surfaceCreated()
        thread = new LunarThread(holder, context, new Handler() {
            @Override
            public void handleMessage(Message m) {
                mStatusText.setVisibility(m.getData().getInt("viz"));
                mStatusText.setText(m.getData().getString("text"));
            }
        });

        setFocusable(true); // make sure we get key events
    
Methods Summary
public com.example.android.lunarlander.LunarView$LunarThreadgetThread()
Fetches the animation thread corresponding to this LunarView.

return
the animation thread

        return thread;
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent msg)
Standard override to get key-press events.

        return thread.doKeyDown(keyCode, msg);
    
public booleanonKeyUp(int keyCode, android.view.KeyEvent msg)
Standard override for key-up. We actually care about these, so we can turn off the engine or stop rotating.

        return thread.doKeyUp(keyCode, msg);
    
public voidonWindowFocusChanged(boolean hasWindowFocus)
Standard window-focus override. Notice focus lost so we can pause on focus lost. e.g. user switches to take a call.

        if (!hasWindowFocus) thread.pause();
    
public voidsetTextView(android.widget.TextView textView)
Installs a pointer to the text view used for messages.

        mStatusText = textView;
    
public voidsurfaceChanged(android.view.SurfaceHolder holder, int format, int width, int height)

        thread.setSurfaceSize(width, height);
    
public voidsurfaceCreated(android.view.SurfaceHolder holder)

        // start the thread here so that we don't busy-wait in run()
        // waiting for the surface to be created
        thread.setRunning(true);
        thread.start();
    
public voidsurfaceDestroyed(android.view.SurfaceHolder holder)

        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }