在Android中,drawCircle()
方法本身不支持直接自定義邊框。但是,您可以通過以下步驟實現帶有自定義邊框的圓形:
ShapeDrawable
或GradientDrawable
。在這個類中,您可以設置圓形的屬性,如顏色、大小和邊框寬度及顏色。public class CustomCircleDrawable extends ShapeDrawable {
private int borderWidth;
private int borderColor;
public CustomCircleDrawable(int radius, int borderWidth, int borderColor) {
super(new RectF(-radius, -radius, radius, radius));
this.borderWidth = borderWidth;
this.borderColor = borderColor;
}
@Override
protected void onDraw(Canvas canvas, Rect bounds) {
Paint paint = getPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderWidth);
paint.setColor(borderColor);
super.onDraw(canvas, bounds);
}
}
CustomCircleDrawable customCircleDrawable = new CustomCircleDrawable(radius, borderWidth, borderColor);
Canvas canvas = new Canvas();
canvas.drawCircle(width / 2f, height / 2f, radius, customCircleDrawable);
這樣,您就可以創建一個帶有自定義邊框的圓形了。