Android 如何为一个视图的摄影机预览创建透明的彩色背景?
我想为我的相机预览创建一个类似扫描仪的视图我已经创建了一个视图,其中一个矩形视图位于相机预览的中心。现在我想为相机预览添加一个透明的颜色背景,但我想创建一个清晰的矩形视图。我尝试过设置alpha和背景色,但在我的视图中不起作用Android 如何为一个视图的摄影机预览创建透明的彩色背景?,android,xml,layout,view,preview,Android,Xml,Layout,View,Preview,我想为我的相机预览创建一个类似扫描仪的视图我已经创建了一个视图,其中一个矩形视图位于相机预览的中心。现在我想为相机预览添加一个透明的颜色背景,但我想创建一个清晰的矩形视图。我尝试过设置alpha和背景色,但在我的视图中不起作用 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="htt
<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>
请注意,实体部分设置为透明,并且只有笔划被指定为实体颜色。这是您可以实现的
<?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>