在Android开发中我们为了美观很多UI设计都需要一些背景图片,为了能减少资源图片,减小apk的大小,以及更好的适配(当然.9图片也可以很好的适配),Android为我们提供了shape可以定义一定规则的形状,如矩形(rectangle),椭圆形(oval),线性(line)和环形(ring)。
几个基本属性:
solid填充色
<solid android:color="#2196F3"/>
corners圆角
<corners android:radius="10dp"//全部圆角的半径,无限大就成半圆如9999dp
android:topLeftRadius="5dp"//左上角圆半径
android:topRightRadius="5dp"//右上角圆半径
android:bottomLeftRadius="5dp"//左下角圆半径
android:bottomRightRadius="5dp" />右下角圆半径
stroke边框
<stroke
android:width="1px"//边框的宽度
android:color="#ff0000"//边框的颜色
android:dashWidth="1dp"//虚线的宽度,值为0时是实线
android:dashGap="1dp"/>//虚线的间隔
size大小
<size
android:width="30dp"
android:height="15dp" />
gradient渐变色
<gradient
android:type="linear"//linear线性渐变(默认) ,radial放射渐变, sweep扫描式渐变
android:startColor="#B3CDCE"//渐变开始点的颜色
android:centerColor="#673AB7"//渐变中间点的颜色,在开始与结束点之间
android:endColor="#074747"//渐变结束点的颜色
android:angle="90"//渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="0.5"//渐变中心X的相当位置,范围为0~1
android:centerY="0.5"//渐变中心Y的相当位置,范围为0~1
android:gradientRadius="5dp"//渐变的半径,只有当渐变类型为radial时才能使用
android:useLevel="false" />//使用LevelListDrawable时就要设置为true,设为false时才有渐变效果
padding内边距
<padding
android:left="10dp"//左内边距
android:top="10dp"//上内边距
android:right="10dp"//右内边距
android:bottom="10dp" />//下内边距
最好把这些属性组合成一个例子:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#2196F3"/>
<corners android:radius="9999dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
/>
<stroke
android:width="1px"
android:color="#ff0000"
android:dashWidth="1dp"
android:dashGap="1dp"/>
<size
android:width="30dp"
android:height="15dp" />
<gradient
android:type="linear"
android:angle="90"
android:centerX="0.5"
android:centerY="0.5"
android:startColor="#B3CDCE"
android:centerColor="#673AB7"
android:endColor="#074747"
android:gradientRadius="5dp"
android:useLevel="false" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
更多请关注公众号:先跑的笨鸟