|
public class LaceEdgeLayout extends LinearLayout { |
|
private Paint mPaint; |
|
// Paint's color. |
|
private int color = Color.WHITE; |
|
// The distance between a half circle and a half circle. |
|
private float gap = 8; |
|
// Circle's radius. |
|
private float radius = 10; |
|
// The total number of circles on edge. |
|
private int circleNum; |
|
private float circleRemain; |
|
|
|
// Normal custom attribute setting. Three constructors. |
|
public LaceEdgeLayout(Context context) { |
|
this(context, null, 0); |
|
} |
|
|
|
public LaceEdgeLayout(Context context, AttributeSet attrs) { |
|
this(context, attrs, 0); |
|
} |
|
|
|
public LaceEdgeLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
|
super(context, attrs, defStyleAttr); |
|
|
|
// Search the attrs from res/values/attrs.xml |
|
if (null != attrs) { |
|
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LaceEdgeLayout); |
|
this.color = typedArray.getInt(R.styleable.LaceEdgeLayout_circleColor, Color.WHITE); |
|
this.gap = typedArray.getFloat(R.styleable.LaceEdgeLayout_gap, 0); |
|
this.radius = typedArray.getFloat(R.styleable.LaceEdgeLayout_radius, 0); |
|
typedArray.recycle(); |
|
} |
|
|
|
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
|
mPaint.setDither(true); |
|
mPaint.setColor(this.color); |
|
mPaint.setStyle(Paint.Style.FILL); |
|
} |
|
|
|
@Override |
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
|
super.onSizeChanged(w, h, oldw, oldh); |
|
|
|
// Counting how many circle we need on the edge. |
|
if (circleRemain == 0) { |
|
circleRemain = (int) (w - gap) % (2 * radius + gap); |
|
} |
|
circleNum = (int) ((w - gap) / (2 * radius + gap)); |
|
} |
|
|
|
@Override |
|
protected void onDraw(Canvas canvas) { |
|
super.onDraw(canvas); |
|
for (int i = 0 ; i < circleNum ; i++) { |
|
float x = gap + radius + circleRemain / 2 + ((gap + radius * 2) * i); |
|
canvas.drawCircle(x, 0, radius, mPaint); |
|
canvas.drawCircle(x, getHeight(), radius, mPaint); |
|
} |
|
} |
|
} |