自定义View基础之——canvas,paint的基本用法

了解了View的绘制流程,并不是立刻就能自定义View了,我们还是需要工具的,那就是Canvas和Paint。不是很了解VIew的,请看上一篇博客,自定义View基础之——初识View

Canvas代表画布,Paint代表画笔,有了画笔和画布,通过Canvas提供的API,我们当然想画什么就画什么了,接下来我们就看看绘图的基本方法。而在绘图之前,我们通常需要初始化画笔,设置下画笔的各个属性:

//画笔宽度
mPaint.setStrokeWidth(5);
//设置画笔风格,描边,填充,填充且描边
mPaint.setStyle(Paint.Style.STROKE);
//mPaint.setStyle(Paint.Style.FILL);
//mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//抗锯齿,平滑
//mPaint.setAntiAlias(true);
//设置画笔颜色
mPaint.setColor(Color.BLACK);
//设置字体的尺寸
//mPaint.setTextSize(30);

画笔初始化好之后,我们当然可以随意绘制图形了:
1、绘制矩形,参数为左上角的横纵坐标,以及右下角的横纵坐标,即left,top,right,bottom
canvas.drawRect(0,0,150,100,mPaint);
2、绘制点,参数为点的横纵坐标,即x,y
mPaint.setStrokeWidth(20);
canvas.drawPoint(200,50,mPaint);
3、绘制线,参数为线起始点以及结束点横纵坐标,即startX,startY,stopX,stopY
mPaint.setStrokeWidth(5);
canvas.drawLine(250,100,350,0,mPaint);
4、绘制圆角矩形,参数为rectF,radiusX,radiusY。RectF与Rect并没有多大区别,同样都是绘制矩形,不过RectF的参数类型为float,而Rect的参数类型为int
RectF roundRectF=new RectF(400,0,550,100);
canvas.drawRoundRect(roundRectF,10,10,mPaint);
5、绘制圆形,参数为圆心的横纵坐标以及半径,即centerX,centerY,raidus
canvas.drawCircle(650,50,45,mPaint);

6、绘制多条直线,参数为pts,就是多个起始点结束点的坐标,每次每次都从pts中取4个值,代表startX,startY,stopX,stopY,等同于drawLine(pts[0], pts[1], pts[2], pts[3]),还有drawLine(pts[4], pts[5], pts[6], pts[7])依次下去。

mPaint.setColor(Color.CYAN);
float[] pts={0,250,100,150,200,250,300,150};
canvas.drawLines(pts,mPaint);


7、绘制弧形和扇形,参数为外界的矩形以及开始角度,扫过的角度,即rectF,startAngle,sweepAngle,userCenter,顺时针绘制,0度在三点钟位置,sweepAngle代表扫过的角度,不是终点的角度。
mPaint.setColor(getResources().getColor(android.R.color.holo_blue_light));
RectF arcRectF=new RectF(0,300,100,400);
canvas.drawArc(arcRectF,0,120,true,mPaint);
RectF arcRectF2=new RectF(150,300,250,400);
canvas.drawArc(arcRectF2,0,120,false,mPaint);
8、绘制椭圆以及它的外切矩形,参数就是外切矩形,即rectF
RectF ovalRectF=new RectF(300,300,450,400);
mPaint.setColor(Color.RED);
canvas.drawRect(ovalRectF,mPaint);
mPaint.setColor(getResources().getColor(android.R.color.holo_blue_light));
canvas.drawOval(ovalRectF,mPaint);


9、绘制文本,参数text,x,y。text为文字内容,x为文字绘制的起始横坐标,y为绘制的文字的baseline,并不是文字的左上角纵坐标
String text="Hello,World";
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLUE);
mPaint.setTextSize(30);
//mPaint.setTextAlign(Paint.Align.LEFT);
//mPaint.setFakeBoldText(true);
//mPaint.setUnderlineText(true);
//mPaint.setStrikeThruText(true);
canvas.drawText(text,0,600,mPaint);
10、绘制直线路径,参数为path,moveTo()为设置起始点,lineTo()为设置上条线的终点,也是下条线的起点,close()是将首尾相连
mPaint.setStyle(Paint.Style.STROKE);
Path path=new Path();
path.moveTo(200,600);
path.lineTo(250,500);
path.lineTo(300,600);
path.close();
canvas.drawPath(path,mPaint);
11、绘制其他路径,可以将各种图形加入路径之中,参数为图形和绘制的方向,方向分两种,CW代表顺时针,CCW代表逆时针
Path path=new Path();
RectF rect=new RectF(350,500,450,600);
path.addRect(rect,Path.Direction.CW);
//path.addArc();
//path.addRoundRect();
//path.addOval();
//path.addCircle();
canvas.drawPath(path,mPaint);


12、沿着路径绘制文字,参数为文字内容,路径,水平垂直偏移量,hOffset指与路径起始点的水平偏移,vOffset指与路径中心的垂直偏移
Path path=new Path();
path.addCircle(250,850,200,Path.Direction.CW);
String text="阿乐真帅阿乐真帅呀呀呀呀";
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,mPaint);
mPaint.setColor(getResources().getColor(android.R.color.holo_blue_light));
mPaint.setTextSize(50);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(text,path,0,0,mPaint);

Path path1=new Path();
path1.addCircle(400,1400,200, Path.Direction.CW);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path1,mPaint);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(text,path1,100,50,mPaint);



PS:还是这样的博客写着省心啊,10分钟结束战斗,哇咔咔。。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值