postheadericon Android Shapes Example using Java code





MainActivity.java

package com.example.shapdemo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
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.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

  private void setShapeByDrawable(Drawable drawable) {

ImageView reusableImageView = (ImageView) findViewById(R.id.ImageViewForShape);
reusableImageView.setImageDrawable(drawable);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

if (item.getItemId() == R.id.mnuArc) {
ShapeDrawable d = new ShapeDrawable(new ArcShape(0, 345));
d.setIntrinsicHeight(100);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.MAGENTA);
setShapeByDrawable(d);

} else if (item.getItemId() == R.id.mnuOval) {
ShapeDrawable d = new ShapeDrawable(new OvalShape());
d.setIntrinsicHeight(40);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.RED);
setShapeByDrawable(d);
} else if (item.getItemId() == R.id.mnuPath) {
Path p = new Path();
p.setFillType(Path.FillType.EVEN_ODD); // another fill type
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 d = new ShapeDrawable(new PathShape(p, 100, 100));
d.setIntrinsicHeight(100);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.YELLOW);
setShapeByDrawable(d);

} else if (item.getItemId() == R.id.mnuRectangle) {
ShapeDrawable d = new ShapeDrawable(new RectShape());
d.setIntrinsicHeight(2);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.MAGENTA);
setShapeByDrawable(d);

} else if (item.getItemId() == R.id.mnuRoundRectangle) {
ShapeDrawable d = new ShapeDrawable(new RoundRectShape(new float[] {
5, 5, 5, 5, 5, 5, 5, 5 }, null, null));
d.setIntrinsicHeight(50);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.CYAN);
setShapeByDrawable(d);
} else if (item.getItemId() == R.id.mnuRoundRectangleInner) {
float[] outerRadii = new float[] { 6, 6, 6, 6, 6, 6, 6, 6 };
RectF insetRectangle = new RectF(8, 8, 8, 8);
float[] innerRadii = new float[] { 6, 6, 6, 6, 6, 6, 6, 6 };

ShapeDrawable d = new ShapeDrawable(new RoundRectShape(outerRadii,
insetRectangle, innerRadii));
d.setIntrinsicHeight(50);
d.setIntrinsicWidth(100);
d.getPaint().setColor(Color.BLACK);
setShapeByDrawable(d);

}
  return true;
}
}


res / layout / activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/ImageViewForShape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="115dp"
        android:minHeight="90dp"
        android:minWidth="90dp" >
    </ImageView>

</LinearLayout>

res / menu / activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/mnuRectangle"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Rectangle"/>
    <item
        android:id="@+id/mnuOval"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Oval"/>
    <item
        android:id="@+id/mnuRoundRectangle"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Round Rectangle"/>
    <item
        android:id="@+id/mnuRoundRectangleInner"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Round Inner Rectangle"/>
    <item
        android:id="@+id/mnuPath"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Path - Star"/>
    <item
        android:id="@+id/mnuArc"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Arc"/>  

</menu>


0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.