在包含自定义 Shape
的 ShapeDrawable
上调用 setTint()
似乎对基础形状的颜色没有影响。
CustomShape.kt
class CustomShape : Shape() {
private val paint = Paint()
private val path = Path()
init {
paint.isAntiAlias = true
}
override fun onResize(width: Float, height: Float) {
// update path
}
override fun draw(canvas: Canvas, paint: Paint) {
canvas.drawPath(path, this.paint)
}
}
用法
val shape = CustomShape()
val drawable = ShapeDrawable(shape)
drawable.setTint(Color.RED) // not working
someView.background = drawable
最佳答案
解决方案
使用 draw()
提供的 Paint
对象,它已经应用了反别名标志,并且会尊重您在 ShapeDrawable< 上调用的任何方法
.
问题是我正在创建和使用一个新的 Paint
对象,而不是使用 draw()
中提供的对象。这样做的原因是我需要打开抗锯齿,我想避免在 draw
方法中这样做。
此外,我最初是直接在 CustomShape
中给 Paint
对象一个颜色,然后才意识到让 ShapeDrawable
可能更好/必要处理那个。
关于android - 包含自定义形状的 ShapeDrawable 的 setTint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684972/