Android绘图篇之graphics类详解(四)

447 阅读4分钟

Paint

  • getTextBounds(text: String, start: Int, end: Int, bounds: Rect):获取指定文本的最小矩形边框bounds
val paint = Paint().apply {
    // 抗锯齿
    isAntiAlias = true
    
    // 防抖动
    isDither = true
    
    // bitmap抗锯齿
    isFilterBitmap = true
}

Canvas

  • translate(x: Float, y: Float):平移,x,y表示对应坐标的偏移量
  • scale(sx: Float, sy: Float):缩放,sx,sy表示对应坐标的缩放值
  • scale(sx: Float, sy: Float, px: Float, py: Float):缩放,sx,sy表示对应坐标的缩放值
# px, py有两种理解
1. 缩放中心点,以(100, 100)为中心点,(x, y)分别缩放0.5
canvas.scale(0.5f, 0.5f, 100f, 100f)

2. 先平移,再缩放,最后平移回原位置
canvas.scale(0.5f, 0.5f, 100f, 100f)
=>
canvas.translate(100f, 100f)
canvas.scale(0.5f, 0.5f)
canvas.translate(-100f, -100f)
  • rotate(degrees: Float):旋转,旋转角度正为顺时针,负为逆时针
  • rotate(degrees: Float, px: Float, py: Float):旋转,(px, py)为旋转中心点
  • skew(sx: Float, sy: Float):倾斜,值为tan角度值,例传入1(tan 45°)表示倾斜45°
canvas.skew(1f, 0f):在x方向倾斜45°,表示y轴逆时针旋转45°
canvas.skew(0f, 1f):在y方向倾斜45°,表示x轴顺时针旋转45°
  • clipRect(rect: Rect):在裁剪区绘制,超出裁剪区不绘制
  • clipOutRect(rect: Rect):在裁剪区外绘制,裁剪区内不绘制
  • setMatrix(matrix: Matrix):使用矩阵实现平移、缩放、旋转、倾斜等效果
// Canvas抗锯齿
val paintFlagsDrawFilter = PaintFlagsDrawFilter(
    0,
    Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG
)
canvas.drawFilter = paintFlagsDrawFilter

Point

  • offset(dx: Int, dy: Int):偏移x、y值, dx与 dy的正负代表偏移的方向
  • negate():x、y取反(相当于以原点为中心,旋转了180°+k×360°,k为整数)

PointF

  • length():到原点的距离

Rect

  • intersect(left: Int, top: Int, right: Int, bottom: Int): Boolean:两矩形交集,并将结果保存至当前矩形,返回值是否相交
  • offsetTo(newLeft: Int, newTop: Int):偏移到指定左上角,宽高不变
  • union(r: Rect):更新矩形区域,参数为空不处理
  • union(x: Float, y: Float):更新矩形区域,若x,y在区域内则不变,若x,y在区域外则扩充区域
  • inset(dx: Float, dy: Float):若dx,dy为正则缩小区域边界,若dx,dy为负则扩充区域边界

RectF

  • intersect(rectF: RestF):两矩形是否相交
  • intersect(left: Int, top: Int, right: Int, bottom: Int):两矩形是否相交
  • roundOut(rect: Rect):RectF区域四舍五入至Rect
val rectF = RectF(0f, 0f, 10.5f, 30f)
val rect = Rect()
rectF.roundOut(rect)
println($rect) // rect:(0f, 0f, 11f, 30f)

Path

PathMeasure

  • getSegment(start: Float, stop: Float, dst: Path, startWithMoveTo: Boolean): Boolean:截取Path片段。(start, stop)取值在0~path长度之间;截取的path片段存在dst中;startWithMoveTo表示新path片段是否执行moveTo至截取第一个点
  • getPosTan(distance: Float, pos: FloatArray, tan: FloatArray): Boolean:获取距离起点指定长度点的坐标和角度。distance取值在0~path长度之间;pos长度为2,记录该点坐标;tan长度为2,记录该点正切值,tan[0]表示该点对应角sin值,对应x坐标,tan[1]表示该点对应角cos值,对应y坐标。可使用tan计算该点切线与x轴正方向角度val degree = (Math.atan2(tan[0].toDouble(), tan[1].toDouble()) * 180 / Math.PI).toFloat()

Matrix

  • postConcat(other: Matrix):使用指定的矩阵对矩阵计算得到新矩阵。M = 其他 * M
  • mapPoints(pts: FloatArray)
  • mapPoints(dst: FloatArray, src: FloatArray)
  • mapPoints(dst: FloatArray, dstIndex: Int, src: FloatArray, srcIndex: Int, count: Int)
# mapPoints(pts: FloatArray)
// 初始点:(0, 0)、(100, 150)、(200, 300)
val pts = floatArrayOf(0f, 0f, 100f, 150f, 200f, 300f)
val matrix = Matrix()
matrix.setScale(0.5f, 1f)
matrix.mapPoints(pts)

// x缩为原值0.5
// 计算后点pts:(0, 0)、(50, 150)、(100, 300)

# mapPoints(dst: FloatArray, src: FloatArray)
// 原值不变,结果保存至dst

# mapPoints(dst: FloatArray, dstIndex: Int, src: FloatArray, srcIndex: Int, count: Int)
// 原值取指定点计算后存入dst指定位置
  • mapRect(rect: Rect)
  • mapRect(dst: Rect, src: Rect)
# mapRect(rect: Rect)
val rect = Rect(50, 0, 100, 50) // 初始点:(50, 0, 100, 50)
val matrix = Matrix()
matrix.setScale(2f, 3f) // x -> 2倍,y -> 3倍
matrix.mapRect(rect) // 计算后点:(100, 0, 200, 150)

# mapRect(dst: Rect, src: Rect)
val src = Rect(50, 0, 100, 50) // 初始src:(50, 0, 100, 50)
val dst = Rect()
val matrix = Matrix()
matrix.setScale(2f, 3f) // x -> 2倍,y -> 3倍
matrix.mapRect(dst, rect) // 计算后dst:(100, 0, 200, 150),src不变