Putting It All Together

Enough talk, let's do some more coding. Listing 4-15 shows you text rendering in action.

Listing 4-15. The FontTest Activity 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.Rect; import android.graphics.Typeface; import android.os.Bundle;

import android.view.View; import android.view.Window; import android.view.WindowManager;

public class FontTest extends Activity { class RenderView extends View { Paint paint; Typeface font; Rect bounds = new Rect();

public RenderView(Context context) { super(context); paint = new Paint();

font = Typeface.createFromAsset(context.getAssets(), "font.ttf");

protected void onDraw(Canvas canvas) { paint.setColor(Color.YELLOW); paint.setTypeface(font); paint.setTextSize(28); paint.setTextAlign(Paint.Align.CENTER);

canvas.drawText("This is a test!", canvas.getWidth() / 2, 100, paint);

String text = "This is another test o_O"; paint.setColor(Color.WHITE); paint.setTextSize(l8); paint.setTextAlign(Paint.Align.LEFT); paint.getTextBounds(text, 0, text.length(), bounds); canvas.drawText(text, canvas.getWidth() - bounds.width(), 140, paint); invalidate();

^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new RenderView(this));

We won't discuss the onCreate() method of the activity, since we've seen it before.

Our RenderView implementation has three members: a Paint, a Typeface, and a Rect, where we'll store the bounds of a text string later on.

In the constructor we create a new Paint instance and load a font from the file font.ttf in the assets/ directory.

In the onDraw() method, we set the Paint to the color yellow, set the font and its size, and specify the text alignment to be used when interpreting the coordinates in the call to

Canvas.drawText(). The actual drawing call renders the string This is a test!, centered horizontally at coordinate 100 on the y-axis.

For the second text-rendering call, we do something else: we want the text to be right-aligned with the right edge of the screen. We could do this by using Paint.Align.RIGHT and an x-coordinate of Canvas.getWidth() - 1. Instead we do it the hard way by using the bounds of the string to practice very basic text layout a little. We also change the color and the size of the font for rendering. Figure 4-15 shows the output of this activity.

This is a test!

This is another test oj

Figure 4-15. Fun with text (480x800-pixel resolution)

Another mystery of the Typeface class is that it does not allow us to explicitly release all its resources. We have to rely on the garbage collector to do the dirty work for us.

NOTE: We only scratched the surface of text rendering here. If you want to know more . . . well, I guess by now you know where to look.

0 0

Post a comment