Is there a way to perform Matrix Scale for Jetpack Compose Image?

Viewed 587

In XML based image, we can have matrix scale type as per https://medium.com/mobile-app-development-publication/android-matrix-scaletype-explained-4501f0796be8, which is a very powerful custom scale type one can perform on an Image.

However, in JetpackCompose image, we no longer have Matix Scale. Instead the contentScale

        Image(imagePicture,
            contentDescription = null,
            modifier = Modifier.fillMaxSize(),
            alignment = alignment.alignment,
            contentScale = scale.scaleType // the scale here
        )

Only have those as listed here https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/ContentScale, i.e. Crop, Fit, FillWidth, FillHeight, FillBounds, Fit, Inside, and None.

How can I achieve Matrix Scale in JetpackCompose?

1 Answers

The behavior you are looking for, is achievable with graphicsLayer:

val imageBitmap = imageResource(id = R.drawable.cover)
Image(imageBitmap, contentDescription = "Test", modifier = Modifier
    .graphicsLayer {
        translationX = 0.4f
        translationY = 0.4f
        rotationY = 53f
        rotationX = 44f
        rotationZ = 23f
        scaleX = 0.4f
        scaleY = 0.5f
    })
Related