Search code examples
androidimageandroid-jetpack-composenine-patch

How to use a 9 patch drawable (.9.png) in my screen using Jetpack Compose?


I'm trying to use a .9.png file in Image composable as :

            Image(
            painter = painterResource(id = R.drawable.shadow_faq),
            contentDescription = "Faq card 1",
            modifier = Modifier
                .constrainAs(imgGeneral) {
                    top.linkTo(glImgGeneralTop)
                    bottom.linkTo(glImgBottom)
                    start.linkTo(glImgLeft)
                    end.linkTo(glImgRight)
                    height = Dimension.fillToConstraints
                    width = Dimension.fillToConstraints
                }
        )

But on doing this I get a render problem that says java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG

How do I use a .9.png file with Jetpack Compose?


Solution

  • Update: Starting with 1.0.0-rc02 and accompanist 0.14.0 you can use the coil-compose version:

    Image(
        rememberImagePainter(ContextCompat.getDrawable(context,R.drawable.xxx)),
        contentDescription = "Faq card 1",
    )
    

    Previous deprecated answer:
    You can use the DrawablePainter from Accompanist that returns a Painter which draws an Android Drawable.

    Image(
        rememberDrawablePainter(drawable = ContextCompat.getDrawable(context,R.drawable.xxxx) ),
        contentDescription = "Faq card 1"
    )