安卓自定义view画图:三角形,圆形,方形,扇形,弧线
2020-09-21 本文已影响0人
蓝不蓝编程
效果图

主要代码
/**
* 画矩形
*/
private fun drawRect(canvas: Canvas, isFilled: Boolean = true) {
setPaintStyle(isFilled)
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
}
/**
* 画圆形
*/
private fun drawCircle(canvas: Canvas, isFilled: Boolean = true) {
setPaintStyle(isFilled)
canvas.drawCircle(
width.toFloat() / 2,
height.toFloat() / 2,
min(width, height).toFloat() / 2,
paint
)
}
/**
* 画三角形
*/
private fun drawTriangle(canvas: Canvas, isFilled: Boolean = true) {
setPaintStyle(isFilled)
path.moveTo(width.toFloat() / 2, 0f)
path.lineTo(width.toFloat(), height.toFloat())
path.lineTo(0f, height.toFloat())
path.close()
canvas.drawPath(path, paint)
}
/**
* 画扇形
*/
private fun drawSector(canvas: Canvas, isFilled: Boolean = true) {
setPaintStyle(isFilled)
val rectF = RectF(0f, 0f, width.toFloat(), height.toFloat())
canvas.drawArc(rectF, 30f, 120f, true, paint)
}
/**
* 画圆弧
*/
private fun drawArc(canvas: Canvas) {
setPaintStyle(false)
val rectF = RectF(0f, 0f, width.toFloat() - strokeWidth, height.toFloat() - strokeWidth)
canvas.drawArc(rectF, 30f, 120f, false, paint)
}