2016-09-08 80 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); 
     } 
    });