写完Android 画笔Paint,自然少不了Canvas画布,本文延续上篇风格,一一来过Canvas API
drawARGB
1voiddrawARGB(inta,intr,intg,intb)
统一颜色绘制,四个参数取值范围0~255
drawArc
12345voiddrawArc(RectFoval,floatstartAngle,floatsweepAngle,booleanuseCenter,Paintpaint)
绘制弧面或弧线。对于绘制对应图形的填充面,还是图形的轮廓线,这在于画笔Paint中的setStyle。
oval:矩形;
startAngle:起点角度,0度的角度对应于0度的几何角(在手表3点钟);
sweepAngle:顺时针扫过的角度;
useCenter:弧面或弧线;
paint:画笔
弧面12RectFrectF=newRectF(100f,100f,500f,500f);canvas.drawArc(rectF,0,150,true,mPaint);
弧线12RectFrectF=newRectF(100f,100f,500f,500f);canvas.drawArc(rectF,0,150,false,mPaint);
drawBitmap
绘制使用指定的矩阵的位图。123456789101112131415//方法1voiddrawBitmap(Bitmapbitmap,floatleft,floattop,Paintpaint)//方法2voiddrawBitmap(Bitmapbitmap,Rectsrc,Rectdst,Paintpaint)//方法3voiddrawBitmap(Bitmapbitmap,Matrixmatrix,Paintpaint)
例子代码1234567891011121314mBitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.logo);canvas.drawBitmap(mBitmap,0,0,mPaint);//绘制Bitmap的一部分,并对其拉伸//srcRect绘制Bitmap的哪一部分Rectsrc=newRect(0,0,mBitmap.getWidth(),mBitmap.getHeight()/3);//dstRecF绘制的Bitmap拉伸到哪里RectFdst=newRectF(0,mBitmap.getHeight(),canvas.getWidth(),mBitmap.getHeight()+200);canvas.drawBitmap(mBitmap,src,dst,mPaint);Matrixmatrix=newMatrix();matrix.postTranslate(0,mBitmap.getHeight()+200);canvas.drawBitmap(mBitmap,matrix,mPaint);
drawBitmapMesh
12345678voiddrawBitmapMesh(Bitmapbitmap,intmeshWidth,intmeshHeight,float[]verts,intvertOffset,int[]colors,intcolorOffset,Paintpaint)
绘制网格顶点被均匀地分布在该位图
bitmap:位图
meshWidth:横向上把该源位图划成成多少格
meshHeight:竖向上把该源位图划成成多少格
verts:长度为(meshWidth + 1)(meshHeight + 1)2的数组,它记录了扭曲后的位图各顶点位置
vertOffset:控制verts数组中从第几个数组元素开始才对bitmap进行扭曲
colors:可以为null,指定在每个顶点,其值由对应的位图颜色相乘内插的颜色。如果不为空,必须有至少(meshWidth+ 1)*(meshHeight+ 1)+ colorOffset数组中的值。
colorOffset int: Number of color elements to skip before drawing
paint:画笔,可以为null
drawCircle
1234voiddrawCircle(floatcx,floatcy,floatradius,Paintpaint)
绘制圆
cx,cy代表圆心的坐标
radius圆的半径
paint画笔1canvas.drawCircle(500f,500f,200,mPaint);
drawColor
1voiddrawColor(intcolor)
设置画布颜色
drawLine
1234567891011voiddrawLine(floatstartX,floatstartY,floatstopX,floatstopY,Paintpaint)voiddrawLines(float[]pts,Paintpaint)voiddrawLines(float[]pts,intoffset,intcount,Paintpaint)
画线
方法1
startX,startY起点坐标
stopX,stopY终点坐标
paint画笔
方法2
pts:绘制直线的端点数组,每条直线占用4个数据,即起终点坐标。
paint:绘制直线所使用的画笔。
方法2
pts:绘制直线的端点数组,每条直线占用4个数据,即起终点坐标。
offset:跳过的数据个数,取值为4的倍数。
count:实际参与绘制的数据个数。
paint:绘制直线所使用的画笔。
1canvas.drawLine(100f,100f,500f,500f,mPaint);
12345float[]pts={100f,100f,400f,400f,400f,400f,250f,560f,250f,560f,400f,800f,400f,800f,280f,880f};canvas.drawLines(pts,mPaint);
123456float[]pts={100f,100f,400f,400f,400f,400f,250f,560f,250f,560f,400f,800f,400f,800f,280f,880f};//有选择地绘制直线canvas.drawLines(pts,4,8,mPaint);
跳过前4个数据,绘制后面8的数据,即绘制了第二第三个点:
drawOval
1voiddrawOval(RectFoval,Paintpaint)
绘制椭圆1234RectFrectF=newRectF(100f,100f,600f,500f);//等同于圆的效果//RectFrectF=newRectF(100f,100f,500f,500f);canvas.drawOval(rectF,mPaint);
drawPath
1voiddrawPath(Pathpath,Paintpaint)
例子12345Pathpath=newPath();//向Path中加入ArcRectFarcRecF=newRectF(0,0,500,500);path.addArc(arcRecF,0,135);canvas.drawPath(path,mPaint);
更多Path(宝藏,可继续挖掘),详见博客android绘图之Path总结
drawPoint
123voiddrawPoint(floatx,floaty,Paintpaint)
绘制点,x,y分别是点坐标123456voiddrawPoints(float[]pts,intoffset,intcount,Paintpaint)voiddrawPoints(float[]pts,Paintpaint)
drawPoints与drawLines类似
drawRGB
123voiddrawRGB(intr,intg,intb)
RGB绘制画布颜色,取值也是0~255
drawRect
12345voiddrawRect(floatleft,floattop,floatright,floatbottom,Paintpaint)
绘制矩形,前四个参数分别表示矩形的左,顶,右,低
12voiddrawRect(Rectr,Paintpaint)voiddrawRect(RectFrect,Paintpaint)
Rect 和 RectF区别是Rect初始化 是int,RectF 是left
1canvas.drawRect(100f,100f,500f,500f,mPaint);
drawRoundRect
1234voiddrawRoundRect(RectFrect,floatrx,floatry,Paintpaint)
绘制圆矩形
rx:圆角x方向的半径
ry:圆角y方向的半径
12RectFrectF=newRectF(100f,100f,500f,500f);canvas.drawRoundRect(rectF,50,150,mPaint);
drawText
12345678910111213141516voiddrawText(CharSequencetext,intstart,intend,floatx,floaty,Paintpaint)voiddrawText(Stringtext,floatx,floaty,Paintpaint)voiddrawText(Stringtext,intstart,intend,floatx,floaty,Paintpaint)
绘制文本
start:指的文本从哪个开始;end:文本结束的位置;x,y:文本起点1234mPaint.setTextSize(50);Stringtext="我的微信公众号:吴小龙同学";canvas.drawText(text,2,text.length(),100,100,mPaint)canvas.drawText("我的微信公众号:吴小龙同学",100,400,mPaint);
drawTextOnPath
123456789101112voiddrawTextOnPath(Stringtext,Pathpath,floathOffset,floatvOffset,Paintpaint)voiddrawTextOnPath(char[]text,intindex,intcount,Pathpath,floathOffset,floatvOffset,Paintpaint)
沿着Path绘制一段文字
hOffset : 与路径起始点的水平偏移距离
vOffset : 与路径中心的垂直偏移量
12345678Pathpath=newPath();//Path.Direction.CW,沿外环;W,沿内环path.addCircle(500,500,200,Path.Direction.CW);mPaint.setTextSize(50);//绘制路径canvas.drawPath(path,mPaint);Stringtext="我的微信公众号:吴小龙同学";canvas.drawTextOnPath(text,path,0f,0f,mPaint);
drawVertices
123456789101112voiddrawVertices(Canvas.VertexModemode,intvertexCount,float[]verts,intvertOffset,float[]texs,inttexOffset,int[]colors,intcolorOffset,short[]indices,intindexOffset,intindexCount,Paintpaint)
绘制顶点数组