[Android 知识点] 自定义View(三)

来源:互联网 发布:db2和oracle的sql区别 编辑:程序博客网 时间:2024/06/05 19:48

Canvas类

Canvas我们可以称之为画布,能够在上面绘制各种东西,是安卓平台2D图形绘制的基础,非常强大。

drawColor(int color) 绘制底色

drawARGB(int a, int r, int g, int b)
drawColor(int color, PorterDuff.Mode mode)
  • PorterDuff.Mode

image

canvas.drawColor(Color.BLUE); //绘制蓝色

drawPoint(float x, float y, Paint paint) 绘制一个点

  • float X:点的X坐标
  • float Y:点的Y坐标
canvas.drawPoint(100, 100, paint);

image

drawPoints(float[] pts, Paint paint)

drawPoints(float[] pts, int offset, int count, Paint paint)
  • float[] pts:点的合集
  • offset:偏移点
  • count:绘制个数
float []pts={10,10,100,100,200,200,400,400};  canvas.drawPoints(pts, 2, 4, paint);  

image

drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 绘制直线

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

绘制一组线,每四数字(两个点的坐标)确定一条线

canvas.drawLine(300,300,500,600,mPaint);   // 在坐标(300,300)(500,600)之间绘制一条直线 canvas.drawLines(new float[]{              // 绘制一组线 每四数字(两个点的坐标)确定一条线    100,200,200,200,    100,300,200,300},mPaint);

image

drawRect(float left, float top, float right, float bottom, Paint paint) 绘制矩形

drawRect(Rect r, Paint paint) 坐标封装为Rect
drawRect(RectF rect, Paint paint) 坐标封装RectF

确定确定一个矩形最少需要四个数据,就是对角线的两个点的坐标值,这里一般采用左上角和右下角的两个点的坐标。

// 第一种canvas.drawRect(100,100,800,400,mPaint);// 第二种Rect rect = new Rect(100,100,800,400);canvas.drawRect(rect,mPaint);// 第三种RectF rectF = new RectF(100,100,800,400);canvas.drawRect(rectF,mPaint);
  • 区别:Rect是int(整形)的,而RectF是float(单精度浮点型)的。

矩形工具类RectF与Rect:

RectF:

构造函数有下面四个,但最常用的还是第二个,根据四个点构造出一个矩形;

  1. RectF()
  2. RectF(float left, float top, float right, float bottom)
  3. RectF(RectF r)
  4. RectF(Rect r)

Rect

构造函数如下,最常用的也是根据四个点来构造矩形

  1. Rect()
  2. Rect(int left, int top, int right, int bottom)
  3. Rect(Rect r)

drawRoundRect(RectF rect, float rx, float ry, Paint paint) 绘制圆角矩形

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

image

红线标注的 rx 与 ry 就是两个半径,也就是相比绘制矩形多出来的那两个参数。

// 第一种RectF rectF = new RectF(100,100,800,400);canvas.drawRoundRect(rectF,30,30,mPaint);// 第二种canvas.drawRoundRect(100,100,800,400,30,30,mPaint);

image

drawCircle(float cx, float cy, float radius, Paint paint) 绘制圆

canvas.drawCircle(500,500,400,mPaint);  // 绘制一个圆心坐标在(500,500),半径为400 的圆。

image

drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 绘制圆弧

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
  • oval:生成椭圆的矩形
  • startAngle:弧开始的角度,以X轴正方向为0度
  • sweepAngle:弧持续的角度
  • useCenter:是否有弧的两边,True,还两边,False,只有一条弧

image

drawOval(float left, float top, float right, float bottom, Paint paint) 绘制椭圆

drawOval(RectF oval, Paint paint)

给定一个矩形,即可获得一个内切圆

drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) 绘图

Matrix方法

Matrix其实就是一个3*3的矩阵,利用这个矩阵对图像操作。

构造函数

  • Matrix() 一个单位矩阵
  • Matrix(Matrix src) 对src拷贝

图像处理

  • Translate 平移
  • Rotate 旋转
  • Scale 缩放
  • Skew 倾斜

由于矩阵不满足交换律,所以用矩阵B乘以矩阵A,需要考虑是左乘(B*A),还是右乘(A*B),post的方式是对原矩阵进行后乘,pre方式是对原矩阵进行前乘,set的方式调用就会进行一次reset重置

平移

  • postTranslate(float dx, float dy) 平移(dx,dy)
  • preTranslate (float dx, float dy)
  • setTranslate (float dx, float dy)
//平移(w,h)mMatrix.reset();mMatrix.postTranslate(w, h);canvas.drawBitmap(mBitmap, mMatrix, mPaint);

旋转

  • preRotate(float degrees) 旋转degrees度
  • preRotate(float degrees, float px, float py) 基于坐标(px,py)进行进行旋转
  • postRotate (float degrees)
  • postRotate (float degrees, float px, float py)
  • setRotate (float degrees)
  • setRotate (float degrees, float px, float py)
  • setSinCos(float sinValue, float cosValue)
  • setSinCos(float sinValue, float cosValue, float px, float py)
 mMatrix.preRotate(90);

缩放

  • postScale(float sx, float sy, float px, float py) 基于坐标(px,py)进行按照(sx,sy)比例进行放缩
  • postScale(float sx, float sy) 基于坐标(0,0)进行按照(sx,sy) 比例进行放缩
  • preScale (float sx, float sy)
  • preScale (float sx, float sy, float px, float py)
  • setScale (float sx, float sy)
  • setScale (float sx, float sy, float px, float py)
//放大2倍mMatrix.reset();mMatrix.postScale(2, 2);canvas.drawBitmap(mBitmap, mMatrix, mPaint);

倾斜

  • preSkew(float kx, float ky) kx,ky为斜率
  • preSkew(float kx, float ky, float px, float py)
  • postSkew (float kx, float ky)
  • postSkew (float kx, float ky, float px, float py)
  • setSkew (float kx, float ky)
  • setSkew (float kx, float ky, float px, float py)

组合变化

镜像

镜像变化:对于scale变化,如果以一个负数缩放,那么会将该图像绘制到坐标系统的负值空间。由于(0,0)点位于左上角,使用x轴上的负数会导致向左绘制图像。因此我们需要使用postTranslate方法,将图像向右移动即可实现镜像变化。

matrix.setScale(-1, 1);matrix.postTranslate(mBitmap.getWidth(),0);
倒影
matrix.postScale(1, -1);matrix.postTranslate(0, mBitmap.getHeight());

drawBitmap

drawBitmap(int[] colors, int offset,…)
  • drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)
drawBitmap(int[] colors, int offset,…)
  • drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint)
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
  • 第一个参数是图片
  • 第二个参数是图片需显示区域
  • 第三个参数表示图片绘画的位置
  • 第四个参数表示画笔
drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
  • 提供一个左上角坐标
drawBitmapMesh(Bitmap bitmap, int meshWidth, …)
  • drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint)

对bitmap进行扭曲

  • bitmap    需要扭曲的源位图
  • meshWidth   控制在横向上把该源位图划成成多少格
  • meshHeight   控制在纵向上把该源位图划成成多少格
  • verts     长度为(meshWidth + 1) * (meshHeight + 1) * 2的数组,它记录了扭曲后的位图各顶点位置
  • vertOffset 控制verts数组中从第几个数组元素开始才对bitmap进行扭曲

参考别人的博客

drawPaint(Paint paint)

drawPath(Path path, Paint paint) 绘制任意多边形

  • path:包含路径信息的Path对象。
Path path = new Path();                     //Path对象  path.moveTo(50, 100);                       //起始点  path.lineTo(50, 300);                       //连线到下一点  path.lineTo(100, 500);                      //连线到下一点  path.lineTo(400, 500);                      //连线到下一点  path.lineTo(300, 300);                      //连线到下一点  path.lineTo(450, 50);                       //连线到下一点  path.lineTo(200, 200);                      //连线到下一点  canvas.drawPath(path, paint);               //绘制任意多边形  

path类

构造函数

  • Path()
  • Path(Path src)

addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle) 画弧

addArc(RectF oval, float startAngle, float sweepAngle)
  • float left, float top, float right, float bottom 构建矩形
  • float startAngle, float sweepAngle 开始和结束角度

addCircle(float x, float y, float radius, Path.Direction dir) 画圆

  • float x, float y 圆心
  • radius 半径
  • Path.Direction 方向:Diection.CCW 逆时针方向 ,Diection.CW 顺时针方向

addOval(RectF oval, Path.Direction dir) 椭圆

addOval(float left, float top, float right, float bottom, Path.Direction dir)
  • RectF oval 矩形
  • Path.Direction 方向:Diection.CCW 逆时针方向 ,Diection.CW 顺时针方向

addPath(Path src, Matrix matrix) 将src经过矩阵m变换后添加到当前path上

addPath(Path src) 拷贝一个副本

addPath(Path src, float dx, float dy) 将src平移(x,y)后添加到当前path上

addRect(RectF rect, Path.Direction dir) 向路径中添加一个封闭的矩形轮廓

  • Path.Direction 方向:Diection.CCW 逆时针方向 ,Diection.CW 顺时针方向

addRect(float left, float top, float right, float bottom, Path.Direction dir) 向路径中添加一个封闭的矩形轮廓

  • Path.Direction 方向:Diection.CCW 逆时针方向 ,Diection.CW 顺时针方向

addRoundRect(RectF rect, float rx, float ry, Path.Direction dir) 向路径中添加一个封闭的圆角矩形轮廓

  • rx,ry 代表弧度

addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Path.Direction dir) 向路径中添加一个封闭的圆角矩形轮廓

  • rx,ry 代表弧度

addRoundRect(RectF rect, float[] radii, Path.Direction dir) 向路径中添加一个封闭的圆角矩形轮廓

addRoundRect(float left, float top, float right, float bottom, float[] radii, Path.Direction dir)

arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo) 画弧

arcTo(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean forceMoveTo) 画弧

arcTo(RectF oval, float startAngle, float sweepAngle) 画弧

close() 关闭路径

computeBounds(RectF bounds, boolean exact)计算path中控制的点的边界,将结果写入bounds中,如果Path中只有0或者1个点,那么bounds会返回(0,0,0,0)的值,exact这个变量没用

cubicTo(float x1, float y1, float x2, float y2, float x3, float y3)从最后一个点开始增加一段贝塞尔曲线到path当中,接近(x1,y1),(x2,y2),同时在(x3,y3)中结束,如果这个轮廓没有调用过moveTo(),那么将会以(0,0)作为起点

getFillType() 获取path的填充方式,这定义了在内部的计算方法,默认值为WINDING

incReserve(int extraPtCount) 提示path将会增加extraPtCount个点,这能使path有效率的分配它的存储空间

isConvex() 返回路径的凸度,由路径的内容决定

isEmpty () 查询path是否为空(不包括任何直线,曲线)

isInverseFillType() 判断filltype是否为INVERSE中的一种

isRect(RectF rect) 如果path指定了一个矩形,那么返回true

lineTo(float x, float y) 画线

moveTo(float x, float y) 移动光标

offset(float dx, float dy, Path dst) 平移,将结果写到dst中,如果dst为null,那么改变后的结果直接写在当前path中

offset(float dx, float dy) 平移

op(Path path1, Path path2, Path.Op op) 将path1和path2按照op的规定处理,将新得到的path存到当前path中

op(Path path,Path.op op) 将path与当前path按照op处理,将新得到path存到当前path中

quadTo(float x1, float y1, float x2, float y2) 增加一条二次的贝塞尔曲线到path的最后一个点,接近(x1,y1),在(x2,y2)点结束。如果path没有调用moveTo来确定第一个点,那第一个点设置为(0,0)

rCubicTo(float x1, float y1, float x2, float y2, float x3, float y3)

rLineTo(float dx, float dy)

rMoveTo(float dx, float dy)

rQuadTo(float dx1, float dy1, float dx2, float dy2)

相对位置

reset() 清空Path的任何直线和曲线,让它变成空,但是不会改变filltype

rewind() 回退当前path,清除掉所有直线,曲线,但是保留它内部的数据结构,以便更好的重新使用

set(Path src) 用src的值替代当前path的值

setFillType(Path.FillType ft) 设置路径的填充类型

setLastPoint(float dx, float dy) 设置当前path的最后一个点

toggleInverseFillType() 切换的filltype相反状态

transform(Matrix matrix, Path dst) 将path进行matrix变化后,将结果保存到dst当中,如果dst=null,将结果保存到当前path当中

transform(Matrix matrix) 将path进行matrix变化后,将结果保存到当前path当中

drawPicture(Picture picture, Rect dst) 绘制图片,拉伸以适合dst矩形

drawPicture(Picture picture, RectF dst)
drawPicture(Picture picture)

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

drawText(String text, float x, float y, Paint paint)
drawText(String text, int start, int end, float x, float y, Paint paint)
drawText(char[] text, int index, int count, float x, float y, Paint paint)
drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
  • hOffset参数指定水平偏移 vOffset参数指定垂直偏移
drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, Paint paint)

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

getClipBounds(Rect bounds) 获取边界宽高等数据

getClipBounds()

getDensity() 获取分辨率

getDrawFilter()

getHeight() 返回当前绘图图层的高度

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

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

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

getWidth() 返回当前绘图图层的宽度

isHardwareAccelerated() 指示此Canvas是否使用硬件加速

isOpaque() 如果当前图层绘制的设备不透明

quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type)

quickReject(RectF rect, Canvas.EdgeType type)
quickReject(Path path, Canvas.EdgeType type)

restore() 此调用平衡以前对save()的调用,并用于删除自上次保存调用以来对矩阵/剪辑状态的所有修改

restoreToCount(int saveCount) 弹出任何调用save()

rotate(float degrees, float px, float py) 预旋转指定旋转的当前矩阵

rotate(float degrees)

save(int saveFlags) 基于saveFlags,可以保存当前矩阵并剪切到私有堆栈

save() 将当前矩阵和剪辑保存到私有堆栈

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

saveLayer(RectF bounds, Paint paint, int saveFlags)
saveLayer(RectF bounds, Paint paint)
saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags)
saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)
saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
saveLayerAlpha(float left, float top, float right, float bottom, int alpha)
saveLayerAlpha(RectF bounds, int alpha)

scale(float sx, float sy, float px, float py) 基于坐标(px,py)进行按照(sx,sy)比例进行放缩

scale(float sx, float sy)

setBitmap(Bitmap bitmap) 指定要绘制的画布的位图

setDensity(int density) 指定此Canvas的备份位图的密度。

setDrawFilter(DrawFilter filter)

setMatrix(Matrix matrix) 用指定的矩阵完全替换当前矩阵

skew(float sx, float sy) 指定偏斜的当前矩阵

translate(float dx, float dy) 指定的平移预转换当前矩阵

0 0