相机透明矩形框
Rectangular frame with transparency for camera
我正在使用自定义相机模块开发 Android 应用程序。自定义相机模块的主要目的是为相机添加覆盖层 - 它应该类似于外部透明边框(参见第一张图片)。它实际上在所附图片中是模糊的,但我至少需要透明效果,因为在 Android 上实现模糊并非易事。
这里的重点是保证"clear"可见区域对应A4幅面,所以高除以宽应该是√2(本app的主要用途是拍摄A4纸)。
我已经想出了如何实现 "inverted" 效果 - 请参见第二张图片。但是我无法理解如何设置视图以获得所需的效果 - 第一张图片。有人可以分享他们对这个问题的看法吗?这是代码:
camera.xml:
<LinearLayout
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:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/overlay"
android:layout_width="316dp"
android:layout_height="446dp"
android:layout_centerInParent="true"
android:background="@drawable/overlay"
android:duplicateParentState="false" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="@android:color/black">
<Button
android:id="@+id/button_capture"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/capture_button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/button_close"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_centerVertical="true"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_toLeftOf="@+id/button_capture"
android:background="@drawable/close" />
</RelativeLayout>
</LinearLayout>
overlay.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<solid android:color="#88ffffff" />
为了避免棘手的矢量蒙版,只需将 id/overlay 定义为具有 4 个半透明矩形的布局,共享您现在使用的相同可绘制对象:顶部、底部、左侧和右侧。您可以在运行时确定每个矩形的尺寸,但如果您喜欢智力挑战,可以将它们定义为 ConstraintLayout。
我找到了一篇与我所需要的差不多的文章。我可以毫不费力地调整那里的代码以获得预期的效果:https://blog.budiharso.info/2016/01/09/Create-hole-in-android-view/
我正在使用自定义相机模块开发 Android 应用程序。自定义相机模块的主要目的是为相机添加覆盖层 - 它应该类似于外部透明边框(参见第一张图片)。它实际上在所附图片中是模糊的,但我至少需要透明效果,因为在 Android 上实现模糊并非易事。
这里的重点是保证"clear"可见区域对应A4幅面,所以高除以宽应该是√2(本app的主要用途是拍摄A4纸)。
我已经想出了如何实现 "inverted" 效果 - 请参见第二张图片。但是我无法理解如何设置视图以获得所需的效果 - 第一张图片。有人可以分享他们对这个问题的看法吗?这是代码:
camera.xml:
<LinearLayout
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:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/overlay"
android:layout_width="316dp"
android:layout_height="446dp"
android:layout_centerInParent="true"
android:background="@drawable/overlay"
android:duplicateParentState="false" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="@android:color/black">
<Button
android:id="@+id/button_capture"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/capture_button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/button_close"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_centerVertical="true"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_toLeftOf="@+id/button_capture"
android:background="@drawable/close" />
</RelativeLayout>
</LinearLayout>
overlay.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<solid android:color="#88ffffff" />
为了避免棘手的矢量蒙版,只需将 id/overlay 定义为具有 4 个半透明矩形的布局,共享您现在使用的相同可绘制对象:顶部、底部、左侧和右侧。您可以在运行时确定每个矩形的尺寸,但如果您喜欢智力挑战,可以将它们定义为 ConstraintLayout。
我找到了一篇与我所需要的差不多的文章。我可以毫不费力地调整那里的代码以获得预期的效果:https://blog.budiharso.info/2016/01/09/Create-hole-in-android-view/