Drawing Shapes (Using ShapeDrawable)

Drawing Shapes (Using ShapeDrawable)

Note : Uncomment code to see various shapes
/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">
	<ImageView android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:id="@+id/ImageView1" />
</LinearLayout>

/res/drawable/green_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<solid android:color="#0f0" />
</shape>

/res/drawable/green_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<solid android:color="#0f0" />
</shape>

/res/drawable/shaperred.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="oval">
	<gradient android:startColor="#f00" android:centerColor="#fffa0f"
		android:endColor="#f0f" android:angle="180" />
	<stroke android:width="3dp" android:color="#00f"
		android:dashWidth="5dp" android:dashGap="3dp" />
</shape>

Activity file

package com.androidwork.shapesdemo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class ShapesDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// ////////Getting shapes from xml file //////////////
		// ImageView iv = (ImageView) findViewById(R.id.ImageView1);
		// // iv.setImageResource(R.drawable.green_rect);
		// iv.setImageResource(R.drawable.shaperred);
		// ///////////////////////////////////

		// //////////////Drawing Rectangle Programmatically////////////////////

		// ShapeDrawable rect = new ShapeDrawable(new RectShape());
		// rect.setIntrinsicHeight(20);
		// rect.setIntrinsicWidth(100);
		// rect.getPaint().setColor(Color.MAGENTA);
		//
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rect);

		// ///////// Rounded rectangle Programmatically/////////////////

		// ShapeDrawable rndrect = new ShapeDrawable(
		// new RoundRectShape( new float[] { 5, 15, 5, 15, 5, 15, 5, 15 },
		// null, null));
		// rndrect.setIntrinsicHeight(50);
		// rndrect.setIntrinsicWidth(100);
		// rndrect.getPaint().setColor(Color.CYAN);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rndrect);

		// ////////////Testing RoundedRect //////////////
		//
		// float[] outerRadii = new float[]{ 16,16, 6, 6, 16,16, 6,6 };
		// RectF insetRectangle = new RectF(8, 8, 8, 8);
		// float[] innerRadii = new float[]{ 16, 16, 6, 6, 16, 16, 6, 6 };
		//
		// //
		// //// float[] outerRadii = new float[]{ 6,6, 6, 6, 6,6, 6,6 };
		// //// RectF insetRectangle = new RectF(10, 10, 10, 10);
		// //// float[] innerRadii = new float[]{ 16, 16, 16, 16, 16, 16, 16, 16
		// };
		// ShapeDrawable rndrect = new ShapeDrawable(
		// new RoundRectShape(
		// outerRadii,insetRectangle , innerRadii));
		// rndrect.setIntrinsicHeight(50);
		// rndrect.setIntrinsicWidth(100);
		// rndrect.getPaint().setColor(Color.WHITE);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rndrect);

		// //////Drawing oval & Circle programmatically /////////////
		// ShapeDrawable oval = new ShapeDrawable(new OvalShape());
		// oval.setIntrinsicHeight(10);
		// oval.setIntrinsicWidth(50);
		// oval.getPaint().setColor(Color.CYAN);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(oval);

		// //////////Drawing Arc///////////////////

		ShapeDrawable pacMan = new ShapeDrawable(new ArcShape(0, 340));
		pacMan.setIntrinsicHeight(100);
		pacMan.setIntrinsicWidth(100);
		pacMan.getPaint().setColor(Color.MAGENTA);
		ImageView iView = (ImageView) findViewById(R.id.ImageView1);
		iView.setImageDrawable(pacMan);

		// ////////////Drawing Path///////////////////

		// Path p = new Path();
		// // p.mo
		// p.moveTo(50, 0);
		// p.lineTo(25,100);
		// p.lineTo(100,50);
		// p.lineTo(0,50);
		// p.lineTo(75,100);
		// p.lineTo(50,0);
	
		// ShapeDrawable star =
		// new ShapeDrawable(new PathShape(p, 100, 100));
		// star.setIntrinsicHeight(100);
		// star.setIntrinsicWidth(100);
		// star.getPaint().setColor(Color.YELLOW);
	
		// star.getPaint().setStyle(Paint.Style.STROKE);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(star);
	}
}

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