stuart-d2
3/6/2014 - 10:47 PM

Shape Drawables : Programmatic Demo : Creating shapes ovals to display in a empty relative layout view container.

Shape Drawables : Programmatic Demo : Creating shapes ovals to display in a empty relative layout view container.

//ShapeDrawACtivity.java

package course.examples.Graphics.ShapeDraw;

//1. Imports
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class ShapeDrawActivity extends Activity {
//2.  Set alpha transparency as an int  
	int alpha = 127;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

//3.  Set image height and width and padding, referencings dimens.xml  
		int width = (int) getResources().getDimension(R.dimen.image_width);
		int height = (int) getResources().getDimension(R.dimen.image_height);
		int padding = (int) getResources().getDimension(R.dimen.padding);

	//4. Get the relative layout container View
		RelativeLayout rl = (RelativeLayout) findViewById(R.id.main_window);

	//5. Create a shape. Create Cyan Shape, subclass OvalShape 
		ShapeDrawable cyanShape = new ShapeDrawable(new OvalShape());
		cyanShape.getPaint().setColor(Color.CYAN);
		cyanShape.setIntrinsicHeight(height);
		cyanShape.setIntrinsicWidth(width);
		cyanShape.setAlpha(alpha);

	//6. Send the shape to the imageview.  Put Cyan Shape into an ImageView
		ImageView cyanView = new ImageView(getApplicationContext());
		cyanView.setImageDrawable(cyanShape);
		cyanView.setPadding(padding, padding, padding, padding);

		//7. Specify placement of ImageView within RelativeLayout
		RelativeLayout.LayoutParams cyanViewLayoutParams = new RelativeLayout.LayoutParams(
				height, width);
		cyanViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		cyanViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		cyanView.setLayoutParams(cyanViewLayoutParams);
		rl.addView(cyanView);

		// Create Magenta Shape
		ShapeDrawable magentaShape = new ShapeDrawable(new OvalShape());
		magentaShape.getPaint().setColor(Color.MAGENTA);
		magentaShape.setIntrinsicHeight(height);
		magentaShape.setIntrinsicWidth(width);
		magentaShape.setAlpha(alpha);

		// Put Magenta Shape into an ImageView
		ImageView magentaView = new ImageView(getApplicationContext());
		magentaView.setImageDrawable(magentaShape);
		magentaView.setPadding(padding, padding, padding, padding);

		// Specify placement of ImageView within RelativeLayout
		RelativeLayout.LayoutParams magentaViewLayoutParams = new RelativeLayout.LayoutParams(
				height, width);
		magentaViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		magentaViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		magentaView.setLayoutParams(magentaViewLayoutParams);
	
	//8.  Add the shape to the relative layout holder
		rl.addView(magentaView);

	}
}

============================
//main.xml view  
// Empty RelativeLayout holder id "main_Window"   

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_window"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

</RelativeLayout>