版权声明:本文为博主原创文章,转载请注明出处(http://blog.csdn.net/jaden_hool) https://blog.csdn.net/Jaden_hool/article/details/80757072
绘制圆角矩形的方法
/**
* Draw the specified round-rect using the specified paint. The roundrect will be filled or
* framed based on the Style in the paint.
*
* @param rect The rectangular bounds of the roundRect to be drawn
* @param rx The x-radius of the oval used to round the corners
* @param ry The y-radius of the oval used to round the corners
* @param paint The paint used to draw the roundRect
*/
public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) {
super.drawRoundRect(rect, rx, ry, paint);
}
该方法来自Canvas
类,rect
代表矩形,rx
和ry
分别代表形成圆角所需要的椭圆的x和y轴半径,那么rx
和ry
究竟如何形成圆角呢?
形成圆角的原理
矩形的四个圆角是分别生成的,以左上角的圆角为例:
首先通过rect
绘制出矩形,然后以矩形的左上角定点为起点,分别向x和y轴平移rx
和ry
, 得到的点为中心,以rx
和ry
为x和y轴的半径绘制椭圆,椭圆的坐上半部分圆弧就是圆角了。如下图所示,图中第一个图形是根据圆角的原理用矩形和椭圆绘制的,第二个图形就是roundRect.