当我在BottomNavigationView中切换片段时,为什么我的CameraPreview中的SurfaceTexture被破坏了?

时间:2019-04-09 07:10:47

标签: android kotlin android-camera2 textureview

我尝试在TextureView中使用相机预览,这时启动应用程序一切正常。

但是当我想将CameraPreview片段放入BottomNavigationView时出现问题。应用程序启动时一切正常,但是如果我从BottomNavigationView切换片段,则调用SurfaceTextureListener中的onSurfaceTextureDestroyed()方法(意味着我的surfaceTexture被销毁),并且我的片段为空白(没有相机预览)。 在日志中,mTextureView不可用,因此在onResume方法的条件下,将重新分配侦听器,但永远不会调用onSurfaceTextureAvailable()方法。

当我将手机锁定在CameraPreview Fragment上时,不会调用destroy方法,并且在解锁手机时可以使用相机预览,解锁手机并更改BottomNavigationView中显示的片段有什么区别,以及如何解决我的问题?

我的代码基于Google Camera2API示例。

mtextureListener


private val mTextureListener = object : TextureView.SurfaceTextureListener
{
    @SuppressLint("MissingPermission")
    override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) { checkCameraPermission(width, height) }
    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) = transformImage(width, height)
    override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) = Unit
    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean
    {
        // Called on fragment switch in bottomnavView but not called on phone locked... why ? and how to fix ?
        Log.e(TAG, "Surface texture destroyed"); return true 
    }
}

onResume纹理初始化


if (mTextureView.isAvailable)
{
    this.checkCameraPermission(mTextureView.width, mTextureView.height)
} else
{
    this.mTextureView.surfaceTextureListener = mTextureListener
}

实际:在BottomNavView中打开cameraPreview时不可用

预期:每次从BottomNavView切换时,我都想看到cameraPreview

0 个答案:

没有答案