1 Preface
Recently when the project encountered a card coupon effect, because I think with the picture to do can appear the adaptation effect is not good, coupled with their own custom view knowledge is weak, so want to try to use custom view to achieve. But because of their weak knowledge, at the beginning of the thought of using a rectangle to set the edge of the implementation, a brother behind me, thank him here.
2 Implementation Analysis
The picture above is actually the same as the normal linearlayout,relativelayout, just up and down on both sides of the shape similar to semi-circular jagged. Then you just have to deal with different places. This effect can be achieved by drawing a small white circle on top and bottom two lines.
If the distance between the upper and lower halves and the semicircle and semicircle is fixed, then the different size of the screen will certainly draw a different number of semicircular, then we only need to depend on the width of the control to get the number of semi-circular.
We observe the picture, it is easy to find that the number of circles is always the number of circular spacing-1, that is, if the number of circles is circlenum, then the circle spacing is circlenum+1.
So we can calculate the circlenum based on this.
Circlenum = (int) ((W-GAP)/(2*RADIUS+GAP));
Here gap is circular spacing, radius is the radius of the circle, W is the width of the view.
3 Specific implementations
Here's a look at the code:
The circle's radius and circle spacing are defined above, and the values are initialized and the number of circles to be drawn is obtained.
Next, you just need one to draw the circle out.
A circular drawing is simply based on the number of circlenum.
REMAIN/2 here is because, in some cases, the calculated number of can be drawn is not exactly divisible. This will cause the last space on the right to be wider than the rest.
So we add half of the remaining spacing when we draw the first one, even if it's not divisible. At a minimum, the first and last spacing widths are guaranteed to be the same.
This is achieved by looking at the layout file:
:
Very clever and simple way to achieve the edge of the special image of the drawing, but here is specified a specific color (white), if you want to directly like the eraser as transparent, you can use Xfermode to do the processing.
Learn Android Custom controls-edge bump view