Rounded corners of image in Jetpack Glance Widget

737 Views Asked by At

Is there any way how to make image corners in Jetpack Glance Widget rounded? I know there is a GlanceModifier method cornerRadius, but this is available only in higher versions (S+).

3

There are 3 best solutions below

0
On

Unfortunately the underlying API to apply the rounded corners is only available in S and there is no easy way to backport that for Glance.

You can either create a drawable with rounded corners in XML or modify at runtime your image and apply the corners there.

0
On

Use: GlanceModifier.cornerRadius(8.dp)

2
On

I am using a Coil to reach it for all version of android

Example (pay attention cornerRadiusPx is zero there)

val model by remember(
        source,
        isRoundedImage,
        borderImageWidth,
        borderImageColor
    ) {
        mutableStateOf(

            ImageRequest.Builder(context).apply {
                data(source)
                if (isRoundedImage) {
                    transformations(
                        BorderedCircleCropTransformation(
                            dpToPx(context, borderImageWidth),
                            borderImageColor.toArgb()
                        )
                    )
                } else {
                    transformations(
                        BorderedRoundedCornersTransformation(
                            dpToPx(context, borderImageWidth),
                            0f,
                            borderImageColor.toArgb(),
                        )
                    )
                }
            }.build()
        )
    }

    AsyncImage(
        modifier = modifier,
        model = model,
        contentDescription = "Avatar",
    )