Shape简介
XML在Andorid系统中可不是仅仅是一个布局的文件,配置列表。在Android开发中,它可以画一副图。Android的开发者给XML提供了几个强大的技能来帮我们实现。
我们在Android开发中都知道XML文件必定是比图片小的,所以针对APP的优化,我们能够用shape实现的效果就尽量用shape实现。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
//默认为rectangle
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners //当shape="rectangle"时使用 //定义圆角
android:radius="integer" //全部的圆角半径
android:topLeftRadius="integer"0 //左上角的圆角半径
android:topRightRadius="integer" //右上角的圆角半径
android:bottomLeftRadius="integer" //左下角的圆角半径
android:bottomRightRadius="integer" /> //右下角的圆角半径
<gradient //渐变
android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="integer" //渐变中心X的相当位置,范围为0~1
android:centerY="integer" //渐变中心Y的相当位置,范围为0~1
android:centerColor="integer" //渐变中间点的颜色,在开始与结束点之间
android:endColor="color" //渐变结束点的颜色
android:gradientRadius="integer" //渐变的半径,只有当渐变类型为radial时才能使用
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
//共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变。
android:useLevel=["true" | "false"] />
//使用LevelListDrawable时就要设置为true。设为false时才有渐变效果
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size //指定大小 一般用在imageview中的scaleType中使用
android:width="integer"
<solid //填充颜色
android:color="color" />
<stroke //指定边框
android:width="integer"
android:color="color"
android:dashWidth="integer"
</shape>
从上诉代码可以看出来
android:shape=[“rectangle” | “oval” | “line” | “ring”] >
shape可以画四种图形,分别是:矩形(rectangle)、椭圆(oval)、线(line)、圆环(ring)。
Shape可以说是XML绘图的精华所在,它的功能十分强大,无论是扁平化,还是你无话,还是渐变,它都能绘制,整个shape的可用参数,在上面的表中已经列举的很清楚了。
怎么用shape?
1、在项目 res/drawable 文件夹下创建文件 my_shape.xml :
2、在项目 res/layout 布局文件中引用:
3、run效果