我有一个自定义视图:
class StepView(
context: Context?,
@ColorInt
color: Int,
private val size: Float
) : View(context) {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
init {
paint.color = color
setWillNotDraw(false)
}
fun setColor(@ColorInt color: Int) {
paint.color = color
invalidate()
}
private fun getCenterX() = x + width / 2
private fun getCenterY() = y + height / 2
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.drawCircle(getCenterX(), getCenterY(), size / 2, paint)
}
}
private fun init(stepCount: Int = defaultStepCount, currentStep: Int = defaultCurrentStep) {
orientation = HORIZONTAL
removeAllViews()
for (i in 0 until stepCount) {
createStepView(i)
}
}
private fun createStepView(index: Int) {
val stepView = StepView(context, arrayOf(Color.RED, Color.GREEN, Color.BLUE, Color.BLACK)[index], 20f)
val layoutParams = LayoutParams(20, 20)
addView(stepView, layoutParams)
}
canvas?.drawCircle()
drawRect()
)仅适用于第一个:
第二个和第三个在布局中,但没有绘制(边框来自UiAutomatorViewer;
onDraw
如果我打电话
canvas?.drawColor()