《Android自定义控件入门到精通》文章索引 ☞ https://blog.csdn.net/Jhone_csdn/article/details/118146683
《Android自定义控件入门到精通》所有源码 ☞ https://gitee.com/zengjiangwen/Code
几何图形绘制
矩形
drawRect(float left, float top, float right, float bottom, Paint paint)
drawRect(Rect r, Paint paint)
drawRect(RectF rect, Paint paint)
矩形由左上右下四个值决定,这三个方法其实都差不多,没啥大的区别,我们就挑一个示范下
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.parseColor("#ff0000"));
mPaint.setStrokeWidth(2);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(20,20,90,80,mPaint);
}
圆角矩形
drawRoundRect(RectF rect, float rx, float ry,Paint paint)
跟画矩形对比,就多了rx,ry两个参数。
rx :x轴方向的radius(圆半径)
ry :y轴方向的radius(圆半径)
@Override
protected void onDraw(Canvas canvas) {
RectF rect=new RectF(20,20,120,200);
canvas.drawRoundRect(rect,20