电池类圆电池的Widget重生(Battery circle like Battery Widget

2019-08-17 02:42发布

我试图让借鉴基础上的电池百分比的圆。 我有以下代码:

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

我试图得到类似电池的Widget重生,其中基于百分比的方式圆路径绘制的,可能有人请帮助。

编辑:

下面是什么,我试图完成一个镜头

Answer 1:

你应该能够做到这一点Path.addArc() 你只需要定义外接矩形,起始角度的界限,多少度扫。

就像是:

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


Answer 2:

你可以使用这个类作为一个完整的工作示例。 您还可以添加在视图中的文本,但超越的问题...

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

    }

}


文章来源: Battery circle like Battery Widget Reborn