2013-02-01 179 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可能会更好。

相关问题