Skip to content Skip to sidebar Skip to footer

How To Draw A Half Circle In Android

I'm using this code to draw a half in my app:

Solution 1:

I would suggest to draw it through code.

1- Create class MyView and put below code.

publicclassMyViewextendsView {

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

    @OverrideprotectedvoidonDraw(Canvas canvas) {

        // TODO Auto-generated method stubsuper.onDraw(canvas);
        floatwidth= (float) getWidth();
        floatheight= (float) getHeight();
        float radius;

        if (width > height) {
         radius = height / 4;
        } else {
         radius = width / 4;
        }

        Pathpath=newPath();
        path.addCircle(width / 2,
         height / 2, radius,
         Path.Direction.CW);

        Paintpaint=newPaint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);

        float center_x, center_y;
        finalRectFoval=newRectF();
        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);
    }
}

2-Initialize this class inside you activity or fragment:-

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(newMyView(this));
}

Solution 2:

Your can use a rectangle shape .xml file and edit the corners on one side only.

Example:

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:height="30dp"android:width="30dp"/><solidandroid:color="@color/black"/><cornersandroid:topLeftRadius="15dp"android:bottomLeftRadius="15dp"/></shape>

Solution 3:

Solution 4:

this is how i created my semi circle in a drawable xml file.

<sizeandroid:width="180dp"android:height="90dp"></size><cornersandroid:topLeftRadius="200dp"android:topRightRadius="200dp"></corners>

Solution 5:

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:width="180dp"android:height="90dp"></size><cornersandroid:topLeftRadius="200dp"android:topRightRadius="200dp"></corners><strokeandroid:width="5px"android:color="@color/black" /></shape>

Post a Comment for "How To Draw A Half Circle In Android"