Shapes in Android Studio?


i want to draw a circle, semicircle and triangle shape in android studio. Is there any simple way to draw shapes in android studio or should i use images for each shape?

i tried this as drawable source

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

but its not good enough to create exact outer shape of circle and other shapes. Is there any other way to do it?

semicircletriangle circle


Answers:


you can use following class for Draw Half Circle

public class MyView extends View {


    public MyView(Context context) {

        super(context);

        // TODO Auto-generated constructor stub

    }


    @Override

    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub

        super.onDraw(canvas);

        float width = (float) getWidth();

        float height = (float) getHeight();

        float radius;


        if (width > height) {

            radius = height / 4;

        } else {

            radius = width / 4;

        }


        Path path = new Path();

        path.addCircle(width / 2,

                height / 2, radius,

                Path.Direction.CW);


        Paint paint = new Paint();

        paint.setColor(Color.BLACK);

        paint.setStrokeWidth(5);


        paint.setStyle(Paint.Style.FILL);

        float center_x, center_y;
        final RectF oval = new RectF();


        paint.setStyle(Paint.Style.STROKE);
        center_x = width / 2;

        center_y = height / 2;

        oval.set(center_x - radius,

                center_y - radius,

                center_x + radius,

                center_y + radius);

        canvas.drawArc(oval, 90, 180, false, paint);


    }

}

Output

enter image description here