knennigtri
knennigtri

Reputation: 197

Android: Using drawArc() with different arc sizes with all the same center

I am trying to draw a pie chart in a slightly different way than the conventional way. The pie has equal pieces no matter what, but then each piece's radius is different. So all the arcs are still centered at one point but have different radiuses.

Here is the simplified code for it.

rect1 = new RectF(0,0,8,8)
rect2 = new RectF(0,0,6,6)
rect3 = new RectF(0,0,4,4)
rect4 = new RectF(0,0,2,2)

canvas.drawArc(rect1,0,90,true, paint)
canvas.drawArc(rect2,90,90,true, paint)
canvas.drawArc(rect3,180,90,true, paint)
canvas.drawArc(rect4,270,90,true, paint)

This creates all the correct arcs, but the point of all the arcs are not centered at the same place. I understand that this is because of how the RectF class works.

So my question is, can I line up these different Arcs in the center of the canvas? Is there an arc offset somewhere that I can use to do this?

I tried this solution with paint, but was unsuccessful. Any suggestions would be helpful!

Upvotes: 2

Views: 2759

Answers (1)

kabuko
kabuko

Reputation: 36302

It's not really based on "how the RectF class works" as much as just how you're placing those rectangles. Instead of aligning the upper left of all of these rectangles at (0, 0), align the centers. A little basic math will get you this. Assuming a center point (centerX, centerY) and a radius radius, the left side will be centerX - radius, the top will be centerY - radius the right will be centerX + radius, the bottom will be centerY + radius. Do that for each rectangle with the same center point and different radii and unsurprisingly, you'll end up with arcs with the same center.

Upvotes: 4

Related Questions