2

我正在尝试根据电池百分比画一个圆圈。我有以下代码:

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);

我正在尝试类似于 Battery Widget Reborn,它是基于百分比绘制的圆形路径,有人可以帮忙。

编辑:

下面是我想要完成的一个镜头

百分比圆的图像

4

2 回答 2

4

您可以将此类用作完整的工作示例。您也可以在视图中添加文本,但这超出了问题范围......

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

public class DrawView extends View {

    Paint mPaint = new Paint();

    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {

        Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG |
                Paint.DITHER_FLAG |
                Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither(true);
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(1);

        int size = 200;
        int radius = 190;
        int delta = size - radius;
        int arcSize = (size - (delta / 2)) * 2;
        int percent = 42;

        //Thin circle
        canvas.drawCircle(size, size, radius, mPaint);

        //Arc
        mPaint.setColor(Color.parseColor("#33b5e5"));
        mPaint.setStrokeWidth(15);
        RectF box = new RectF(delta,delta,arcSize,arcSize);
        float sweep = 360 * percent * 0.01f;
        canvas.drawArc(box, 0, sweep, false, mPaint);

    }

}
于 2013-03-02T19:33:46.347 回答
2

您应该可以使用Path.addArc(). 您只需要定义封闭矩形的边界、起始角度以及要扫描的度数。

就像是:

RectF box = new RectF(0,0,bmp.getWidth(),bmp.getHeight());
float sweep = 360 * level * 0.01f;
circle.addArc(box, 0, sweep);
于 2013-02-20T20:25:05.977 回答