Marx
Marx

Reputation: 71

Position of half circle with DrawArc (Android)

I'm trying to draw a half circle inside the ImageView but i can't control where draw it. The idea is to draw it inside another circle with another color using drawcircle (50, 50, 20, paint). This is the code i'm using:

<ImageView android:id="@+id/circle" 
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_gravity="center_horizontal" 
android:layout_marginTop="10sp" />

Bitmap bmp = Bitmap.createBitmap(100, 70, Bitmap.Config.RGB_565);
ImageView circle = (ImageView) findViewById (R.id.circle); 
Paint paint = new Paint(); 
paint.setColor(Color.RED); 
Canvas canvas = new Canvas (bmp); 
RectF rectf = new RectF (0, 0, 40, 40); 
canvas.drawArc(rectf, 0, 180, true, paint);

Thanks.

Upvotes: 0

Views: 2650

Answers (1)

kaho
kaho

Reputation: 4784

I think the problem you are having is: you do not understand the two different representations that is used by Circle and Arc.

For Circle, you need to give it the x, y position of the center, and the radius. but for the Arc, you need the 4 edges of the container.

so it should look something like this:

//setting for Circle
    paint.setColor(Color.RED);
    int xPosition=50;
    int yPosition=50;
    int size = 40;
    canvas.drawCircle(xPosition, yPosition, size/2, paint);
//setting for Arc
    paint.setColor(Color.YELLOW);
    size = size * 8 / 10;             // make the Arc smaller
    xPosition=xPosition-size/2;
    yPosition=yPosition-size/2;
    RectF rectf = new RectF (xPosition, yPosition, xPosition+size, yPosition+size);
    canvas.drawArc(rectf, 0+45, 180, true, paint);

Upvotes: 2

Related Questions