android图形 Shape Drawable
在res/drawable/目录下,创建图形资源文件 res/drawable/filename.xml,支持rectangle,oval,line,ring四种图形,其中默认是rectangle,也是用的最多的。图形资源大量用来设置为控件的背景。
在res/drawable/目录下新建一个rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@color/blue"/>
</shape>
生成一个矩形图形资源文件,填充蓝色,四个角圆角半径10dp
在res/drawable/目录下新建一个rectangle_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<gradient android:angle="0"
android:startColor="@color/blue"
android:endColor="@color/white"/>
<stroke android:color="@color/white"
android:width="5dp"
android:dashWidth="10dp"
android:dashGap="10dp"/>
</shape>
生成一个矩形图形资源文件,以渐变色填充,四个角圆角半径10dp,边界线高5dp,边界线宽10dp,边界线间隔10dp
在res/drawable/目录下新建一个oval.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dp"/>
<solid android:color="@color/blue"/>
</shape>
生成一个椭圆资源文件,以蓝色填充,椭圆半径10dp
在res/drawable/目录下新建一个line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="10dp"
android:color="@color/blue" />
</shape>
生成一条水平线,以蓝色填充,高度10dp,其中必须设置stroke属性
在res/drawable/目录下新建一个ring.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="20dp"
android:thickness="30dp"
android:useLevel="false">
<solid android:color="@color/blue"/>
</shape>
生成一个圆环,内半径20dp,环的厚度30dp,填充蓝色
Shape为ring属性时,注意如下几点:
1、android:useLevel=”false”该属性须设置为false
android:useLevel="boolean" //如果当做是LevelListDrawable使用时值为true,否则为false
2、以下属性只适用于ring
android:innerRadius="dimension" //内环半径
android:innerRadiusRatio="float" //内环半径相对于环的宽度的比例
android:thickness="dimension" //环的厚度
android:thicknessRatio="float" //环的厚度相对于环的宽度的比例