Draw text with custom font : Font « 2D Graphics « Android






Draw text with custom font

  


package app.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ImageView;

public class Test extends Activity {
  ImageView drawingImageView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
    Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
        .getDefaultDisplay().getWidth(), (int) getWindowManager()
        .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawingImageView.setImageBitmap(bitmap);

    // Custom Font Text

    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setTextSize(40);
    Typeface chops = Typeface.createFromAsset(getAssets(),
        "ChopinScript.ttf");
    paint.setTypeface(chops);
    float text_x = 120;
    float text_y = 120;
    canvas.drawText("Hello", text_x, text_y, paint);

  }
}

   
    
  








Related examples in the same category

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