3

我想为视图添加边框,边框宽度、颜色、半径可以由用户设置。所以我试着为它画一个矩形。我用drawRoundRect画的时候,拐角处的线不平滑,也比其他地方粗。我不知道如何解决它。请给我一些指导。还有其他方法吗?我必须使用代码来绘制它。

非常感谢。附代码:矩形的红色角。过去的代码:

public class MPCTextView extends TextView {
    // private Context context;
    private final static String TAG = "MPCTextView";
    public final static int DEFAULT_BACKGROUND_COLOR = Color
            .parseColor("#28FF28");
    public final static int DEFAULT_BORDER_COLOR = Color.parseColor("#FF0000");

    public int mBoderWidth = 2;
    public int mBoderColor;
    public int mBoderRadius = 20;
    public int mbackgroundColor;
    public boolean isHaveBorder = true;
    public boolean isHaveBackground = true;
    RectF mRectF = new RectF();
    Rect mRec = new Rect();
    Paint mPaint = new Paint();

    public MPCTextView(Context context) {
        super(context);
        // this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // try to add a boder for this view.
        canvas.getClipBounds(mRec);

        // draw background
        // canvas.drawColor(mbackgroundColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(DEFAULT_BACKGROUND_COLOR);
        if (mBoderRadius > 0) {
            mRectF.set(mRec);
            canvas.drawRoundRect(mRectF, mBoderRadius, mBoderRadius, mPaint);

        } else {
            canvas.drawRect(mRec, mPaint);
        }

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBoderWidth);
        mPaint.setColor(DEFAULT_BORDER_COLOR);
                mPaint.setAntiAlias(true);

        if (mBoderRadius > 0) {
            mRectF.set(mRec);
            canvas.drawRoundRect(mRectF, mBoderRadius, mBoderRadius, mPaint);

        } else {
            canvas.drawRect(mRec, mPaint);
        }

        super.onDraw(canvas);

    }

在此处输入图像描述

4

4 回答 4

8

问题的核心是填充,而不是抗锯齿。拐角不是更厚,而是正常宽度。但是,直线被剪裁了。

如果将笔画宽度设置为 2,则真正的直线宽度为 1,因为笔画是矩形,轴是线 x = 0。这意味着矩形是 (left=0,up=-1,right=length, bottom=1),但是 up -1 在画布之外,所以不会被绘制。角落是全宽的,因为它在画布中。

所以,你只需要设置填充。

这是使圆角矩形完全在画布内绘制的代码:

float pad = 1f;
mRectF.set(new RectF(mRec.left + pad, mRec.top + pad, mRec.right - pad, mRec.bottom - pad));
canvas.drawRoundRect(mRectF, mBoderRadius, mBoderRadius, mPaint);
于 2015-11-12T08:58:07.477 回答
1

将抗锯齿设置为您用于绘制红色矩形的油漆。例如

mPaint.setAntiAlias(true);
于 2013-07-19T15:51:08.370 回答
0

我希望此代码对您有所帮助。

public int mBoderWidth = 2;
public int mBoderColor;
public int mBoderRadius = 20;
public int mbackgroundColor;
public boolean isHaveBorder = true;
public boolean isHaveBackground = true;
RectF mRectF = new RectF();
Rect mRec = new Rect();
Paint mPaint = new Paint();

public MPCTextView(Context context) {
    super(context);
    // this.context = context;
}

@Override
protected void onDraw(Canvas canvas) {

    // try to add a boder for this view.
    canvas.getClipBounds(mRec);

    // draw background
    // canvas.drawColor(mbackgroundColor);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(DEFAULT_BACKGROUND_COLOR);
    if (mBoderRadius > 0) {
        mRectF.set(mRec);
        canvas.drawRoundRect(mRectF, mBoderRadius, mBoderRadius, mPaint);

    } else {
        canvas.drawRect(mRec, mPaint);
    }

    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(mBoderWidth);
    mPaint.setColor(DEFAULT_BORDER_COLOR);
    mPaint.setAntiAlias(true);

    if (mBoderRadius > 0) {
        mRectF.set(mRec);

        ///////////// look at this code
        mRectF.left += mBorderWidth/2;
        mRectF.right -= mBorderWidth/2;
        mRectF.top += mBorderWidth/2;
        mRectF.bottom -= mBorderWidth/2;

        canvas.drawRoundRect(mRectF, mBoderRadius, mBoderRadius, mPaint);

        mRectF.left -= mBorderWidth/2;
        mRectF.right += mBorderWidth/2;
        mRectF.top -= mBorderWidth/2;
        mRectF.bottom += mBorderWidth/2;
    } else {
        canvas.drawRect(mRec, mPaint);
    }

    super.onDraw(canvas);
}
于 2014-01-15T12:22:03.773 回答
0

我知道我很晚才回答这个问题,但它可能会帮助其他人修改您的代码,如下所示

Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
于 2018-09-21T10:50:33.170 回答