我想画一个圆形矩形,它的笔划是蓝色的,填充是红色的,但是在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);

最佳答案

一次只能使用一种颜色。

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

如果发现圆角矩形看起来不对,则可能会在视图边界处剪裁。调整矩形以允许一半的行程宽度:
mRectF.set(1, 1, mWidth - 1, mHeight - 1);

关于android - 如何设置笔触颜色以在 Canvas 上绘制矩形?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39469810/

10-10 06:18