如何使透明的彩色背景相机预览散发一个视图



我想为我的相机预览制作一个类似扫描仪的视图我已经创建了一个在相机预览中心有矩形视图的视图,现在我想为相机预览添加一个透明的彩色背景,但想制作一个矩形清晰的视图。我尝试过设置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预览相机视图是我的矩形视图。

尝试以下操作:

忽略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可绘制:

<?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>

请注意,实体部分已设置为透明,并且只有笔划具有实体颜色。这是你可以实现的。

最新更新