新版本Jetpack Compose AndroidView在低版本Android(例如Android 7 android Android 10)中工作错误

问题描述 投票:0回答:3

解决方案:将SurfaceView更改为TextureView,虽然我不知道为什么,但谢谢@Halifax

关于AndroidView bug 这里的布局 代码:

@RequiresApi(Build.VERSION_CODES.M)
@Composable
fun IjkPlayer(player: IjkMediaPlayer, activityVar: ActivityVar) {
    // https://juejin.cn/post/7034363130121551903
    AndroidView(
        factory = { context ->
            SwithunLog.d("AndroidView # factory")
            val surfaceView = SurfaceView(context)
            surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
                override fun surfaceCreated(holder: SurfaceHolder) {
                    // surfaceView在activity Stop时会destroy,重新切到前台会重新走create,这里要重新setDisplay
                    // 否则会黑屏但是有声音 https://github.com/Bilibili/ijkplayer/issues/2666#issuecomment-800083756
                    player.setDisplay(holder)
                    player.start()
                }

                override fun surfaceChanged(
                    holder: SurfaceHolder,
                    format: Int,
                    width: Int,
                    height: Int,
                ) {
                }

                override fun surfaceDestroyed(holder: SurfaceHolder) {
                    player.pause()
                }

            })

            activityVar.mySurfaceView = surfaceView

            surfaceView
        },
        modifier = Modifier.width(100.dp).height(100.dp),
        update = {
            SwithunLog.d("AndroidView # update")
        })
}

它可以在Android12上正常运行 在此输入图片描述

但在 Android 7 和 Android 10 上 在此输入图片描述 它工作错误,只有 AndroidView 一切都是黑色的,但左侧的按钮仍然可以单击,只是看不到 「布局检查器显示的内容与 Android 7,10,12 上的第一个选择器相同」


这是在我更新一些有关 compose 的库之后发生的,在此之前,所有设备都工作正常

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation "androidx.compose.ui:ui:1.3.3"
    implementation 'androidx.compose.material3:material3:1.0.1'
    implementation "androidx.compose.ui:ui-tooling-preview:1.3.3"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'
    implementation 'androidx.activity:activity-compose:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.3"
    debugImplementation "androidx.compose.ui:ui-tooling:1.3.3"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.3.3"

我在互联网上搜索,Android我已经尝试过

surfaceView.post { // setlayoutParam width and height here } 

android-jetpack-compose android-jetpack
3个回答
1
投票

如果您想继续使用

SurfaceView
而不是切换到
TextureView
,您可以使用
Modifier.clipToBounds()


0
投票

请尝试设置您的

SurfaceView
宽度和高度,修改
LayoutParams

现在需要解决的是覆盖左侧按钮区域的内容。


0
投票

使用

surfaceView.setZOrderOnTop(true)

© www.soinside.com 2019 - 2024. All rights reserved.