Search code examples
androidcanvasbattery

Battery circle like Battery Widget Reborn


I am trying to make draw a circle based on the battery percentage. I have the following code:

Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(200, 200, conf);

Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);

circle = new Path();
circle.addCircle(100, 100, level, Direction.CW);

Canvas canvas = new Canvas(bmp);
canvas.drawPath(circle, mPaint);

I am trying to get similar to Battery Widget Reborn, where its a circle path drawn based on a percentage, could someone please help.

Edit:

below is a shot of what i am trying to accomplish

image of the percentage circle


Solution

  • You should be able to do this with Path.addArc(). You just have to define the bounds of the enclosing rectangle, the start angle, and how many degrees to sweep.

    Something like:

    RectF box = new RectF(0,0,bmp.getWidth(),bmp.getHeight());
    float sweep = 360 * level * 0.01f;
    circle.addArc(box, 0, sweep);