a

    athos

    1 year ago
    Hi, is there a way to load and draw an xml shape drawable into a Canvas?
    This is the drawable:
    <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:
    val thumb = painterResource(id = R.drawable.metro_wheel_thumb)
    and
    val thumb = rememberVectorPainter(       ImageVector.vectorResource(R.drawable.metro_wheel_thumb)
        )
    and drawing with:
    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):
    val ctx = LocalContext.current
    val drawable = ContextCompat.getDrawable(ctx, R.drawable.metro_wheel_thumb)
    and
    drawable?.let {
        drawImage(
                it.toBitmap(
                        width = THUMB_SIZE.roundToPx(),
                        height = THUMB_SIZE.roundToPx()
                ).asImageBitmap(),
        )
    }
    But I wonder if there's a more appropriate way
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago