PowenKo, Android Tutorial 087-2, Canvas, Draw text

December 19, 2011   ·   0 Comments

Screen Shot 2011-12-19 at 2.21.53 PM


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

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