【问题标题】:How to make transparent Color background for camera preview exuding one View?如何使相机预览的透明彩色背景散发出一个视图?
【发布时间】:2021-07-02 06:44:47
【问题描述】:

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

【问题讨论】:

    标签: android xml layout view preview


    【解决方案1】:

    尝试以下操作:

    忽略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>
    

    注意,实心部分设置为透明,只有笔划被赋予纯色。这是你可以实现的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多