Android 圓角邊框RoundRect原理
阿新 • • 發佈:2019-01-09
繪製圓角矩形的方法
/**
* 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.