Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behaviour in drawing a ring using Path.arcTo() in Android

I have implemented an animated drawing ring by start sweeping at angle 0 to 360 successfully. However when the tail of the ring meets its head at 360 angle, all drawing is disappear.

This is my code for the ring in onDraw()

        float startAngle = 270;
        float sweepAngle = -359;

        paint.setColor(Color.DKGRAY);
        paint.setShadowLayer(4, 2, 2, 0x80000000);

        rectF.set(cenX - outerRadius, cenY - outerRadius, cenX + outerRadius,
                cenY + outerRadius);
        path.arcTo(rectF, startAngle, sweepAngle);
        //canvas.drawArc(rectF, 0, 360, false, paint);
        rectF.set(cenX - innerRadius, cenY - innerRadius, cenX + innerRadius,
                cenY + innerRadius);
        /*paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawArc(rectF, 0, 360, false, paint);*/
        path.arcTo(rectF, startAngle + sweepAngle, -(sweepAngle));

        canvas.drawPath(path, paint);

and this is the result,

enter image description here

notice here that i set sweepAngle to -359 just before it becomes circle. However if I change sweepAngle to -360. It produces this result.

enter image description here

It disappears!! Anyone know how to solve this please help me?

Thanks.

PS. I don't want to use drawArc() because I want to make a hole inside the ring. With drawArc(), my button will be gone.

like image 458
SaintTail Avatar asked Oct 15 '13 14:10

SaintTail


2 Answers

I suppose android will mod 360 before really drawing it. So x - 360 === x and it will draw nothing!

like image 196
Pylipala Avatar answered Sep 29 '22 00:09

Pylipala


I'm way late with this, but I solved it by adding a solid circle as the last frame of the animation (I was using AnimationDrawable, but the idea is the same in this case). All the draw code was the same for that last frame, except one line:

replace:

mPath.arcTo(rectF, startAngle, sweepAngle);

with something like:

mPath.addCircle(cenX, cenY, innerRadius, Path.Direction.CCW);
like image 27
Kevin Newman Avatar answered Sep 29 '22 00:09

Kevin Newman