如何使用油漆给圆圈加上边框

How to give border to circle using paint

您好,我已经实现了进度条并且工作正常,但我的问题是我需要使用油漆为圆圈提供唯一的边框。我致力于此,但它占据了圆圈的所有区域,我只需要边框。

我的绘画代码:

    mCirclePaint = new Paint();
    mCirclePaint.setAntiAlias(true);
    mCirclePaint.setDither(true);
    mCirclePaint.setColor(mCircleColor);
    mCirclePaint.setStrokeWidth(mCircleStrokeWidth);
    mCirclePaint.setStyle(Paint.Style.STROKE);
    mCirclePaint.setStrokeJoin(Paint.Join.MITER);
    // mCirclePaint.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
    mCirclePaint.setStrokeCap(Paint.Cap.SQUARE);
    canvas.drawPath(mCirclePath, mCirclePaint)

试试这个,

paint = new Paint();
paint.setColor(Color.GREEN);        
paint.setStrokeWidth(2);            
paint.setStyle(Paint.Style.STROKE);         
canvas.drawCircle(0, 0, (float) (width1/(1.4)), paint); 

并参考这个,可能对你有帮助。

这是我为您的解决方案编写的代码。只需复制此 class 并尝试了解您做错了什么。此视图将在视图中心绘制进度条。

    /**
     * Created by GIGAMOLE on 23.01.2016.
     */
    public class StrokeProgressBar extends View {

    private final static float BAR_STROKE = 10.0f;
    private final static float BAR_HEIGHT = 60.0f;
    private final static float BAR_PADDING = 100.0f;

    private final Paint mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
        {
            setDither(true);
            setAntiAlias(true);
            setColor(Color.BLUE);
            setStyle(Style.FILL);
        }
    };

//    private final Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
//        {
//            setDither(true);
//            setAntiAlias(true);
//            setColor(Color.GRAY);
//            setStyle(Style.FILL);
//        }
//    };

    private final Paint mBgStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
        {
            setDither(true);
            setAntiAlias(true);
            setColor(Color.BLUE);
            setStyle(Style.STROKE);
            setStrokeWidth(BAR_STROKE);
            setStrokeCap(Cap.SQUARE);
        }
    };

    public StrokeProgressBar(final Context context) {
        this(context, null);
    }

    public StrokeProgressBar(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StrokeProgressBar(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // Draw always
        setWillNotDraw(false);
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        final float height = canvas.getClipBounds().height();
        final float width = canvas.getClipBounds().width();

        // Background rect
        final Rect bgRect = new Rect(
                (int) BAR_PADDING,
                (int) (height / 2.0f - BAR_HEIGHT / 2.0f),
                (int) (width - BAR_PADDING),
                (int) (height / 2.0f + BAR_HEIGHT / 2.0f)
        );
        // Progress bar rect
        final Rect progressRect = new Rect(
                (int) BAR_PADDING,
                (int) (height / 2.0f - BAR_HEIGHT / 2.0f),
                (int) ((width - BAR_PADDING) * 0.7f), // 0.7f is the fraction of progress == 70%
                (int) (height / 2.0f + BAR_HEIGHT / 2.0f)
        );

        // At first draw stroke
        canvas.drawRect(
                bgRect,
                mBgStrokePaint
        );
//        // At second draw bg
//        canvas.drawRect(
//                bgRect,
//                mBgPaint
//        );
        // At third draw progress
        canvas.drawRect(
                progressRect,
                mProgressPaint
        );
    }
}