1. 程式人生 > >canvas.drawRoundRect方法,繪製圓角矩形

canvas.drawRoundRect方法,繪製圓角矩形

public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
Draw the specified round-rect using the specified paint. The roundrect will be filled or framed based on the Style in the paint.


Parameters
rectThe rectangular bounds of the roundRect to be drawn
rxThe x-radius of the oval used to round the corners
ryThe y-radius of the oval used to round the corners
paintThe paint used to draw the roundRect


【功能說明】該方法用於在畫布上繪製圓角矩形,通過指定RectF物件以及圓角半徑來實現。該方法是繪製圓角矩形的主要方法,同時也可以通過設定畫筆的空心效果來繪製空心的圓角矩形。
【基本語法】public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
引數說明
rect:RectF物件。
rx:x方向上的圓角半徑。
ry:y方向上的圓角半徑。
paint:繪製時所使用的畫筆。


protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//新建一隻畫筆,並設定為綠色屬性
Paint _paint = new Paint();
_paint.setColor(Color.GREEN);
//新建矩形r1
RectF r1 = new RectF();
r1.left = 50;
r1.right = 250;
r1.top = 50 ;
r1.bottom = 150;

//新建矩形r2
RectF r2 = new RectF();
r2.left = 50;
r2.right = 250;
r2.top = 200 ;
r2.bottom = 300;

//畫出矩形r1
canvas.drawRect(r1, _paint);
//畫出圓角矩形r2
_paint.setColor(Color.rgb(204, 204, 204));
canvas.drawRoundRect(r2, 10, 10, _paint);


}