Maarten
Maarten

Reputation: 7338

Do not clip bounds of AndroidView in Compose

From AndroidView documentation:

AndroidView will clip its content to the layout bounds, as being clipped is a common assumption made by Views - keeping clipping disabled might lead to unexpected drawing behavior. Note this deviates from Compose's practice of keeping clipping opt-in, disabled by default.

This seems to suggest that there is a way to turn clipping off, but I can't manage to do so.

I've tried:

Is it possible to turn off clipping?

Upvotes: 4

Views: 4467

Answers (2)

Jason Hong
Jason Hong

Reputation: 1

use this method setClipToOutline,according to Android documentation

AndroidView will not clip its content to the layout bounds. Use View.setClipToOutline on the child View to clip the contents, if desired. Developers will likely want to do this with all subclasses of SurfaceView to keep its contents contained.

Upvotes: 0

Phil Dukhov
Phil Dukhov

Reputation: 88142

It's a known feature request, here's a workaround until it's implemented:

@Composable
fun <T : View> AndroidView(
    clipToBounds: Boolean,
    factory: (Context) -> T,
    modifier: Modifier = Modifier,
    update: (T) -> Unit = NoOpUpdate,
) {
    androidx.compose.ui.viewinterop.AndroidView(
        factory = factory,
        modifier = modifier,
        update = if (clipToBounds) {
            update
        } else {
            {
                (it.parent as? ViewGroup)?.clipChildren = false
                update(it)
            }
        }
    )
}

Upvotes: 6

Related Questions