PowenKo, Android Tutorial 11-4, TextView, Font change different font

December 21, 2011   ·   0 Comments

Screen Shot 2011-12-21 at 8.50.58 AM

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

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon