【问题标题】:Transparent Image in camera preview android相机预览android中的透明图像
【发布时间】:2014-06-02 10:01:27
【问题描述】:

我需要在相机预览中设置一个透明图像,我需要对其进行放大和缩小。

我有相机预览,在顶部的布局中我放了一个带有图像的图像视图。这张图片只有一个边框,中间是透明的,但是我看不到相机预览,我只看到白色背景的图片。

这是我的 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>

如果图像是透明的,为什么要设置白色背景?

谢谢

【问题讨论】:

    标签: android android-layout android-camera


    【解决方案1】:

    您需要使用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 >
    

    【讨论】:

    • 但是这样ImageView适合RelativeLayout,如果FrameLayout预览变小了怎么办?我需要一个与相机预览(FrameLayout)精确大小的 ImageView。
    【解决方案2】:

    这个问题与相机预览无关,只是取决于您选择的 ViewGroup。

    LinearLayout 将它的子元素并排排列,而不是像您想要的那样在顶部。在您的布局中,您的 LinearLayout 正在消耗片段或活动中的所有可见空间,因为它的宽度和高度设置为 match_parent。同样,您的 ImageView 也在做同样的事情,并占用了 LinearLayout 的所有可见空间。随后,FrameLayout 不再适合并消失在屏幕外的某个地方。

    您将希望改用 FrameLayout(如果您想要更细粒度地控制其中的视图放置,则可以使用 RelativeLayout)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多