简体   繁体   English

相机预览Android中的透明图像

[英]Transparent Image in camera preview android

i need to set a transparent image in the camera preview, and i need to do zoomin and zoomout of it. 我需要在相机预览中设置透明图像,并且需要对其进行放大和缩小。

I have the camera preview, and in the layout on top i put a imageview with the image. 我有相机预览,并在顶部的布局中放置了一个带有图像的imageview。 This image its only a border, the center its transparent, but i cant see the camera preview, i only view the image with a white background. 该图像仅是一个边框,其中心是透明的,但是我看不到相机预览,我只使用白色背景查看图像。

This is my XML: 这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/imageView"
      android:layout_gravity="left|top" android:scaleType="fitXY"
      android:src="@drawable/red"/>
  <FrameLayout
      android:id="@+id/camera_preview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      >
  </FrameLayout>
</LinearLayout>

Why put white background if the image its transparent? 如果图像透明,为什么要放白色背景?

Thanks 谢谢

You need to use RelativeLayout to set ImageView over camera preview. 您需要使用RelativeLayout在摄像机预览上设置ImageView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <FrameLayout
      android:id="@+id/camera_preview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/imageView"
      android:scaleType="fitXY"
      android:src="@drawable/red"/>
</RelativeLayout >

The problem has nothing to do with a camera preview, it's simply down to your choice of ViewGroup. 该问题与摄像机预览无关,这完全取决于您选择的ViewGroup。

A LinearLayout arranges its children along side each other—not on top as you're intending. LinearLayout的子元素彼此并排放置,而不是按照您的意图排列在顶部。 In your layout, your LinearLayout is consuming all visible space in your fragment or activity as its width and height are set to match_parent . 在布局中,LinearLayout占用了片段或活动中的所有可见空间,因为其宽度和高度设置为match_parent Likewise, your ImageView is doing the same, and consuming all of the LinearLayout's visible space. 同样,您的ImageView也是这样做,并占用了LinearLayout的所有可见空间。 Subsequently, the FrameLayout no longer fits and disappears somewhere off-screen. 随后,FrameLayout不再适合并且在屏幕外消失。

You will want to use a FrameLayout instead (or a RelativeLayout if you want more fine-grained control of the placement of views inside it). 您将要改用FrameLayout(或者,如果要对视图在其中的放置位置进行更细微的控制,则使用RelativeLayout)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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