简体   繁体   English

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

[英]How to set stroke color to draw a rectangle on canvas?

I want to draw a round rectangle which its stroke is blue and its fill is red, but I can't find a method in Paint class to set stroke color. 我想绘制一个圆形矩形,其笔划为蓝色,填充为红色,但我在Paint类中找不到设置笔触颜色的方法。 How can I do that? 我怎样才能做到这一点?

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

Paint only allows for one color at a time. 油漆一次只允许一种颜色。

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

If you find your rounded rectangle doesn't look right, it may be clipped at the bounds of the view. 如果您发现圆角矩形看起来不正确,则可能会在视图的边界处剪切。 Adjust the RectF to allow for half the StrokeWidth: 调整RectF以允许StrokeWidth的一半:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用笔划设置矩形边框的不同颜色 - how to set different color of border of rectangle using stroke 如何绘制矩形画布? - how to draw rectangle canvas? 如何在Android画布上的自定义形状上绘制笔触 - How to draw stroke on custom shape on android canvas 如何使用 canvas 在 Bitmap 中绘制笔画? - How to draw a stroke in a Bitmap using canvas? 如何在android中绘制复杂的形状,包含三角形,矩形和笔划 - How to draw complicated shape in android, with contain of triangle, rectangle and stroke 如何绘制具有可变笔触宽度的Android Shape Rectangle - How to draw a Android Shape Rectangle with variable stroke width 试图绘制一个按钮:如何设置笔触颜色以及如何在不知道高度的情况下将渐变“对齐”到底部? - Trying to draw a button: how to set a stroke color and how to “align” a gradient to the bottom without knowing the height? 如何用画布逐步绘制圆角矩形 - How to draw a rounded corner rectangle progressively with canvas 如何在Android中的Canvas上绘制具有透明边的矩形 - How to draw rectangle with transparent sides on Canvas in Android 如何通过使用画布在按钮上绘制矩形 - How to draw rectangle on Buttons by using canvas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM