如何设置笔触颜色以在画布上绘制矩形?

时间:2016-09-13 12:13:04

标签: android android-canvas

我想绘制一个圆形矩形,其笔触为蓝色,填充为红色,但我无法在Paint类中找到设置笔触颜色的方法。我怎么能这样做?

    mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR);
    mCanvas.setDrawFilter(mPaintFlagsDrawFilter);

    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(Color.RED);
    mPaint.setStrokeWidth(2);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mRectF.set(0, 0, mWidth, mHeight);
    mCanvas.drawRoundRect(mRectF, 10, 10, mPaint);

1 个答案:

答案 0 :(得分:15)

Paint一次只允许一种颜色。

mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR);
mCanvas.setDrawFilter(mPaintFlagsDrawFilter);

mFillPaint.setStyle(Paint.Style.FILL);
mFillPaint.setColor(Color.RED);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(Color.BLUE);
mStrokePaint.setStrokeWidth(2);
mStrokePaint.setStrokeCap(Paint.Cap.ROUND);
mRectF.set(0, 0, mWidth, mHeight);
mCanvas.drawRoundRect(mRectF, 10, 10, mFillPaint);
mCanvas.drawRoundRect(mRectF, 10, 10, mStrokePaint);

如果您发现圆角矩形看起来不正确,则可能会在视图的边界处剪切。调整RectF以允许StrokeWidth的一半:

mRectF.set(1, 1, mWidth - 1, mHeight - 1);