please create a new folder called “fonts” on assets, and copt your ttf font file to there,

RockFont.ttf and FEASFBRG.TTF.

like below.

package com.powenko;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

public class Tutorial_UI_TextView_TextViewOutlineActivity extends Activity {
    /** Called when the activity is first created. */
	private myView mv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);
   // }
         mv = new myView(this);
        setContentView(mv);

    }
}

class myView extends View {

public myView(Context context) {
   super(context);
}

    @Override
    public void draw(Canvas canvas){

    	Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		canvas.drawColor(Color.BLACK);
		canvas.drawBitmap(kangoo, 130, 10, null);

    	Paint t_paint = new Paint();

    	t_paint.setColor(Color.rgb(0, 0, 255)); //Color.CYAN);

    	canvas.drawRect(0,0,320,480, t_paint);

    	//Typeface tf = Typeface.create("Helvetica",Typeface.DEFAULT_BOLD);
        Paint strokePaint = new Paint();
        strokePaint.setARGB(255, 255, 0, 0);
        strokePaint.setTextAlign(Paint.Align.CENTER);
        strokePaint.setTextSize(24);
        strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(2);

        Paint textPaint = new Paint();
        textPaint.setARGB(255, 255, 255, 255);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(24);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);

        canvas.drawText("Some Text", 100, 100, strokePaint);
        canvas.drawText("Some Text", 100, 100, textPaint);

        drawFont(canvas, "fonts/RockFont.ttf", 50, "RockFont 93");

        drawFont(canvas, "fonts/FEASFBRG.TTF", 150, "FEASFBRG");

        super.draw(canvas);//, mapView, shadow);
    }
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Typeface mFace;
    void drawFont(Canvas canvas, String path, int y, String name)
    {
    	mPaint.setTextSize(34);
    	mPaint.setColor(Color.WHITE);

    mFace = Typeface.createFromAsset(getContext().getAssets(), path);

    mPaint.setTypeface(mFace);
    canvas.drawText(name, 30, y, mPaint);

    }

}

sample code:

Tutorial_UI_TextView_TextViewOutlineActivity

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply