Shape生成图形,既简单有实用,灵活性比较大,而且可以减少包的大小:
Android 使用Shape绘制背景图片的步骤:
1.在drawable文件夹下新建一个xml文件
2.在xml中绘制图形
3.在代码中引用这个xml
官网Sample:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
1.Shape语法
shape:可以画四种图形,分别是:矩形(rectangle)、椭圆(oval)、线(line)、圆环(ring)。
android:shape=["rectangle" | "oval" | "line" | "ring"]
Solid: 填充颜色
<solid
android:color="color" />
Corners: 圆角大小:
四个角可以不同的弧度,也可以相同的弧度
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
Size: 图形的大小:
<size
android:width="integer"
android:height="integer" />
Gradient: 关于渐变颜色
android:angle:渐变角度:0:左到右;90:下到上;180:右到左;270:上到下
android:centerX:表示渐变的X轴起始位置,范围0-1,0.5表示圆心。
android:centerY:表示渐变的Y轴起始位置,范围0-1,0.5表示圆心。
android:startColor:渐变起始颜色
android:endColor:渐变结束颜色
android:type:渐变类型,有三种
分别是:
linear 线性渐变,默认的渐变类型
radial 放射渐变,设置该项时,android:gradientRadius也必须设置
sweep 扫描性渐变
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
Stroke:边框
android:width:边框大小
android:color:边框颜色
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
Padding:设置内边距,4个方向的内边距
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
示例:
1.圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorPrimary"></solid>
<size android:height="100dp"
android:width="100dp"></size>
</shape>
2.方形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"></corners>
<solid android:color="@color/colorPrimary"></solid>
<padding android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp"></padding>
</shape>
3.线型
<?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/colorPrimaryDark" <!--虚线间距宽度-->
android:dashGap="3dp" <!--虚线间距宽度-->
android:dashWidth="4dp"></stroke> <!--虚线宽度-->
<!--线的高度,size大小必须大于stroke 的android:width-->
<size android:height="3dp"></size>
</shape>
4.圆环
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="10dp">
<solid android:color="@color/colorAccent"></solid>
<!--useLevel需要设置为false-->
<!--android:thickness:圆环宽度-->
</shape>