How to add redline in camera preview

2019-08-06 09:57发布

问题:

Im trying to customize my camera in my project android scanner, where i can put red line in the middle of my camera and make my background transparent.

mycurrent camera layout:

I want to make it like this:

activity_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1A237E"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context="com.example.administrator.myapplication.CameraTestActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:orientation="horizontal"
        android:weightSum="1">

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#1A237E"
        android:text="Help"
        android:textColor="#ffff"
        android:textSize="15sp"
        android:textStyle="italic" />

    <TextView

        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.07"
        android:text="ASEC MOBILE SCANNER"
        android:textAlignment="center"
        android:textColor="#ffff"
        android:textSize="30sp"
        android:textStyle="bold"></TextView>

    <TextView
        android:id="@+id/scanText"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="0.07"
        android:text="Scanning..."
        android:textAlignment="center"
        android:textColor="#ffff"
        android:textSize="30sp"></TextView>

    <FrameLayout
        android:id="@+id/cameraPreview"
        android:layout_width="294dp"
        android:layout_height="307dp"
        android:layout_gravity="center|clip_vertical|clip_horizontal"
        android:layout_marginHorizontal="200dp"
        android:layout_weight="0.15" />

</LinearLayout>

I tried to duplicate FrameLayout but it doesn't work. Is there any way to do it using FrameLayout that I already have?

回答1:

A simple way by playing with views.

just replace below code with your layout-xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1A237E"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.example.administrator.myapplication.CameraTestActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:orientation="horizontal"
    android:weightSum="1">

</LinearLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="#1A237E"
    android:text="Help"
    android:textColor="#ffff"
    android:textSize="15sp"
    android:textStyle="italic" />

<TextView

    android:id="@+id/textView8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.07"
    android:text="ASEC MOBILE SCANNER"
    android:textAlignment="center"
    android:textColor="#ffff"
    android:textSize="30sp"
    android:textStyle="bold"></TextView>

<TextView
    android:id="@+id/scanText"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_weight="0.07"
    android:text="Scanning..."
    android:textAlignment="center"
    android:textColor="#ffff"
    android:textSize="30sp"></TextView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">

    <FrameLayout
        android:id="@+id/cameraPreview"
        android:layout_width="294dp"
        android:layout_height="307dp" />

    <View
        android:layout_width="294dp"
        android:layout_height="1dp"
        android:layout_centerVertical="true"
        android:background="#ff0000" />

</RelativeLayout>