为什么 CameraXBasic 项目不将两个布局文件合并为一个?
Why doesn't CameraXBasic project merge two layout files into one?
以下代码来自CameraXBasic project Github
CameraFragment.kt先加载fragment_camera.xml,再加载camera_ui_container.xml.
我觉得很奇怪 CameraXBasic 项目没有合并两个布局文件 fragment_camera.xml 和 camera_ui_container.xml合二为一,CameraFragment.kt只加载合并后的布局文件。
CameraFragment.kt
class CameraFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_camera, container, false)
private fun updateCameraUi() {
// Remove previous UI if any
container.findViewById<ConstraintLayout>(R.id.camera_ui_container)?.let {
container.removeView(it)
}
// Inflate a new view containing all UI for controlling the camera
val controls = View.inflate(requireContext(), R.layout.camera_ui_container, container)
..
}
}
fragment_camera.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
camera_ui_container.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_ui_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@+id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginLeft="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
<ImageButton
android:id="@+id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginBottom="@dimen/shutter_button_margin"
android:scaleType="fitCenter"
android:background="@drawable/ic_shutter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/capture_button_alt" />
<ImageButton
android:id="@+id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginRight="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitCenter"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
添加内容
我测试了合并后的布局(Normal 和 landsacpe),效果很好,我觉得原版和我的速度差不多。
CameraFragment.kt(新)
private fun updateCameraUi() {
// Remove previous UI if any
// container.findViewById<ConstraintLayout>(R.id.camera_ui_container)?.let {
// container.removeView(it)
//}
// Inflate a new view containing all UI for controlling the camera
//val controls = View.inflate(requireContext(), R.layout.camera_ui_container, container)
// Listener for button used to capture photo
...
}
fragment_camera.xml (新)
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@+id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginLeft="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
<ImageButton
android:id="@+id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginBottom="@dimen/shutter_button_margin"
android:scaleType="fitCenter"
android:background="@drawable/ic_shutter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/capture_button_alt" />
<ImageButton
android:id="@+id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginRight="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitCenter"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_camera.xml (新地)
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginRight="@dimen/margin_xlarge"
android:layout_marginBottom="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitXY"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
<ImageButton
android:id="@id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginRight="@dimen/shutter_button_margin"
android:background="@drawable/ic_shutter"
android:contentDescription="@string/capture_button_alt"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginRight="@dimen/margin_xlarge"
android:layout_marginTop="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitXY"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
他们的想法是每次配置更改时重新创建 UI,而 TextureView 仍然存在。
您是否注意到有两个 XML camera_ui_container.xml 文件,一个用于纵向,一个用于横向,以实现不同的 UI 旋转。和行
// Remove previous UI if any
container.findViewById(R.id.camera_ui_container)?.let
{
container.removeView(it)
}
实际上是在做相机的娱乐部分UI @Alex Cohn 是对的,在同一点上。
如果您只处理单一方向,则可以使用单一布局。
以下代码来自CameraXBasic project Github
CameraFragment.kt先加载fragment_camera.xml,再加载camera_ui_container.xml.
我觉得很奇怪 CameraXBasic 项目没有合并两个布局文件 fragment_camera.xml 和 camera_ui_container.xml合二为一,CameraFragment.kt只加载合并后的布局文件。
CameraFragment.kt
class CameraFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_camera, container, false)
private fun updateCameraUi() {
// Remove previous UI if any
container.findViewById<ConstraintLayout>(R.id.camera_ui_container)?.let {
container.removeView(it)
}
// Inflate a new view containing all UI for controlling the camera
val controls = View.inflate(requireContext(), R.layout.camera_ui_container, container)
..
}
}
fragment_camera.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
camera_ui_container.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_ui_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@+id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginLeft="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
<ImageButton
android:id="@+id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginBottom="@dimen/shutter_button_margin"
android:scaleType="fitCenter"
android:background="@drawable/ic_shutter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/capture_button_alt" />
<ImageButton
android:id="@+id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginRight="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitCenter"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
添加内容
我测试了合并后的布局(Normal 和 landsacpe),效果很好,我觉得原版和我的速度差不多。
CameraFragment.kt(新)
private fun updateCameraUi() {
// Remove previous UI if any
// container.findViewById<ConstraintLayout>(R.id.camera_ui_container)?.let {
// container.removeView(it)
//}
// Inflate a new view containing all UI for controlling the camera
//val controls = View.inflate(requireContext(), R.layout.camera_ui_container, container)
// Listener for button used to capture photo
...
}
fragment_camera.xml (新)
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@+id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginLeft="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
<ImageButton
android:id="@+id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginBottom="@dimen/shutter_button_margin"
android:scaleType="fitCenter"
android:background="@drawable/ic_shutter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/capture_button_alt" />
<ImageButton
android:id="@+id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginBottom="@dimen/margin_xlarge"
android:layout_marginRight="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitCenter"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_camera.xml (新地)
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Camera control and gallery buttons -->
<ImageButton
android:id="@id/camera_switch_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginRight="@dimen/margin_xlarge"
android:layout_marginBottom="@dimen/margin_small"
android:padding="@dimen/spacing_small"
android:scaleType="fitXY"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/gallery_button_alt" />
<ImageButton
android:id="@id/camera_capture_button"
android:layout_width="@dimen/round_button_large"
android:layout_height="@dimen/round_button_large"
android:layout_marginRight="@dimen/shutter_button_margin"
android:background="@drawable/ic_shutter"
android:contentDescription="@string/capture_button_alt"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@id/photo_view_button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginRight="@dimen/margin_xlarge"
android:layout_marginTop="@dimen/margin_small"
android:padding="@dimen/spacing_large"
android:scaleType="fitXY"
android:background="@drawable/ic_outer_circle"
app:srcCompat="@drawable/ic_photo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/switch_camera_button_alt" />
</androidx.constraintlayout.widget.ConstraintLayout>
他们的想法是每次配置更改时重新创建 UI,而 TextureView 仍然存在。
您是否注意到有两个 XML camera_ui_container.xml 文件,一个用于纵向,一个用于横向,以实现不同的 UI 旋转。和行
// Remove previous UI if any
container.findViewById(R.id.camera_ui_container)?.let { container.removeView(it) }
实际上是在做相机的娱乐部分UI @Alex Cohn 是对的,在同一点上。
如果您只处理单一方向,则可以使用单一布局。