Font Test Activity : Font « 2D Graphics « Android






Font Test Activity

  
package app.test;

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 Test 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 a test";
      paint.setColor(Color.WHITE);
      paint.setTextSize(18);
      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 savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new RenderView(this));
  }
}

   
    
  








Related examples in the same category

1.Using Typeface to create ttf file
2.Draw text with custom font
3.Typefaces Demo
4.Font Sampler
5.Set text to use bold and italic font style
6.Using FontMetrics