Draw a hollow circle on an Android Canvas

2019-05-21 18:54发布

问题:

I am working on creating a custom view where a user can select an angle. Below is an example of what the end result should look like:

I am achieving this with the following code:

mPaint.setColor(Color.BLACK);
canvas.drawCircle((int) (mSize.right / 2), (int) (mSize.bottom / 2),
        mWidthOutside, mPaint);

mPaint.setColor(Color.LTGRAY);
canvas.drawCircle((int) (mSize.right / 2), (int) (mSize.bottom / 2),
        mWidthInside, mPaint);

The problem with doing it this way, is the background is a static LTGRAY, which I hope to make Transparent.

How would I go about leaving the center of the circle transparent?

I have tried the following hoping the the drawArc function would only create a line the width of the paint, and not fill the center. It does in face fill the center.

RectF rectF = new RectF(centerX - mRadius, centerY - mRadius, centerX
            + mRadius, centerY + mRadius);
canvas.drawArc(rectF, 0, 360, false, mPaint);

Suggestions on how to keep the center of the circle transparent?

回答1:

As asked :) The solution is to set the style to be Paint.Style.STROKE



回答2:

Hollow Circle in canvas.

Bitmap bitmap=Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
    Canvas canvas=new Canvas(bitmap);
    //canvas.clipPath(,Region.Op.DIFFERENCE);
    Paint paint=new Paint();
    paint.setColor(Color.RED);
    paint.setStrokeWidth(140);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(250,250,150,paint);
    imageView.setImageBitmap(bitmap);