如何为相机预览制作透明的彩色背景,散发出一个视图?

问题描述

我想为我的相机预览制作一个类似扫描仪的视图我在相机预览的中心创建了一个矩形视图的视图,现在我想为相机预览添加一个透明的颜色背景,但想要制作一个矩形清晰视图。我尝试过设置 alpha 和背景颜色,但在我看来它不起作用。

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



<ImageView
    android:id="@+id/switchcamera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="camera_change"
    android:onClick="onClick"
    android:src="@drawable/frontcamera_foreground"
    app:layout_constraintEnd_toEndOf="@+id/preview"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/text_animator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:text="1"
    android:textColor="@color/white"
    android:textSize="70sp"
    android:textStyle="bold"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintGuide_percent="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<View
    android:id="@+id/viewRectangle"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/border"
    android:visibility="gone"
    android:alpha="1"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这里的FrameLayout是用于使用自定义SurfaceView的相机预览 View 是我的 RectangleView。

解决方法

尝试以下操作:

忽略 com.otaliastudios.cameraview.CameraView 这只是我导入的一个相机库。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.otaliastudios.cameraview.CameraView
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:keepScreenOn="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/viewRectangle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/border"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

现在重要的部分是如何构造 border.xml drawable:

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

    <solid android:color="@android:color/transparent"/>
    <stroke android:color="#a00" android:width="1dp"/>
</shape>

请注意,纯色部分设置为透明,只有笔触为纯色。这是您可以实现的。