shape的优势,Android开发中,使用shape可以方便的帮我们画出背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适应不同的手机;
1.shape(GradientDrawable)
shape顾名思义就是形状的意思,我们在平时开发的中,应用的频率也很高,该文件是一个xml文件,并放在drawable文件夹下如res/drawable/filename.xml,那么引用方式也很简单,我们一般在控件的background使用,如android:background="@drawable/filename";
shape文件基本结构如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=""> <corners /> <gradient /> <padding /> <size /> <solid /> <stroke /> </shape>
1.1<shape>标签下常见属性
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] >
属性说明
<!-- shape必须为根元素
xmlns:命名空间
android:shape指定形状类型
rectangle:填充包含视图的矩形,当不写该属性时默认此形状
oval:椭圆形状
line:线形状,此形状需要 <stroke> 元素定义线宽
ring:环形
当android:shape="ring"时,有可选属性
android:innerRadius:尺寸。环内部(中间的孔)的半径
android:innerRadiusRatio:浮点型。环内部的半径,以环宽度的比率表示。例如,如果 android:innerRadiusRatio="5",则内半径等于环宽度除以 5。此值被 android:innerRadius 覆盖。默认值为 9。
android:thickness:环的厚度
android:thicknessRatio:浮点型。环的厚度,表示为环宽度的比率。例如,如果 android:thicknessRatio="2",则厚度等于环宽度除以 2。此值被 android:innerRadius 覆盖。默认值为 3。
需要注意的是,如果你使用了上面几个属性绘制一个圆,会发现在控件中并没有效果,我们还需要一个重要属性
android:useLevel:布尔值。如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。
-->
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] />
1.2<corners>标签下常见属性
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
属性说明
<!--
corners为形状产生圆角,仅当形状为矩形时使用
android:radius:尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。
android:topLeftRadius:左上角的半径,以尺寸值或尺寸资源表示。
android:topRightRadius:右上角的半径,以尺寸值或尺寸资源表示。
android:bottomLeftRadius:左下角的半径,以尺寸值或尺寸资源表示。
android:bottomRightRadius:右下角的半径,以尺寸值或尺寸资源表示
-->
1.3<gradient>标签下属性说明
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
属性说明
<!--
指定形状的渐变颜色,使用渐变时不要使用solid,否则会被覆盖,看不到效果
android:angle:整型。渐变的角度(度)。0 为从左到右,90 为从下到上。必须是 45 的倍数。默认值为 0,如果写该值不是45的整数倍,将没有效果。
android:centerX:浮点型。渐变中心的相对 X 轴位置 (0 - 1.0)。
android:centerY:浮点型。渐变中心的相对 Y 轴位置 (0 - 1.0)。
android:centerColor:颜色。起