Canvas (Drawing various shapes)

Canvas (Rectangle, Rounded Rectangle, Circle, Arc, 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.canvasredcircle;

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.RectF;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.View;

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

class ViewWithRedDot extends View {

	public ViewWithRedDot(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint p1 = new Paint(Paint.ANTI_ALIAS_FLAG);
		canvas.drawColor(Color.BLACK);
		p1.setColor(Color.WHITE);

		// //canvas.drawColor(Color.BLACK);
		Paint circlePaint = new Paint();
		circlePaint.setColor(Color.rgb(255, 255, 255));
		circlePaint.setStyle(Paint.Style.STROKE);
		circlePaint.setColor(Color.RED);
		canvas.drawRect(100, 100, 175, 175, circlePaint);
		circlePaint.setColor(Color.WHITE);
		canvas.drawCircle(25, 25, 10, circlePaint);
		RectF rf = new RectF(75, 75, 200, 200);
		canvas.drawArc(rf, 180, 255, true, circlePaint);
		rf = new RectF(10, 10, 150, 150);
		canvas.drawRoundRect(rf, 250, 50, circlePaint);
		canvas.drawText("hello", 100, 100, 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