https://kotlinlang.org logo
#compose-android
Title
# compose-android
s

Shivam Verma

11/14/2023, 12:11 PM
Hi all 👋 In certain cases, I would like to draw an image on top of a Text such that the image scales to fit the height and width of the Text component. I'm trying the following approach but I cannot find a way for the image to cover the whole Text component. Any pointers would be really helpful. See video in thread 🧵
Copy code
Text(
    text = text,
    color = Color.Transparent,
    modifier = modifier.drawWithContent {
        drawContent()
        drawImage(image = bitmap, colorFilter = ColorFilter.tint(tintColor, BlendMode.SrcIn))
    },
)
e

ephemient

11/14/2023, 12:22 PM
Copy code
drawImage(dstSize = size)
or whatever other adjustments you want to do too preserve aspect ratio
s

Shivam Verma

11/14/2023, 12:23 PM
image_on_text.webm
@ephemient wow! that seems to work perfectly. thanks so much 🙏 Are there any performance improvements I could make ?
e

efemoney

11/14/2023, 1:16 PM
Are there any performance issues you are facing?
s

Shivam Verma

11/14/2023, 2:18 PM
@efemoney not particularly. I noticed another modifier 👉
drawWithCache
and was wondering if there was a way to use that instead for better performance
e

efemoney

11/14/2023, 2:22 PM
For your particular case there is nothing to “cache”. The difference is that the withCache modifier gives you a lambda where you can first allocate (and hence cache) some objects that are then used across different runs of the actual draw operation. So expensive calculations, usually size-based calculations, can be done once and reused and will be rerun when the size changes for instance
gratitude thank you 1