該解決方案使用Canvas.drawLine()
和Canvas.draw Arc()
。我介紹了偏移量並根據測量的高度/寬度設置了「mSize」,以確保在所有邊緣上都能看到所需的筆觸寬度。修改'mCornerRadius'當然會改變角落。
我用紅色油漆與圓角半徑8得到這個:
@Override
protected void onDraw(Canvas canvas)
{
// to make sure that the whole shape fits into the View
// (else some lines may be thinner/ missing)
mSize = Math.min(getMeasuredHeight(), getMeasuredWidth())*0.8f;
float offset_top = 4;
float offset_left = 4;
float sizeMinusRadius = mSize - mCornerRadius;
// horizontal lines
canvas.drawLine(offset_left + mCornerRadius, offset_top, offset_left + sizeMinusRadius, offset_top, mPaintShape);
canvas.drawLine(offset_left + mCornerRadius, offset_top + mSize, offset_left + sizeMinusRadius, offset_top + mSize, mPaintShape);
// vertical lines
canvas.drawLine(offset_left, offset_top + mCornerRadius, offset_left, offset_top + sizeMinusRadius, mPaintShape);
canvas.drawLine(offset_left + mSize, offset_top + mCornerRadius, offset_left + mSize, offset_top + sizeMinusRadius, mPaintShape);
// corner bottom left
RectF rBottomLeft = new RectF(offset_left,
offset_top + mSize - 2 * mCornerRadius, offset_left + 2 * mCornerRadius,
offset_top + mSize);
canvas.drawArc(rBottomLeft, 90, 90, false, mPaintShape);
// corner bottom right
RectF rBottomRight = new RectF(offset_left + mSize - 2 * mCornerRadius,
offset_top + mSize - 2 * mCornerRadius, offset_left + mSize, offset_top + mSize);
canvas.drawArc(rBottomRight, 0, 90, false, mPaintShape);
// corner top left
RectF rTopLeft = new RectF(offset_left, offset_top,
offset_left + 2 * mCornerRadius, offset_top + 2 * mCornerRadius);
canvas.drawArc(rTopLeft, 180, 90, false, mPaintShape);
// corner top right
RectF rTopRight = new RectF(offset_left + mSize - 2 * mCornerRadius, offset_top,
offset_left + mSize, offset_top + 2 * mCornerRadius);
canvas.drawArc(rTopRight, 270, 90, false, mPaintShape);
}
謝謝,但我需要做到這一點的方法的onDraw –
我已經加入這個解決方案也 – sud
我問題是我想要得到一個帶有圓角外角的正方形的線條。你想邀請我使用兩個完整填充的方格嗎?所以外部有圓角,裏面沒有? –