在Android的一个角落绘制半径边框到imageview或textview

时间:2012-05-08 08:37:27

标签: android css3

我需要在我的应用中为imageview或textview绘制边框,但我只需要在一个角落绘制它,就像图像一样。

enter image description here

我做了一个形状,但我在所有四个方面都获得了边界:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1px" android:color="#FF000000" />
    <solid android:color="#ffffffff"/>  
    <padding android:left="1px" android:top="1px"
        android:right="0px" android:bottom="0px" />
    <corners android:topLeftRadius="8px" /> 
</shape> 

我该如何在图像中制作它?

谢谢,Mattia

2 个答案:

答案 0 :(得分:6)

使用此代码将解决它:

<?xml version="1.0" encoding="utf-8" ?> 
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
   <shape android:shape="rectangle">
    <solid android:color="#FF0000" /> 
     <corners android:topLeftRadius="15dp" /> 
    </shape>
 </item>
<item android:left="10dp" android:top="10dp">
  <shape android:shape="rectangle">
   <solid android:color="#000000" /> 
    <corners android:topLeftRadius="15dp" /> 
  </shape>
 </item>
</layer-list>

你必须在(布局Xml)中进行调整,如下:

android:layout_width

机器人:layout_height

机器人:paddingTop

android:paddingLeft

这是输出:

enter image description here

希望这有帮助。

答案 1 :(得分:0)

这就是我的成就。使用inset drawable我们可以轻松实现这一目标,并且可以使用更少的代码进行更多自定义。使用此twoside_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- inset: It can remove border from any other side-->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="-15dp"
android:insetRight="-15dp">

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rectangle">
    <stroke
        android:width="20dp"
        android:color="#ED302F" />

     <!--<corners
        android:topLeftRadius="20dp"
       />-->

    <!--<solid android:color="#f50a0a" />-->
</shape>

insetBottom&amp; insetRight -dp 值有助于隐藏我们不需要的边框并作为输出:

two side border image

获取角点曲线删除上面代码中的注释行

<corners android:topLeftRadius="20dp"/>

现在我们可以看到曲线弯曲

border with curve image

xml frame layout中使用此<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="5dp" android:scaleType="centerCrop" android:src="@drawable/your_image_file" /> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/twoside_border" /> </FrameLayout> ,如下所示&amp;根据需要调整填充或边距,使其适合看起来像框架的边框图像。

{{1}}