如何使用paint赋予圆圈边框

时间:2016-01-23 06:39:30

标签: android paint android-progressbar stroke

嗨我已经实现了进度条,它工作正常,但我的问题是我需要使用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)

2 个答案:

答案 0 :(得分:9)

试试这个,

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

并参考此信息,可能对您有所帮助。Android : canvas.drawBitmap() method not working properly

答案 1 :(得分:1)

这是我的解决方案代码。只需复制这个类,并尝试了解你做错了什么。此视图将在视图中心绘制进度条。

    /**
     * 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
        );
    }
}