Draw a hollow circle on an Android Canvas

百般思念 提交于 2020-01-02 21:58:23

问题


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


来源:https://stackoverflow.com/questions/21387282/draw-a-hollow-circle-on-an-android-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!