below demo, how to use android Canvas to draw the text, and display different Font.

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);

        super.draw(canvas);//, mapView, shadow);
    }

}

reference:
http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/
http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2
http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2

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