LunarViewpublic class LunarView extends android.view.SurfaceView implements SurfaceHolder.CallbackView 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 | mContextHandle to the application context, used to e.g. fetch Drawables. | private android.widget.TextView | mStatusTextPointer to the text view to display "Paused.." etc. | private LunarThread | threadThe 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$LunarThread | getThread()Fetches the animation thread corresponding to this LunarView.
return thread;
| public boolean | onKeyDown(int keyCode, android.view.KeyEvent msg)Standard override to get key-press events.
return thread.doKeyDown(keyCode, msg);
| public boolean | onKeyUp(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 void | onWindowFocusChanged(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 void | setTextView(android.widget.TextView textView)Installs a pointer to the text view used for messages.
mStatusText = textView;
| public void | surfaceChanged(android.view.SurfaceHolder holder, int format, int width, int height)
thread.setSurfaceSize(width, height);
| public void | surfaceCreated(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 void | surfaceDestroyed(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) {
}
}
|
|