简体   繁体   中英

How to make Button on top of Surfaceview in android

I am creating a QR code scanner and facing an issue where my button is blurr because of surface view overlay. i want my button on top of surface-view. any way of doing it?

在此处输入图像描述

my layout

<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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/preview_view"
        app:layout_constraintEnd_toEndOf="@id/preview_view"
        app:layout_constraintStart_toStartOf="@id/preview_view"
        app:layout_constraintTop_toTopOf="@id/preview_view" >
    </SurfaceView>

    <androidx.camera.view.PreviewView
        android:id="@+id/preview_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >


    </androidx.camera.view.PreviewView>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="50dp"
        android:translationZ="200dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/ic_menu_manage" />
</androidx.constraintlayout.widget.ConstraintLayout>

Wrap the overlay in separate layout, eg ConstraintLayout. Below a working code of mine:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <FrameLayout
        android:id="@+id/scanViewContainer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<ImageButton
    android:id="@+id/scannerCloseBtn"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="24dp"
    android:background="@color/transparent"
    android:padding="8dp"
    android:scaleType="fitCenter"
    android:src="@android:drawable/ic_menu_close_clear_cancel"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/scannerFlashIcon"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="24dp"
    android:padding="8dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_flash"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.widget.Button
    android:id="@+id/scannerFlashOffButton"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="24dp"
    android:background="@color/transparent"
    android:text="@string/flash_off"
    android:textAllCaps="false"
    android:textColor="@color/white"
    app:layout_constraintStart_toEndOf="@+id/scannerFlashIcon"
    app:layout_constraintTop_toTopOf="parent" />

<android.widget.Button
    android:id="@+id/scannerFlashOnButton"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginStart="1dp"
    android:layout_marginTop="24dp"
    android:background="@color/transparent"
    android:text="@string/flash_on"
    android:textAllCaps="false"
    android:textColor="@color/white"
    app:layout_constraintStart_toEndOf="@+id/scannerFlashOffButton"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM