如果您需要繪製(在畫布上)具有不同半徑的圓角,可以使用以下代碼:
private void drawAsymmetricRoundRect(Canvas canvas, RectF rectF, float[] radii, Paint paint) {
float topLeftX = rectF.left + radii[0];
float topLeftY = rectF.top + radii[0];
float topRightX = rectF.right - radii[1];
float topRightY = rectF.top + radii[1];
float bottomRightX = rectF.right - radii[2];
float bottomRightY = rectF.bottom - radii[2];
float bottomLeftY = rectF.bottom - radii[3];
float bottomLeftX = rectF.left + radii[3];
RectF topLeftCorner = new RectF(rectF.left, rectF.top, topLeftX + radii[0], topLeftY + radii[0]);
RectF topRightCorner = new RectF(topRightX - radii[1], rectF.top, rectF.right, topRightY + radii[1]);
RectF bottomRightCorner = new RectF(bottomRightX - radii[2], bottomRightY - radii[2], rectF.right, rectF.bottom);
RectF bottomLeftCorner = new RectF(rectF.left, bottomLeftY - radii[3], bottomLeftX + radii[3], rectF.bottom);
canvas.drawArc(topLeftCorner, 180, 90, true, paint);
canvas.drawArc(topRightCorner, 270, 90, true, paint);
canvas.drawArc(bottomRightCorner, 0, 90, true, paint);
canvas.drawArc(bottomLeftCorner, 90, 90, true, paint);
canvas.drawRect(topLeftX, rectF.top, topRightX, bottomLeftY < bottomRightY ? bottomLeftY : bottomRightY, paint); //top rect
canvas.drawRect(topLeftX > bottomLeftX ? topLeftX : bottomLeftX, topRightY, rectF.right, bottomRightY, paint); //right rect
canvas.drawRect(bottomLeftX, topLeftY > topRightY ? topLeftY : topRightY, bottomRightX, rectF.bottom, paint); //bottom rect
canvas.drawRect(rectF.left, topLeftY, bottomRightX < topRightX ? bottomRightX : topRightX, bottomLeftY, paint); //left rect
}
float[] radii
是浮點陣列(長度= 4),其存儲你的角部的半徑的尺寸(順時針,從左上角=>{topLeft, topRight, bottomRight, bottomLeft}
開始)。
基本上這種方法繪製4弧(角),並填充4角之間的所有角落。
重要說明:我在此方法中放置了角RectFs
的初始化以降低發佈代碼的複雜性。由於事實上你很可能會從你的onDraw()
方法中調用這個方法,所以你應該提取這部分代碼,並將它放在你初始化其他地方的地方(只要你還沒有在onDraw()
中初始化它們:P) 。
這可能有幫助 - http://developer.android.com/reference/android/graphics/drawable/shapes/RoundRectShape.html – Varun
@Varun我需要更多的信息,然後呢?當你想要某些特定的東西時,開發者文檔並不總是友好的。 – KickingLettuce