Canvas (Draw Text)

Canvas (Drawing Text)

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

Activity File

package com.androidwork.textdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

public class TextDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new ViewWithRedDot(this));
	}
}

class ViewWithRedDot extends View {
	public ViewWithRedDot(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		Paint circlePaint = new Paint();
		Typeface mType = Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC);
		circlePaint.setTextSize(25);
		circlePaint.setColor(Color.RED);
		circlePaint.setTypeface(mType);
		canvas.drawText("Default Typeface", 50, 100, circlePaint);
		//
		circlePaint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
		circlePaint.setColor(Color.YELLOW);
		canvas.drawText("Underline Text Flag", 50, 120, circlePaint);
		//
		circlePaint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		circlePaint.setColor(Color.GREEN);
		canvas.drawText("Strike Thru Text Flag", 50, 140, circlePaint);
		//
		circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(Color.WHITE);
		canvas.drawText("Anti Alias Flag", 50, 160, circlePaint);
		//
		circlePaint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
		circlePaint.setColor(Color.WHITE);
		canvas.drawText("Dev Kern Text Flag", 50, 180, circlePaint);
		//
		circlePaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
		circlePaint.setColor(Color.CYAN);
		canvas.drawText("Fake Bold Text Flag", 50, 200, circlePaint);
	}
}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s