https://kotlinlang.org logo
#compose
Title
# compose
s

Stylianos Gakis

10/20/2023, 3:55 PM
Using an API which requires
placeholder: Painter? = null,
to show on the screen, I am trying to figure out a way to provide a
Painter
which paints a solid color but with a specific shape (with some rounded corners etc.) but I can not figure out the right API to use here and I am pretty sure there must be something I can use. Any pointers?
f

Francesc

10/20/2023, 3:59 PM
there is a
ColorPainter
that paints a solid color, an option would be to clone that and tweak it to add rounded corners
thank you color 1
s

Stylianos Gakis

10/20/2023, 4:02 PM
Yep that is exactly what I am trying out right now 😄
Copy pasting ColorPainter and replacing onDraw with
Copy code
override fun DrawScope.onDraw() {
  val outline = shape.createOutline(size, layoutDirection, this)
  when (outline) {
    is Outline.Generic -> {
      drawPath(
        outline.path,
        color = color,
        alpha = alpha,
        colorFilter = colorFilter,
      )
    }
    is Outline.Rectangle -> {
      drawRect(color = color, alpha = alpha, colorFilter = colorFilter)
    }
    is Outline.Rounded -> {
      // drawCircle maybe not what I want here exactly, but good enough for me for now
      drawCircle(color = color, alpha = alpha, colorFilter = colorFilter)
    }
  }
}
Seems to work perfectly fine actually. In my case I only needed the
Outline.Generic
case, since I want to use this squircle shape here, but I added the others just in case.
And the entire class along with a
remember*
companion function just works for me 👍 If there is some built-in way to do this I would love to know about it still, but for now this works well for me
👍 1
3 Views