Game Development Reference
In-Depth Information
Putting It All Together
Let's write a quick test activity that demonstrates the preceding methods. This time, we want
you to analyze the code in Listing 4-13 first. Figure out where on a 480×800 screen in portrait
mode the different shapes will be drawn. When doing graphics programming, it is of utmost
importance to imagine how the drawing commands you issue will behave. It takes some
practice, but it really pays off.
Listing 4-13. ShapeTest.java; Drawing Shapes Like Crazy
package
com.badlogic.androidgames;
import
android.app.Activity;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Paint.Style;
import
android.os.Bundle;
import
android.view.View;
import
android.view.Window;
import
android.view.WindowManager;
public class
ShapeTest
extends
Activity {
class
RenderView
extends
View {
Paint paint;
public
RenderView(Context context) {
super
(context);
paint =
new
Paint();
}
protected void
onDraw(Canvas canvas) {
canvas.drawRGB(255, 255, 255);
paint.setColor(Color.
RED
);
canvas.drawLine(0, 0, canvas.getWidth()-1, canvas.getHeight()-1, paint);
paint.setStyle(Style.
STROKE
);
paint.setColor(0xff00ff00);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 40, paint);
paint.setStyle(Style.
FILL
);
paint.setColor(0x770000ff);
canvas.drawRect(100, 100, 200, 200, paint);
invalidate();
}
}
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.
FEATURE_NO_TITLE
);
getWindow().setFlags(WindowManager.LayoutParams.
FLAG_FULLSCREEN
,