2013-02-01 99 views
2

我目前正在使用drawable來獲取環形圖像視圖。RingShapeDrawable的半徑設置爲父寬高度而不是寬度

問題是:我有兩個這些imageviews所有與weight = 1,所以他們都有相同的寬度。形狀環drawable的xml標籤屬性「innerRadiusRatio」依賴於drawable的寬度。因此,如果圖像視圖的寬度變得比高度大,則環形形狀將被裁剪。有什麼辦法可以讓innerRadiusRatio依賴高度而不是寬度?甚至將其scaleType設置爲「fitCenter」或類似的東西?

這裏是我的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_height="64dip" 
      android:layout_width="match_parent"> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@drawable/ring_shape_drawable" 
    android:src="@drawable/test_button_drawable"/> 

<ImageView   
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@drawable/ring_shape_drawable" 
    android:src="@drawable/test_button_drawable"/> 
</LinearLayout> 

這裏的形狀繪製的XML:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadiusRatio="2.8" 
    android:thicknessRatio="30" 
    android:useLevel="false" 
    android:shape="ring" > 

    <solid android:color="@color/custom_red" /> 

</shape> 

而這裏的結果(與裁剪ringShapeDrawable我不希望): enter image description here

這將是理想的結果: enter image description here

感謝您的幫助:)

+0

您的drawables是XML嗎?如果是這樣,讓他們。 – anthropomo

+0

可繪製的形狀沒有什麼特別的。但無論如何,我編輯了我的問題,並添加到它的XML。 – MrMaffen

回答

0

如果設置了ImageViews的高度和重量屬性wrap_content你應該得到你想要的結果。

+0

不,我不這樣做,因爲innerRadiusRatio完全依賴於imageView的寬度。 – MrMaffen

1

由於您在LinearLayout中指定了一個不變的高度,因此您應該可以使用innerRadius屬性。您可能希望將ThicknessRatio切換爲具有一定尺寸的厚度。

我已經看過GradientDrawable的源代碼,但似乎並沒有簡單的方法讓它使用高度,所以如果innerRadius不合適,您可能需要將每個ImageView在另一個LinearLayout中,並使用android:gravity進行居中。這樣做需要drawable來確定ImageView的大小或手動指定它,所以innerRadius可能會更好。

相關問題