简体   繁体   中英

Android Jetpack Compose (Composable) Disable Hardware Acceleration for particular composable

I have a composable, that is using NativeCanvas together with BlurMaskFilter for applying some custom blur effect. This is working fine for device with with API > 23, and the problem is caused by hardware acceleration for older devices.

Here you can see the blur is working on API 30 (left) and not so much on API 22 (right).

在此处输入图像描述

I know that I can disable it manually for the whole application or activity from the manifest file using android:hardwareAccelerated="false" .

For Views we could disable it for particular views rather than the whole activity using view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

Is there a way we could disable the hardware acceleration for particular Composable and not for the whole application/activity?

Using Interoperability APIs you can embed ComposeView with the needed configuration inside your composable:

@Composable
fun SoftwareLayerComposable(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit,
) {
    AndroidView(
        factory = { context ->
            ComposeView(context).apply {
                setLayerType(View.LAYER_TYPE_SOFTWARE, null)
            }
        },
        update = { composeView ->
            composeView.setContent(content)
        },
        modifier = modifier,
    )
}

Usage:

SoftwareLayerComposable(Modifier) {
    // your view
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM