Android 自定义view基础之Canvas类

155 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第8天,点击查看活动详情

重写onDraw()方法 可以获取 canvas对象

/**
 * Implement this to do your drawing.
 *
 * @param canvas the canvas on which the background will be drawn
 */
protected void onDraw(Canvas canvas) {
}

点进父类方法发现这是一个空方法,我们可以不去call它的父类方法

canvas绘制的坐标 是自定义view的坐标,每一个自定义view都会有一个自己画布是矩形的,具体的大小取决于measure过程的测量值

1.Canvas 类的绘制方法

1.1底色绘制

  • drawcolor(Color.BLACK) 在一整个区域涂上指定颜色
  • drawColor(Color.parse("#88880000"); // 半透明红色
    
  • canvas.drawRGB(100, 200, 100);
    canvas.drawARGB(100, 100, 200, 100);
    

1.2 drawCircle 画圆

  • drawCircle(float centerX, float centerY, float radius, Paint paint)

参数名就能知名见意,这里不做解释

1.3 drawRect 画矩形

  • drawRect(float left, float top,float rigtht,float bottom,Paint paint)

    • 重载

    • canvas.drawRect(RectF rect,Paint paint);
      
    • canvas.drawRect(Rect rect, paint);
      

      RectF与Rect都是长方形,只是精度不同 float与int

1.4 drawPoint 画点

  • 单点 drawPoint(float x, float y, Paint paint)

  • 多点

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

    数组中两个点组成一对构成一个坐标

    offset 表示要跳过前面几个数

    count 表示点的数量

1 .5 drawOval 画椭圆

  • rawOval(float left, float top, float right, float bottom, Paint paint)

    • 可以理解为椭圆在一个矩形之中

    重载方法

    • drawOval(RectF rect, Paint paint)

1.6 drawLine 画线

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

    由于直线不是封闭图形,所以 setStyle(style) 对直线没有影响。

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

  • drawLines(float[] pts, Paint paint)

方法解析与画多个点相似

1.7 画圆角矩形

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

rx 与ry 是圆角的横向半径与纵向半径

从圆心到水平方向的最远端的距离就是横向半径,从圆心到垂直方向的最远端的距离就是纵向半径

重载

drawRoundRect(RectF rect, float rx, float ry, Paint paint)

1.8 drawArc画弧

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

    • startAngle 是弧形的起始角度(x 轴的正向,即正右的方向,是 0 度的位置;顺时针为正角度,逆时针为负角度)
    • sweepAngle 是弧形划过的角度;
    • useCenter 表示是否连接到圆心,如果不连接到圆心,就是弧形,如果连接到圆心,就是扇形。

1.9 自定义图形drawPath(Path path ,Paint paint)

1.9.1 直接描述路径 xxxTo() addxxx()

addxxx() 添加子图形

  1. 添加圆addCircle(float x, float y, float radius, Direction dir)

    1. Dirction 描述圆是顺时针还是逆时针

      1. path.Direction.CW
      2. 顺时针 ( CW clockwise) 和逆时针 ( CCW counter-clockwise)
      3. 对于普通情况,这个参数填 CW 还是填 CCW 没有影响。它只是在** 需要填充图形** ( Paint.Style FILL FILL_AND_STROKE ) ,并且图形出现自相交时,用于判断填充范围的。
  2. addOval(float left, float top, float right, float bottom, Direction dir) / addOval(RectF oval, Direction dir) 添加椭圆

addRect(float left, float top, float right, float bottom, Direction dir) / addRect(RectF rect, Direction dir) 添加矩形

addRoundRect(RectF rect, float rx, float ry, Direction dir) / addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Direction dir) / addRoundRect(RectF rect, float[] radii, Direction dir) / addRoundRect(float left, float top, float right, float bottom, float[] radii, Direction dir) 添加圆角矩形

addPath(Path path) 添加另一个 Path

弧线**addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle) / addArc(RectF oval, float startAngle, float sweepAngle)**

> 默认forceMoveTo=true

xxxTo() 画线(直线或曲线)

  1. lineTo(float x, float y) 从当前位置画线到 x,y位置

    • 绝对坐标:原点不变
  2. rlineTo(float x,float y)

    • 相对坐标 :画笔的上一次坐标为原点
  3. quadTo(float x1, float y1, float x2, float y2) / rQuadTo(float dx1, float dy1, float dx2, float dy2) 画二次贝塞尔曲线

  4. cubicTo(float x1, float y1, float x2, float y2, float x3, float y3) / rCubicTo(float x1, float y1, float x2, float y2, float x3, float y3) 画三次贝塞尔曲线

  5. moveTo 与rMoveTo()

    • 移动画笔(不产生痕迹)
  6. arcTo()

    • 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) 画弧形
    • forceMoveTo 参数的意思是,绘制是要「抬一下笔移动过去」,还是「直接拖着笔过去」,区别在于是否留下移动的痕迹。

爱心绘制

path.addArc(200, 200, 400, 400, -225, 225);
path.arcTo(400, 200, 600, 400, -180, 225, false);
path.lineTo(400, 542);

1.9.2 path 方法第二类 :辅助的设置或计算

close() 使图形封闭

连接最初的起点

Path.setFillType(Path.FillType fillType)

  • EVEN_ODD
  • WINDING (默认值)
  • INVERSE_EVEN_ODD
  • INVERSE_WINDING

1.10其他API

  • drawBitmap(Bitmap bitmap,float left,float top,Paint paint) 绘制图片

  • drawText(text,float x,float y,Paint paint ) 绘制文字

    • x y代表起始坐标

      paint.setTextSize(int size); 设置字体大小

参考 rengwuxian.com/ui-1-1/

[官方文档​]  developer.android.google.cn/reference/a…