如何设置描边颜色在画布上绘制矩形?

问题描述 投票:0回答:2

我想画一个圆角矩形,其描边为蓝色,填充为红色,但我在 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);
android android-canvas
2个回答
27
投票

绘画一次只能使用一种颜色。

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

-1
投票

有人知道 FILL_AND_STROKE 有什么用,而我们知道无法为每种颜色指定颜色?

© www.soinside.com 2019 - 2024. All rights reserved.