概要:
本人在目前项目中很多页面都需要自定义控件,所以抽出时间对自定义控件进行系统的复习,并对此发表一下本人的拙见,也方便今后复习。
学习之前先看一下,这次分享最终要做的自定义控件吧,整张图除了中间的文字是TextView 其他的都是画出来的,而且圆指向柱状体顶部的箭头是
通过计算出来位置后画出来的。这个Demo我会在最后一篇文章中进行分享
好其他不多说开始这次的学习之旅吧!!!
它有4个基本的组件,包括:
一个Bitmap的画布,
一个Canvas类会调用这个Bitmap(把图画在这个Bitmap身上),
一个填充的域 Path、Rect、ovil等(比如矩形,路径,文字,图片等)
一个画笔 Paint(用来定义画出来的控件的颜色,样式等等)
(一)获取Canvas第一种在所有View的onDraw(Canvas canvas)方法中直接获取
@Override protected void onDraw(Canvas canvas) { <span> </span>super.onDraw(canvas); }第二种直接new一个Canvas
//先得到一个Bitmap对象,通过Canvas的构造函数创建一个Canvas类 Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); //或者先创建一个Canvas类,通过setBitmap()方法创建一个画布 Canvas canvas = new Canvas(); canvas.setBitmap(bitmap);第三种调用SurfaceHolder.lockCanvas(),返回一个Canvas对象
(二)Canvas的API自定义控件完全是自己对API的熟练程度,所以大家对Canvas类的方法一定要熟悉,以便在运用的时候能够灵活的掌握
//获取画布宽高
canvas.getHeight();
canvas.getWidth();
//获取Canvas画布像素密度
canvas.getDensity();
//获取画布过滤器
canvas.getDrawFilter();
//clip相关的方法主要是对画布Canvas进行裁剪 canvas.clipPath(path_STROKE); canvas.clipPath(path_STROKE, op); canvas.clipRect(rect); canvas.clipRect(rect, op); canvas.clipRect(left, top, right, bottom); canvas.clipRect(left, top, right, bottom, op); canvas.clipRegion(region); canvas.clipRegion(region, op);
//给Canvas画布填充颜色 canvas.drawARGB(a, r, g, b); canvas.drawColor(color); canvas.drawColor(color, mode);
//通过不同参数在Canvas上画位图 canvas.drawBitmap(bitmap, left, top, paint_STROKE); canvas.drawBitmap(bitmap, matrix, paint_STROKE); canvas.drawBitmap(bitmap, src, dst, paint_STROKE); canvas.drawBitmap(bitmap, src, dst, paint_STROKE); canvas.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint_STROKE); canvas.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, paint_STROKE); //通过不同的参数在Canvas上画图片 canvas.drawPicture(picture); canvas.drawPicture(picture, dst); canvas.drawPicture(picture, dst);
//在Canvas上画圆和椭圆 canvas.drawCircle(cx, cy, radius, paint_STROKE); //扇形 canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint_STROKE); //椭圆 canvas.drawOval(oval, paint_STROKE);
//在Canvas上画线 canvas.drawLine(startX, startY, stopX, stopY, paint_STROKE); canvas.drawLines(pts, paint_STROKE); canvas.drawLines(pts, offset, count, paint_STROKE);
//在Canvas画布上画点 canvas.drawPoint(x, y, paint_STROKE); canvas.drawPoints(pts, paint_STROKE); canvas.drawPoints(pts, offset, count, paint_STROKE);
//在Canvas画布上画矩形或圆角矩形 canvas.drawRect(r, paint_STROKE); canvas.drawRect(rect, paint_STROKE); canvas.drawRoundRect(rect, rx, ry, paint_STROKE);
//利用Canvas在画布上写文字相关Api canvas.drawPosText(text, pos, paint_STROKE); canvas.drawPosText(text, index, count, pos, paint_STROKE); canvas.drawText(text, x, y, paint_STROKE); canvas.drawText(text, index, count, x, y, paint_STROKE); canvas.drawText(text, start, end, x, y, paint_STROKE); canvas.drawText(text, start, end, x, y, paint_STROKE); canvas.drawTextOnPath(text, path_STROKE, hOffset, vOffset, paint_STROKE); canvas.drawTextOnPath(text, index, count, path_STROKE, hOffset, vOffset, paint_STROKE);
//绘制顶点(指多边形)
canvas.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors, colorOffset, indices, indexOffset, indexCount, paint_STROKE);
//给Canvas设置一个bitmap画布
canvas.setBitmap(bitmap);
//获取画布宽高
canvas.getHeight();
canvas.getWidth();
//获取和设置Canvas画布像素密度
canvas.getDensity();
canvas.setDensity(density);
//获取和设置画布过滤器
canvas.getDrawFilter();
canvas.setDrawFilter(filter);
//获取Canvas上截取的矩形
canvas.getClipBounds();
canvas.getClipBounds(bounds);
//获取Canvas的矩阵
canvas.getMatrix();
canvas.getMatrix(ctm);
canvas.setMatrix(matrix);
//返回此位图绘制画布上允许的最大高度和宽度。试图用一个位图画比这个值高会导致错误。
canvas.getMaximumBitmapHeight();
canvas.getMaximumBitmapWidth();
//返回画布上的私有堆栈状态的矩阵或截图。
canvas.getSaveCount();
//判断此画布使用硬件加速
canvas.isHardwareAccelerated();
//如果当前层引入装置是不透明的返回true
canvas.isOpaque();
//暂时没有用过,不知道怎么用
canvas.quickReject(path_STROKE, type);
canvas.quickReject(rect, type);
canvas.quickReject(left, top, right, bottom, type);
//返回调用save()之前状态,是用来清除所有修改矩阵或裁剪自上次保存状态
canvas.restore();
canvas.restoreToCount(saveCount);
//保存当前画布状态,之后的的 translate,scale,rotate,skew,concat or clipRect,clipPath等都会照常运行
//但调用restore()后,会回复到save之前的Canvas状态
canvas.save();
canvas.save(saveFlags);
//和save()类似,在现在的层上新建一层,调用restore后回复原状态
canvas.saveLayer(bounds, paint_STROKE, saveFlags);
canvas.saveLayer(left, top, right, bottom, paint_STROKE, saveFlags);
canvas.saveLayerAlpha(bounds, alpha, saveFlags);
canvas.saveLayerAlpha(left, top, right, bottom, alpha, saveFlags);
//让画布扭曲
canvas.skew(sx, sy);
//让画布旋转
canvas.rotate(degrees);
canvas.rotate(degrees, px, py);
//让画布缩放
canvas.scale(sx, sy);
canvas.scale(sx, sy, px, py);
//让画布翻转
canvas.translate(dx, dy);
Tags:Canvas ondraw android绘制 android画图 Android