Canvas 详解

来源:互联网 发布:航天数据股份有限公司 编辑:程序博客网 时间:2024/06/04 00:56

Canvas 详解

题外话

先插播题外话,其实这个canvas详解很多人都写过,好多还是大神写的,我的这个肯定不能和大神比了,有人就说这简直就是重复造轮子(其实吧,如果将轮子比作车轮,我认为我这里电瓶车的轮子,大神的是法拉利的轮子,两者之间是有不少差距滴,不过正因为有差距,我们才需要努力去学习),浪费时间,不过我却不这么认为,比如通过这一次整理canvas,我了解了它里面的大部分方法(有些还是不了解),知道了他们的用法等等,如果我只是去看看别人写的文章,不自己去整理的话,我想我会很快就忘了,脑袋里也不会有概念,没准以后写东西滴时候,系统明明帮我们实现好了,我们却还要自己去实现(还不一定能实现出来)。。。。。
—–最后来一句,现在重复造别人的轮子,以后就可以造自己的轮子了。↖(^ω^)↗

正文

1.构造方法 (这个应该不需要具体说明了)

1.

Canvas() 

2.

//使用一个bitmap来作为画布Canvas(Bitmap bitmap) 

2.draw开头的方法

2.1 填充画布颜色

2.1.1

drawARGB(int a, int r, int g, int b) 

2.1.2

drawColor(int color)

2.1.3 具体的显示看这里

//方法的第二个参数,用于当两个图片或者颜色重叠时,具体怎么显示用的,//不过由于canvas中有这个参数,paint中也有这个参数,我被两个的结合使用搞晕了,//所以就准备canvas用默认的,paint里面去改就好了    注意Destination和Source的区分   我貌似发现一点,如果//在canvas中设置mode与paint中设置mode时,destination和source是反着滴,要是大家有//什么好的方式学习这个,请告知。drawColor(int color, PorterDuff.Mode mode)

2.1.4

drawRGB(int r, int g, int b)

2.2 画扇形或者弧 (api 21以上)

2.2.1

drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

2.2.2

//其中RectF就是上面的前四个参数new 出来的RectF对象  RectF f = new RectF(left, top, right, bootm),  //startAngle 开始的度数((以bottom-top)的一半的x轴的水平横线为0度,顺时针为正角度,否则为负角度,sweepAngle表示要画的度数 useCenter 是否填充(true-> 扇形) (false-> 弧形))drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

2.3 位图

2.3.1

// 其中matrix是对bitmap进行变换 比如旋转(差原图和旋转图)drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) 

2.3.2

drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) (@deprecated api 21

2.3.3

drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha,Paint paint)(@deprecatedapi 21

2.3.4

// 第一个参数表示需要绘制bitmap的区域--->(0,0,bitmap.getWidth, bitmap.getHeight)绘制整个bitmap 若(0,0,bitmap.getwidth / 2, bitmap.getheight/2)绘制bitmap的1/4(留下左上角那一块) ,这个参数可以为null(默认为绘制全部), 第二个参数表示对bitmap进行缩放(不可以为null),不进行缩放可以设置为(0, 0, bitmap.getwidth, bitmap.getheight),若是(0, 300, 350, 350)表示绘制的顶点在(0,300) 宽度为(350-0),高度为(350 -300) 第四个参数可以为nulldrawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) 

2.3.5

//同上drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) 

2.3.6

//第二三个参数表示绘制的原点(0,0)表示顶点绘制 (300, 300)表示绘制顶点在300drawBitmap(Bitmap bitmap, float left, float top, Paint paint)

2.3.7

//对图片进行扭曲,通过meshwidth和meshheight两个参数将图片分成meshwidth*meshheight 个格子 ,然后 verts是一个(meshwidth + 1) * (meshheight + 1)的数组,实际上他是一个形如(x0,y0),(x1,y1)的数组,通过控制这些位置来对bitmap进行扭曲 vertoffset 表示跳过多少个格子进行扭曲。Colors如果不为null的话 应该有(meshwidth + 1) * (meshheight + 1)个值,对每个格子进行颜色插入, coloroffset表示跳过多少个颜色开始插入 ------->  难用,感觉无法预料效果 (对于vertOffset和colorOffset的用法还需考虑一下  >0 时   数组溢出)drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint)

2.4 画圆

2.4.1

drawCircle(float cx, float cy, float radius, Paint paint)

2.5 画线

2.5.1

drawLine(float startX, float startY, float stopX, float stopY, Paint paint)

2.5.2

//pts 线的端点 4个值组成一条线   offset 跳过多少个pts里面的数据   count 从跳过的数据开始画多少个点drawLines(float[] pts, int offset, int count, Paint paint)

2.5.3

drawLines(float[] pts, Paint paint) //同上

2.6 画椭圆

2.6.1

//若是基于正方形画出的椭圆就变成了圆形drawOval(float left, float top, float right, float bottom, Paint paint)

2.6.2

drawOval(RectF oval, Paint paint)

2.7 Paint()

2.7.1

//绘制背景(我没有试出来其他效果,就是在drawPaint的时候 画布背景色变成了paint设置的颜色,至于其他属性,还有待发掘)drawPaint(Paint paint) 

2.8 Patch

2.8.1

//我在官方文档(API 23)中发现了这两个方法,但是在实际的canvas中并没有发现这个方法,我的targetSdkVersion=23。drawPatch(NinePatch patch, RectF dst, Paint paint)

2.8.2

drawPatch(NinePatch patch, Rect dst, Paint paint)

2.9 画path

2.9.1 具体看这里

drawPath(Path path, Paint paint)// path这个对象不光光可以用来画线,他还有很多其他的方法,具体可以看看看这里

2.10 画picture(矢量图)

2.10.1

//没用过  目前不太清楚如何用  后面更新drawPicture(Picture picture, RectF dst)

2.10.2

drawPicture(Picture picture)

2.10.3

drawPicture(Picture picture, Rect dst)

2.11 画点

2.11.1

//pts 点坐标的集合drawPoint(float x, float y, Paint paint)

2.11.2

drawPoints(float[] pts, Paint paint)

2.11.3

drawPoints(float[] pts, int offset, int count, Paint paint)

2.12 postext(@Deprecated api 16)

2.12.1

drawPosText(String text, float[] pos, Paint paint)

2.12.2

drawPosText(char[] text, int index, int count, float[] pos, Paint paint)

2.13 画矩形

2.13.1

drawRect(float left, float top, float right, float bottom, Paint paint)

2.13.2

drawRect(Rect r, Paint paint)

2.13.3

drawRect(RectF rect, Paint paint)

2.14 圆角矩形

2.14.1

// rx ry 分别为x轴和y轴上圆角的半径drawRoundRect(RectF rect, float rx, float ry, Paint paint) 

2.14.2

drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)

2.15 text

2.15.1

drawText(CharSequence text, int start, int end, float x, float y, Paint paint)

2.15.2

drawText(String text, float x, float y, Paint paint)

2.15.3

drawText(char[] text, int index, int count, float x, float y, Paint paint)

2.15.4

drawText(String text, int start, int end, float x, float y, Paint paint)

2.16 drawTextOnPath (根据path路径画text)

2.16.1

 // hoffset表示水平偏移, vofset表示垂直偏移drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)

2.16.2

drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint) //同上

2.17 TextRun //和预想的结果不一样 还需后面再验证 这里先不做解释 后续更新

2.17.1

drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, Paint paint)

2.17.2

drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, Paint paint)

2.18 drawVertices //这个后续再更新吧 和mesh一样没搞懂

2.18.1

drawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, Paintpaint)

3.clip开头的方法

3.1 clipout(api26以上)//具体效果需要调整到 26的api验证 (目前还没有验证)

3.1.1

clipOutPath(Path path)

3.1.2

clipOutRect(int left, int top, int right, int bottom)

3.1.3

clipOutRect(Rect rect)

3.1.4

clipOutRect(float left, float top, float right, float bottom)

3.1.5

clipOutRect(RectF rect)

3.2 clipPath

3.2 , 3.3 clip方法都是对画布进行裁剪(相当于对把裁剪后的部分作为新的画布,比如原本有一个400 * 400 的矩形画布,如果裁剪成200*200的画布,那么之后所画的东西都会在200*200的画布上)
3.2.1

clipPath(Path path, Region.Op op)

3.2.2

clipPath(Path path)

3.3 clipRect

3.3.1

clipRect(float left, float top, float right, float bottom, Region.Op op)

3.3.2

clipRect(RectF rect)

3.3.3

clipRect(Rect rect)

3.3.4

clipRect(Rect rect, Region.Op op)

3.3.5

clipRect(int left, int top, int right, int bottom)

3.3.6

clipRect(float left, float top, float right, float bottom)

3.3.7

clipRect(RectF rect, Region.Op op)

接下来主要对clip中的Region.Op参数解释(别人那里复制过来滴后面贴出原文地址)一下 原文地址

A:表示第一个裁剪的形状;
B:表示第二次裁剪的形状;
Region.Op.DIFFERENCE :是A形状中不同于B的部分显示出来
Region.Op.REPLACE:是只显示B的形状
Region.Op.REVERSE_DIFFERENCE :是B形状中不同于A的部分显示出来,这是没有设置时候默认的
Region.Op.INTERSECT:是A和B交集的形状
Region.Op.UNION:是A和B的全集
Region.Op.XOR:是全集形状减去交集形状之后的部分

4 canvas的一些其他方法

4.1 getClip

4.1.1

// 返回被剪裁的rect对象final Rect    getClipBounds() 

4.1.2

// 我在测试这个方法的时候  发现都会返回true  不知道是我哪里写错了  后面再更新这个方法吧boolean   getClipBounds(Rect bounds)  

4.2 Density

4.2.1

//返回画布的密度int getDensity() 

4.2.2

//设置canvas支持的位图的密度void  setDensity(int density)

4.3 DrawFilter 原文地址

4.3.1

//这两个东西不知道用来干什么 看了别人的一篇博客有讲到//PaintFlagsDrawFilter介绍(DrawFilter默认啥都没有,其默认有一个子类就是PaintFlagsDrawFilter):唯一的方法 clearBits 清除Paint已经存在的指定flag setBits 设置Paint的flag public PaintFlagsDrawFilter(int clearBits, int setBits)//抗锯齿写法一//paint.setAntiAlias(true);//抗锯齿写法二,按位操作就行//canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));DrawFilter    getDrawFilter()  

4.3.2

void  setDrawFilter(DrawFilter filter) 

4.4 height、weight

4.4.1

//获取当前绘图层的高度int   getHeight()  

4.4.2

//获取当前绘图层的宽度int   getWidth()

4.4.3

// 返回使用此画布绘制的位图的最大允许高度。int   getMaximumBitmapHeight() 

4.4.4

// 返回使用此画布绘制的位图的最大允许宽度。int   getMaximumBitmapWidth() 

4.5 Matrix

4.5.1

//api16 已废弃void    getMatrix(Matrix ctm)  

4.5.2

// api16已废弃final Matrix   getMatrix() 

4.5.3

//用指定的矩阵替换当前矩阵  最好是利用scale或者translate方法来达到目的void  setMatrix(Matrix matrix) 

4.6 save、restore

4.6.1

//返回Canvas私有堆栈上矩阵/剪辑状态的数量。int   getSaveCount() 

4.6.2

//与sava() 一起使用 如果调用次数比save多  会报错先调用sava方法,对现场进行记录一下   然后对当前已经draw了的图形进行变化,比如旋转45°,然后调用 restore方法  画一个正方形,这个时候的正方形没有被旋转45°,如果不调用restore的话  ,  画的正方形会被旋转45°void  restore()  

4.6.3

// 返回调用save方法之前的状态void  restoreToCount(int saveCount) 

4.6.4

//api 26已被废弃 用 sava()  saveFlags int save(int saveFlags)  

4.6.5

//对现场进行记录  对比 restore()int   save() 

4.7 isHardwareAccelerated

4.7.1

//是否允许硬件加速boolean    isHardwareAccelerated() 

4.8 isOpaque

4.8.1

//如果当前图层绘制的设备是不透明的(即不支持每像素alpha),则返回true。boolean    isOpaque() 

4.9 quickReject

4.9.1

//如果指定的矩形在被当前矩阵变换之后将完全位于当前剪辑之外,则返回true。 调用它来检查你打算绘制的区域是否被剪切(因此你可以跳过绘制调用)。boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type)

4.9.2

//同上boolean   quickReject(RectF rect, Canvas.EdgeType type) 

4.9.3

// 4.9这个方法 纯属官方文档放到google翻译里面翻译了一下........//如果指定的路径被当前矩阵转换后将完全位于当前剪辑之外,则返回true。 调用它来检查你打算绘制的区域是否被剪切(因此您可以跳过绘制调用)。 注意:对于速度,即使路径本身可能不与剪辑相交(即,路径的边界相交,但路径不相交),它可能返回false。boolean   quickReject(Path path, Canvas.EdgeType type) 

4.10 rotate(旋转)

4.10.1

void rotate(float degrees)  

4.10.2

//以指定的点为中心点final void   rotate(float degrees, float px, float py) 

4.11 saveLayer

4.11.1

//pi26废弃  调用saveLayer(RectF bounds, Paint paint)int  saveLayer(RectF bounds, Paint paint, int saveFlags) 

4.11.2

//保存图层,相当于在原来图层上新建一个图层,后面画的图形全在新的图层上,调用restore后 新图层上面绘制的图形会被copy到上一个图层中。int   saveLayer(RectF bounds, Paint paint) 

4.11.3

//同上int  saveLayer(float left, float top, float right, float bottom, Paint paint) 

4.11.4

//Api26 中抛弃int  saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags)

4.11.5

//同上  只是对图层多了一个透明度int  saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)

4.11.6

//同上int  saveLayerAlpha(RectF bounds, int alpha) 

4.11.7

//同上int  saveLayerAlpha(RectF bounds, int alpha, int saveFlags) 

4.11.8

int saveLayerAlpha(float left, float top, float right, float bottom, int alpha)

4.12 scale(缩放)

4.12.1

//以px,py点 进行缩放final void  scale(float sx, float sy, float px, float py) 

4.12.2

// 以绘制原点进行缩放void    scale(float sx, float sy) 

4.13 setBitmap

4.13.1

//设置bitmap为画布  还需要测试void  setBitmap(Bitmap bitmap)  

4.14 skew(倾斜)

4.14

void  skew(float sx, float sy) // 指定偏移量  按照度数 换算成tan 函数的值

4.15 translate(平移)

4.15

//平移void  translate(float dx, float dy) 

4.16 concat

4.16.1

//预先定义个martrix???    没有用过。。。。concat(Matrix matrix)
原创粉丝点击