Calling setTint()
on a ShapeDrawable
containing a custom Shape
appears to have no effect on the color of the underlying shape.
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)
}
}
Usage
val shape = CustomShape()
val drawable = ShapeDrawable(shape)
drawable.setTint(Color.RED) // not working
someView.background = drawable
Solution
Use the Paint
object supplied by draw()
, which already has the anti alias flag applied, and will respect any methods you call on the ShapeDrawable
.
The problem is that I was creating and using a new Paint
object rather than using the one supplied in draw()
. The rational for this was that I needed to turn on anti-aliasing, and I wanted to avoid doing so in the draw
method.
Additionally, I was originally giving the Paint
object a color directly in CustomShape
before realizing it might be better/necessary to let the ShapeDrawable
handle that.