当前位置 : 主页 > 手机开发 > android >

android – 如何使边框更流畅

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想为视图添加边框,边框宽度,颜色,半径可以由用户设置.所以我试着为它绘制一个矩形.当我使用drawRoundRect绘制时,角落处的线条不平滑,它也比其他地方更粗.我不知道如何解决它.请给我
我想为视图添加边框,边框宽度,颜色,半径可以由用户设置.所以我试着为它绘制一个矩形.当我使用drawRoundRect绘制时,角落处的线条不平滑,它也比其他地方更粗.我不知道如何解决它.请给我一些指导.还有其他办法吗?我必须使用代码来绘制它.

非常感谢.
附加代码:rect的红色角落.
过去的代码:

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

    }
问题的核心是填充,而不是AntiAliasing.角落不厚,而是正常宽度.但是,直线被剪裁了.

如果将笔触宽度设置为2,则实际直线宽度为1,因为笔划为矩形,轴为x = 0.这意味着矩形为(left = 0,up = -1,right = length, bottom = 1),但是up -1在画布之外,因此它不会被绘制.角落是全宽的,因为它在画布上.

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

下面是在画布中完全圆化rect绘制的代码:

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);
网友评论