绘制圆角矩形的方法
/**
* 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.


这篇博客探讨了Android中实现圆角矩形的方法,详细解析了如何使用`RoundRect`来绘制圆角,解释了如何通过设置椭圆的x和y轴半径来形成矩形的圆角。内容包括矩形的四个圆角如何独立生成,并以左上角的圆角为例进行说明,阐述了椭圆中心点的定位和半径的作用,帮助理解`RoundRect`的绘制过程。
1万+

被折叠的 条评论
为什么被折叠?



