android画图类---Canvas浅谈



Canvas类

首先看一下Canvas类官方说明

  1. The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host
  2. the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,Path, text, Bitmap), and a paint (to describe the colors and styles for thedrawing).
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host
the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,Path, text, Bitmap), and a paint (to describe the colors and styles for thedrawing).
大致意思:Canvas是一个画布,它具有执行画的动作.

它有4个基本的组件,包括:

一个Bitmap的画布,

一个Canvas类会调用这个Bitmap(把图画在这个Bitmap身上),

一个填充的域 Path、Rect、ovil等(比如矩形,路径,文字,图片等)

一个画笔 Paint(用来定义画出来的控件的颜色,样式等等)


(一)获取Canvas

第一种在所有View的onDraw(Canvas canvas)方法中直接获取

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. <span style="white-space:pre"> </span>super.onDraw(canvas);
  4. }
@Override
protected void onDraw(Canvas canvas) {
<span style="white-space:pre">	</span>super.onDraw(canvas);
}

第二种直接new一个Canvas

  1. //先得到一个Bitmap对象,通过Canvas的构造函数创建一个Canvas类
  2. Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
  3. Canvas canvas = new Canvas(bitmap);
  4. //或者先创建一个Canvas类,通过setBitmap()方法创建一个画布
  5. Canvas canvas = new Canvas();
  6. canvas.setBitmap(bitmap);
//先得到一个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类的方法一定要熟悉,以便在运用的时候能够灵活的掌握


  1. //获取画布宽高
  2. canvas.getHeight();
  3. canvas.getWidth();
  4. //获取Canvas画布像素密度
  5. canvas.getDensity();
  6. //获取画布过滤器
  7. canvas.getDrawFilter();
		//获取画布宽高
		canvas.getHeight();
		canvas.getWidth();
		
		//获取Canvas画布像素密度
		canvas.getDensity();
		
		//获取画布过滤器
		canvas.getDrawFilter();


  1. //设置Canvas的Matrix矩阵
  2. public void concat(Matrix matrix)
//设置Canvas的Matrix矩阵
	public void concat(Matrix matrix)

  1. //clip相关的方法主要是对画布Canvas进行裁剪
  2. canvas.clipPath(path_STROKE);
  3. canvas.clipPath(path_STROKE, op);
  4. canvas.clipRect(rect);
  5. canvas.clipRect(rect, op);
  6. canvas.clipRect(left, top, right, bottom);
  7. canvas.clipRect(left, top, right, bottom, op);
  8. canvas.clipRegion(region);
  9. canvas.clipRegion(region, op);
//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);

  1. //给Canvas画布填充颜色
  2. canvas.drawARGB(a, r, g, b);
  3. canvas.drawColor(color);
  4. canvas.drawColor(color, mode);
		//给Canvas画布填充颜色
		canvas.drawARGB(a, r, g, b);
		canvas.drawColor(color);
		canvas.drawColor(color, mode);

  1. //通过不同参数在Canvas上画位图
  2. canvas.drawBitmap(bitmap, left, top, paint_STROKE);
  3. canvas.drawBitmap(bitmap, matrix, paint_STROKE);
  4. canvas.drawBitmap(bitmap, src, dst, paint_STROKE);
  5. canvas.drawBitmap(bitmap, src, dst, paint_STROKE);
  6. canvas.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint_STROKE);
  7. canvas.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, paint_STROKE);
  8. //通过不同的参数在Canvas上画图片
  9. canvas.drawPicture(picture);
  10. canvas.drawPicture(picture, dst);
  11. canvas.drawPicture(picture, dst);
		//通过不同参数在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);

  1. //在Canvas上画圆和椭圆
  2. canvas.drawCircle(cx, cy, radius, paint_STROKE);
  3. //扇形
  4. canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint_STROKE);
  5. //椭圆
  6. canvas.drawOval(oval, paint_STROKE);
//在Canvas上画圆和椭圆
canvas.drawCircle(cx, cy, radius, paint_STROKE);

//扇形
canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint_STROKE);

//椭圆
canvas.drawOval(oval, paint_STROKE);

  1. //在Canvas上画线
  2. canvas.drawLine(startX, startY, stopX, stopY, paint_STROKE);
  3. canvas.drawLines(pts, paint_STROKE);
  4. canvas.drawLines(pts, offset, count, paint_STROKE);
		//在Canvas上画线
		canvas.drawLine(startX, startY, stopX, stopY, paint_STROKE);
		canvas.drawLines(pts, paint_STROKE);
		canvas.drawLines(pts, offset, count, paint_STROKE);

  1. //在Canvas画布上画点
  2. canvas.drawPoint(x, y, paint_STROKE);
  3. canvas.drawPoints(pts, paint_STROKE);
  4. canvas.drawPoints(pts, offset, count, paint_STROKE);
		//在Canvas画布上画点
		canvas.drawPoint(x, y, paint_STROKE);
		canvas.drawPoints(pts, paint_STROKE);
		canvas.drawPoints(pts, offset, count, paint_STROKE);

  1. //在Canvas画布上画矩形或圆角矩形
  2. canvas.drawRect(r, paint_STROKE);
  3. canvas.drawRect(rect, paint_STROKE);
  4. canvas.drawRoundRect(rect, rx, ry, paint_STROKE);
		//在Canvas画布上画矩形或圆角矩形
		canvas.drawRect(r, paint_STROKE);
		canvas.drawRect(rect, paint_STROKE);
		canvas.drawRoundRect(rect, rx, ry, paint_STROKE);

  1. //利用Canvas在画布上写文字相关Api
  2. canvas.drawPosText(text, pos, paint_STROKE);
  3. canvas.drawPosText(text, index, count, pos, paint_STROKE);
  4. canvas.drawText(text, x, y, paint_STROKE);
  5. canvas.drawText(text, index, count, x, y, paint_STROKE);
  6. canvas.drawText(text, start, end, x, y, paint_STROKE);
  7. canvas.drawText(text, start, end, x, y, paint_STROKE);
  8. canvas.drawTextOnPath(text, path_STROKE, hOffset, vOffset, paint_STROKE);
  9. canvas.drawTextOnPath(text, index, count, path_STROKE, hOffset, vOffset, 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);


  1. //绘制顶点(指多边形)
  2. canvas.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors, colorOffset, indices, indexOffset, indexCount, paint_STROKE);
		//绘制顶点(指多边形)
		canvas.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors, colorOffset, indices, indexOffset, indexCount, paint_STROKE);

  1. //给Canvas设置一个bitmap画布
  2. canvas.setBitmap(bitmap);
  3. //获取画布宽高
  4. canvas.getHeight();
  5. canvas.getWidth();
  6. //获取和设置Canvas画布像素密度
  7. canvas.getDensity();
  8. canvas.setDensity(density);
  9. //获取和设置画布过滤器
  10. canvas.getDrawFilter();
  11. canvas.setDrawFilter(filter);
  12. //获取Canvas上截取的矩形
  13. canvas.getClipBounds();
  14. canvas.getClipBounds(bounds);
  15. //获取Canvas的矩阵
  16. canvas.getMatrix();
  17. canvas.getMatrix(ctm);
  18. canvas.setMatrix(matrix);
  19. //返回此位图绘制画布上允许的最大高度和宽度。试图用一个位图画比这个值高会导致错误。
  20. canvas.getMaximumBitmapHeight();
  21. canvas.getMaximumBitmapWidth();
  22. //返回画布上的私有堆栈状态的矩阵或截图。
  23. canvas.getSaveCount();
  24. //判断此画布使用硬件加速
  25. canvas.isHardwareAccelerated();
  26. //如果当前层引入装置是不透明的返回true
  27. canvas.isOpaque();
  28. //暂时没有用过,不知道怎么用
  29. canvas.quickReject(path_STROKE, type);
  30. canvas.quickReject(rect, type);
  31. canvas.quickReject(left, top, right, bottom, type);
		
				
		//给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);

  1. //返回调用save()之前状态,是用来清除所有修改矩阵或裁剪自上次保存状态
  2. canvas.restore();
  3. canvas.restoreToCount(saveCount);
  4. //保存当前画布状态,之后的的 translate,scale,rotate,skew,concat or clipRect,clipPath等都会照常运行
  5. //但调用restore()后,会回复到save之前的Canvas状态
  6. canvas.save();
  7. canvas.save(saveFlags);
  8. //和save()类似,在现在的层上新建一层,调用restore后回复原状态
  9. canvas.saveLayer(bounds, paint_STROKE, saveFlags);
  10. canvas.saveLayer(left, top, right, bottom, paint_STROKE, saveFlags);
  11. canvas.saveLayerAlpha(bounds, alpha, saveFlags);
  12. canvas.saveLayerAlpha(left, top, right, bottom, alpha, saveFlags);
		//返回调用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);

  1. //让画布扭曲
  2. canvas.skew(sx, sy);
  3. //让画布旋转
  4. canvas.rotate(degrees);
  5. canvas.rotate(degrees, px, py);
  6. //让画布缩放
  7. canvas.scale(sx, sy);
  8. canvas.scale(sx, sy, px, py);
  9. //让画布翻转
  10. canvas.translate(dx, dy);
		//让画布扭曲
		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);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值