2012-06-27 48 views
19

我想从代码中制作一个框架,以便我可以将其应用于内部实心填充以及内部透明的圆角内角。 就像一个内部有透明椭圆的实心矩形。附图。我尝试过几种形状组合,所有可用的在线显示外面的角落。带透明内框的圆角内部角落

enter image description here

内部应该是透明的不是白色。 该图像取自这个post,但这里提出的解决方案不是我正在寻找的,我不想使用9修补程序drawable但想要在代码中创建。

请只用有效答案。

回答

20

首先,在绘制文件夹中创建3 xmllayout

  1. 第一:frame.xml
  2. 二:frame_build.xml
  3. 三:red.xml

(你可以陈GE这个名字,如你所愿),

frame.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:bottom="20dp" android:drawable="@drawable/red" android:top="-25dp" /> 
    <item android:bottom="15dp" android:drawable="@drawable/frame_build" android:top="5dp" android:left="-5dp" android:right="-5dp" /> 
</layer-list> 

frame_build.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
    <corners android:radius="40dp" /> 
</shape> 

red.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <stroke android:width="40dp" android:height="40dp" android:color="#B22222" /> 
    <padding android:left="8dp" android:top="-1dp" android:right="8dp" android:bottom="9dp" /> 
    <corners android:radius="-10dp" /> 
</shape> 

最后请参阅您的视图或布局,以帧XML如下:

android:background="@drawable/frame" 

此测试,输出如下图:

Output image

希望这有助于。

+0

好像我刚才所需要的,让我试试那么我会将它标记为答案 – user606669

+0

@ user606669很高兴帮助你 –

+0

我试过了,但它的黑色背景不透明我做错了什么。我在framelayout中使用它,它应该显示它下面的框架。 – user606669

38

创建以下rounded_corner.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:bottom="-10dp" 
     android:left="-10dp" 
     android:right="-10dp" 
     android:top="-10dp"> 
     <shape android:shape="rectangle"> 
      <stroke 
       android:width="10dp" 
       android:color="#ffffff" /> 
      <corners android:radius="20dp" /> 
     </shape> 
    </item> 
</layer-list> 

添加此您的ImageView,要应用它的框架如下:

<View 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignBottom="@+id/my_image_view" 
    android:layout_alignLeft="@id/my_image_view" 
    android:layout_alignRight="@+id/my_image_view" 
    android:layout_alignTop="@id/my_image_view" 
    android:background="@drawable/rounded_corner" /> 
+1

这是最好的答案。谢谢! – joseph

+0

这很好,但我不明白为什么,你能解释一下吗? – Chol

+0

它在覆盖矩形拐角的矩形上绘制更大的“椭圆形笔画”。 –