2016-09-08 79 views
0

我有一个布局,其中包含两个带有一些叠加层的大图像。围绕缩放环绕ViewGroup“紧密”ImageView

在图像的顶部,有一个半透明的背景。这是问题出在哪里:

图像位于布局的水平中心,有android:scaleType="fitCenter"。这是如何正确显示图像。然而,半透明背景占据了布局的整个宽度,而不是将其自身限制为imageview。我想是半透明的酒吧只占据图像的宽度。

我怎么能得到半透明的部分只占用图像,最好只与XML?

Current situation with too broad overlays

当前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:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:background="#222"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_margin="4dp" 
     android:layout_gravity="center_horizontal"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:src="@drawable/test_img_1" 
      android:adjustViewBounds="true" 
      android:layout_centerHorizontal="true" 
      android:scaleType="fitCenter"/> 

     <View android:id="@+id/blur_user" 
      android:layout_width="match_parent" 
      android:layout_height="36dp" 
      android:alpha="0.7" 
      android:background="@color/colorPrimary" 
      android:layout_alignParentBottom="true"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="36dp" 
      android:gravity="center" 
      android:layout_alignParentBottom="true" 
      android:text="Some text"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/user_choice" 
      android:background="?attr/selectableItemBackground"/> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_margin="4dp" 
     android:layout_gravity="center_horizontal"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:src="@drawable/test_img_1" 
      android:adjustViewBounds="true" 
      android:layout_centerHorizontal="true" 
      android:scaleType="fitCenter"/> 

     <View android:id="@+id/blur_installer" 
      android:layout_width="match_parent" 
      android:layout_height="36dp" 
      android:alpha="0.7" 
      android:background="@color/colorPrimary" 
      android:layout_alignParentBottom="true"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="36dp" 
      android:gravity="center" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:text="Some text"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/installer_choice" 
      android:background="?attr/selectableItemBackground"/> 
    </RelativeLayout> 

</LinearLayout> 
+0

'机器人:scaleType = “fitXY”' –

+2

@IntelliJAmiya:设置scaleType为'fitXY'不有所作为,据我所看到的。你能详细说明一下吗? – Marcel50506

+0

在'RelativeLayout'和'ImageView'部分设置'android:layout_width ='match_parent''为测试用例注释'android:adjustViewBounds =“true”' –

回答

0

做了很多尝试和错误的,但是这一切都导致了没有结果。正如@Msk在评论中所说,似乎没有办法只用XML来实现这一点。所以我通过将这些行添加到onCreateView方法来解决它。它监听图像的绘制时间,并相应地调整模糊。

image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      image.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      ViewGroup.LayoutParams layoutParams = blurView.getLayoutParams(); 
      layoutParams.width = image.getWidth(); 
      blurView.setLayoutParams(layoutParams); 
     } 
    });