https://kotlinlang.org logo
a

athos

09/14/2021, 9:31 AM
Hi, is there a way to load and draw an xml shape drawable into a Canvas?
This is the drawable:
Copy code
<shape xmlns:android="<http://schemas.android.com/apk/res/android>"
    android:shape="oval">
  <stroke
      android:width="1dp"
      android:color="#555555" />
  <gradient android:type="radial"
      android:centerX="0.42"
      android:centerY="0.4"
      android:gradientRadius="100%"
      android:startColor="#111111"
      android:centerColor="#111111"
      android:endColor="#444444" />
</shape>
I tried both:
Copy code
val thumb = painterResource(id = R.drawable.metro_wheel_thumb)
and
Copy code
val thumb = rememberVectorPainter(       ImageVector.vectorResource(R.drawable.metro_wheel_thumb)
    )
and drawing with:
Copy code
with(thumb) {
                draw(Size(THUMB_SIZE.toPx(),THUMB_SIZE.toPx()))
            }
in the canvas, but I get exception because a VectorDrawable is expected
Found a way searching in this channel (here):
Copy code
val ctx = LocalContext.current
val drawable = ContextCompat.getDrawable(ctx, R.drawable.metro_wheel_thumb)
and
Copy code
drawable?.let {
    drawImage(
            it.toBitmap(
                    width = THUMB_SIZE.roundToPx(),
                    height = THUMB_SIZE.roundToPx()
            ).asImageBitmap(),
    )
}
But I wonder if there's a more appropriate way
z

Zach Klippenstein (he/him) [MOD]

09/14/2021, 2:32 PM
21 Views