在开发中经常会用到shape标签来定义控件的背景,好处是减少apk的占内存大小,shape标签总共有四个图形选项,分别是rectangle(矩形),oval(椭圆),line(横线)和ring(圆环)。
res下新建一个Drawable resource file:
矩形效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff5900"/>
</shape>
圆角矩形效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff5900"/>
<corners android:radius="20dp"/>
</shape>
注;radius表示四个边角都设置。也可以只设置一个边角的弧度效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff5900"/>
<corners android:topLeftRadius="40dp"/>
</shape>
圆形效果:
扫描二维码关注公众号,回复:
3637451 查看本文章
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#3700ff"/>
<size android:height="50dp" android:width="50dp"/>
</shape>
环形效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="100dp"
android:shape="ring"
android:thickness="2dp"
android:useLevel="false" >
<!-- 设置固定填充色 -->
<solid android:color="#f00" />
<size android:height="44dp"/>
</shape>
虚线条:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:width="2dp" android:color="@color/colorAccent" android:dashWidth="20dp" android:dashGap="10dp"/>
</shape>
属性:
width:线的粗细
dashWidth:小线条的长度
dashgap:线条的间隙
线渐变效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#24abff"
android:centerColor="@color/white"
android:endColor="#ff00d0"
android:angle="0"
android:type="linear"/>
</shape>
属性:
startColor:开始颜色
centerColor:中间颜色
endColor:结束颜色
angle:渐变的角度(必须是45的倍数)
type:渐变类型:(linear表示线性渐变;sweep表示雷达渐变)
雷达渐变效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#25ba0b"
android:endColor="#ff0000"
android:angle="0"
android:type="sweep"/>
</shape>