如何使边框更加流畅

时间:2013-07-19 15:49:17

标签: android draw rect

我想为视图添加边框,边框宽度,颜色,半径可以由用户设置。所以我试着为它绘制一个矩形。当我使用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);

    }

enter image description here

4 个答案:

答案 0 :(得分:6)

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

如果将笔触宽度设置为2,则实线直线宽度为1,因为笔划为矩形,轴为x = 0.这意味着矩形为(左= 0,向上= -1,向右= 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);

答案 1 :(得分:1)

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

mPaint.setAntiAlias(true);

答案 2 :(得分: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);
}

答案 3 :(得分:0)

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

for i in {1..60}; do
    bwa mem -t 8  repeatfile.fas Pk_sd_ "s${i}_R1_001.gz"  Pk_sd_ "s${i}_R2_001.gz" | samtools view -Su | samtools sort -o mapped_to_repeat.bam
done