- rect和rectF
构造方法传入左顶点和右下角的坐标值
先来看一段代码:
@Override
protected void onDraw(Canvas canvas) {
RectF rect = new RectF(10, 10, 100, 100);
Rect rect2 = new Rect(10, 200, 100, 300);
canvas.drawRect(rect, mPaint);
canvas.drawRect(rect2, mPaint);
}
效果是这样的,两者看起来貌似没有什么区别
image.png再看一段代码:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
/**
* 这是一个居中的圆
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
mOval = new RectF( x, y,
getWidth() - x, getHeight() - y);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(mOval,mPaint);
canvas.drawArc(mOval,-90,90,true,mPaint2);
}
当我们设置为true时,就经过圆心
canvas.drawArc(mOval,-90,90,true,mPaint2);
画出来的效果:
当我们设置为false时,不经过圆心
canvas.drawArc(mOval,-90,90,false,mPaint2);
网友评论