androidandroid-jetpack-compose

Jetpack Compose draw on image with Painter


With painter it's possible to draw on ImageBitmap with the snippet

   val imageBitmap: ImageBitmap = imageResource(id = R.drawable.landscape3)

    val customPainter = remember {
        object : Painter() {

            override val intrinsicSize: Size
                get() = Size(imageBitmap.width.toFloat(), imageBitmap.height.toFloat())

            override fun DrawScope.onDraw() {
                drawImage(imageBitmap)
                drawLine(
                    color = Color.Red,
                    start = Offset(0f, 0f),
                    end = Offset(imageBitmap.width.toFloat(), imageBitmap.height.toFloat()),
                    strokeWidth = 5f
                )
            }
        }
    }
    Image(painter = customPainter, contentDescription = null)

loadFontResource is deprecated. Use fontResource instead. imageResource, loadImageResource, vectorResource, and loadVectorResource are deprecated. Use painterResource instead. (I6b809)

with alpha12 imageResource is deprecated. painter's drawImage(imageBitmap) function that draws image has no replacement or another function other than the one takes imageBitmap as parameter?

What's the point of ImageBitmap as of alpha12 since there is no not deprecated function to create it with a resource, and function to get ImageBitmap from Painter does not exist.


Solution

  • starting from Compose UI 1.0.0-beta01

    imageResource and vectorResource are now extension functions on ImageBitmap and ImageVector companions respectively. load{Image,Vector,Font}Resource functions have been deleted. (I89130)

    import androidx.compose.ui.res.imageResource
    // ...
    val imageBitmap: ImageBitmap = ImageBitmap.imageResource(R.drawable.landscape3)
    

    painterResource under the hood calls imageFromResource, so we can use it too:

      val imageBitmap: ImageBitmap = imageFromResource(
        LocalContext.current.resources,
        R.drawable.landscape3
      )