Android : Draw rectangles on line of circle on a canvas in android

on Friday, August 29, 2014


I'm drawing a circle and I want on the circle line rectangles that are rotated in the right degree. Like on this image:


enter image description here


With the help off someone on SO I've found a way to do that (almost).


I've got this:


enter image description here


Like you see there are 2 things missing:


1) they are not on the circle line 2) They are not rotated


I know how to rotate on a canvas in android but then they are all mixed up.


This is my code:



int r = 200;

canvas.save();
mPaint.setStyle(Paint.Style.STROKE);

canvas.translate(rect.width() / 2, rect.height() / 2);
canvas.drawCircle(0, 0, r, mPaint);
mPaint.setStyle(Paint.Style.FILL);

for (int alpha = 0; alpha <= 360; alpha += 20) {

double x = r/3 * Math.cos(Math.toRadians(alpha));
double y = r/3 * Math.sin(Math.toRadians(alpha));
canvas.translate((float)x, (float)y);
canvas.drawRect(0, 0, 70, 20, mPaint);

}
canvas.restore();


Can someone point me out on what I'm doing wrong, why they aren't showing on the line of the circle? And second how to rotate, because when I do canvas.rotate(alpha) they aren't in a circle anymore.


0 comments:

Post a Comment