首页 > 解决方案 > Android Studio Camera2预览画面无法添加UI

问题描述

Android Studio Camera2 预览画面无法添加 UI。我想知道我是否可以在相机预览屏幕上添加其他 UI?(标题栏、按钮、文本等) 我尝试在 XML 设置中添加上述 UI 元素,但在执行过程中仍然没有响应。

以下是我的activity_main.xml代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="657dp"
        android:background="#000"
        android:orientation="vertical"
        tools:context=".MainActivity">
    </FrameLayout>

</RelativeLayout>

以下是我的fragment_camera.xml代码:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".CameraFragment">

    <com.lightweh.camera2preview.AutoFitTextureView
        android:id="@+id/textureView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</FrameLayout >

其他代码可以参考: https ://github.com/xGonZh10n/Camera2Preview

标签: javaxmlandroid-studiolayoutandroid-camera2

解决方案


有什么错误吗?你能告诉我们错误日志吗?检查您在 Android 清单中的权限。也许你错过了这条线。

<uses-permission android:name="android.permission.CAMERA" />

我有一个建议给你。Camera2API 比其他自定义库更容易集成。参考:https ://developer.android.com/guide/topics/media/camera


推荐阅读