ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
#### 6.2.2 ShapeDrawable ShapeDrawable是一种很常见的Drawable,可以理解为通过颜色来构造的图形,它既可以是纯色的图形,也可以是具有渐变效果的图形。ShapeDrawable的语法稍显复杂,如下所示。 <? 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> 需要注意的是<shape>标签创建的Drawable,其实体类实际上是GradientDrawable,下面分别介绍各个属性的含义。 android:shape 表示图形的形状,有四个选项:rectangle(矩形)、oval(椭圆)、line(横线)和ring(圆环)。它的默认值是矩形,另外line和ring这两个选项必须要通过<stroke>标签来指定线的宽度和颜色等信息,否则将无法达到预期的显示效果。 针对ring这个形状,有5个特殊的属性:android:innerRadius、android:thickness、android:innerRadiusRatio、android:thicknessRatio和android:useLevel,它们的含义如表6-2所示。 :-: 表6-2 ring的属性值 ![](https://img.kancloud.cn/6e/31/6e31a7bb1960cd5ddac241c40ff4b0df_1347x369.png) `<corners> ` 表示shape的四个角的角度。它只适用于矩形shape,这里的角度是指圆角的程度,用px来表示,它有如下5个属性: * · android:radius——为四个角同时设定相同的角度,优先级较低,会被其他四个属性覆盖; * · android:topLeftRadius——设定最上角的角度; * · android:topRightRadius——设定右上角的角度; * · android:bottomLeftRadius——设定最下角的角度; * · android:bottomRightRadius——设定右下角的角度。 `<gradient> ` 它与<solid>标签是互相排斥的,其中solid表示纯色填充,而gradient则表示渐变效果,gradient有如下几个属性: * android:angle——渐变的角度,默认为0,其值必须为45的倍数,0表示从左到右,90表示从下到上,具体的效果需要自行体验,总之角度会影响渐变的方向; * · android:centerX——渐变的中心点的横坐标; * · android:centerY——渐变的中心点的纵坐标,渐变的中心点会影响渐变的具体效果; * · android:startColor——渐变的起始色; * · android:centerColor——渐变的中间色; * · android:endColo——渐变的结束色; * · android:gradientRadius——渐变半径,仅当android:type= "radial"时有效; * · android:useLevel——一般为false,当Drawable作为StateListDrawable使用时为true; * · android:type——渐变的类别,有linear(线性渐变)、radial(径向渐变)、sweep(扫描线渐变)三种,其中默认值为线性渐变,它们三者的区别如图6-3所示。 :-: ![](https://img.kancloud.cn/e3/f6/e3f67f413919c6dece45815f078f6b8a_1354x439.png) 图6-3 渐变的类别,从左到右依次为linear、radial、sweep `<solid> ` 这个标签表示纯色填充,通过android:color即可指定shape中填充的颜色。 `<stroke> ` Shape的描边,有如下几个属性: * · android:width——描边的宽度,越大则shape的边缘线就会看起来越粗; * · android:color——描边的颜色; * · android:dashWidth——组成虚线的线段的宽度; * android:dashGap——组成虚线的线段之间的间隔,间隔越大则虚线看起来空隙就越大。 注意如果android:dashWidth和android:dashGap有任何一个为0,那么虚线效果将不能生效。下面是一个具体的例子,效果图如图6-4所示。 :-: ![](https://img.kancloud.cn/e4/5f/e45f6204f7f3e4902103a5eb40b3d442_594x592.png) 图6-4 shape的描边效果 <? xml version="1.0" encoding="utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ff0000" /> <stroke android:dashGap="2dp" android:dashWidth="10dp" android:width="2dp" android:color="#00ff00" /> </shape> `<padding> ` 这个表示空白,但是它表示的不是shape的空白,而是包含它的View的空白,有四个属性:android:left、android:top、android:right和android:bottom。 `<size> ` shape的大小,有两个属性:android:width和android:height,分别表示shape的宽/高。这个表示的是shape的固有大小,但是一般来说它并不是shape最终显示的大小,这个有点抽象,但是我们要明白,对于shape来说它并没有宽/高的概念,作为View的背景它会自适应View的宽/高。我们知道Drawable的两个方法getIntrinsicWidth和getIntrinsicHeight表示的是Drawable的固有宽/高,对于有些Drawable比如图片来说,它的固有宽/高就是图片的尺寸。而对于shape来说,默认情况下它是没有固有宽/高这个概念的,这个时候getIntrinsicWidth和getIntrinsicHeight会返回-1,但是如果通过`<size>`标签来指定宽/高信息,那么这个时候shape就有了所谓的固有宽/高。因此,总结来说,`<size>`标签设置的宽/高就是ShapeDrawable的固有宽/高,但是作为View的背景时,shape还会被拉伸或者缩小为View的大小。