gpt4 book ai didi

android - 为什么 View.display 返回 null?

转载 作者:行者123 更新时间:2023-12-02 13:35:59 25 4
gpt4 key购买 nike

我正在尝试使用本教程实现 CameraX:https://codelabs.developers.google.com/codelabs/camerax-getting-started/#5我的应用程序有一个 Activity 、导航主机和两个 fragment 。我也在我的 fragment 上使用数据绑定(bind)。当我尝试进行显示旋转时,出现错误。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="viewModel"
type="com.ayonix.faceticket.camerapreview.CameraPreviewViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".camerapreview.CameraPreviewFragment">

<TextureView
android:id="@+id/camera_preview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="9:16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是我的 fragment 代码:

class CameraPreviewFragment : Fragment() {

private lateinit var binding: CameraPreviewFragmentBinding
private lateinit var viewModel: CameraPreviewViewModel

private val previewConfig = PreviewConfig.Builder().build()
private val preview = Preview(previewConfig)

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = CameraPreviewFragmentBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this

val viewModelFactory = CameraPreviewViewModelFactory((activity as MainActivity).faceIDUtils)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(CameraPreviewViewModel::class.java)
binding.viewModel = viewModel

startCamera()

return binding.root
}

private fun startCamera() {
preview.setOnPreviewOutputUpdateListener {
val parent = binding.cameraPreview.parent as ViewGroup
parent.removeView(binding.cameraPreview)
parent.addView(binding.cameraPreview, 0)

binding.cameraPreview.surfaceTexture = it.surfaceTexture
updateTransform()
}

CameraX.bindToLifecycle(this, preview, viewModel.analyzerUseCase)
}

private fun updateTransform() {
val matrix = Matrix()

val centerX = binding.cameraPreview.width / 2f
val centerY = binding.cameraPreview.height / 2f

val rotationDegrees = when(binding.cameraPreview.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)

binding.cameraPreview.setTransform(matrix)
}

}

我尝试过像 binding.root.display.rotation 这样的旋转。但它也给出了同样的错误。

这是错误:java.lang.IllegalStateException: binding.cameraPreview.display must not be null

最佳答案

3个月后我找到了原因。必须在 onViewCreated 中调用 startCamera()。你不应该在 onCreateView() 中做任何事情,而是扩大布局。固定代码是这样的:

class CameraPreviewFragment : Fragment() {

private lateinit var binding: CameraPreviewFragmentBinding
private lateinit var viewModel: CameraPreviewViewModel

private val previewConfig = PreviewConfig.Builder().build()
private val preview = Preview(previewConfig)

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = CameraPreviewFragmentBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val viewModelFactory = CameraPreviewViewModelFactory((activity as MainActivity).faceIDUtils)
viewModel =
ViewModelProviders.of(this, viewModelFactory).get(CameraPreviewViewModel::class.java)
binding.viewModel = viewModel

startCamera()
}

private fun startCamera() {
preview.setOnPreviewOutputUpdateListener {
val parent = binding.cameraPreview.parent as ViewGroup
parent.removeView(binding.cameraPreview)
parent.addView(binding.cameraPreview, 0)

binding.cameraPreview.surfaceTexture = it.surfaceTexture
updateTransform()
}

CameraX.bindToLifecycle(this, preview, viewModel.analyzerUseCase)
}

private fun updateTransform() {
val matrix = Matrix()

val centerX = binding.cameraPreview.width / 2f
val centerY = binding.cameraPreview.height / 2f

val rotationDegrees = when (binding.cameraPreview.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)

binding.cameraPreview.setTransform(matrix)
}
}

关于android - 为什么 View.display 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57244526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com