美文网首页
Android中Shape的属性说明及其使用

Android中Shape的属性说明及其使用

作者: jalen2024 | 来源:发表于2019-07-31 17:23 被阅读0次

    shape是常用的通过XML绘制简单形状的方法,一般可用于控件的背景,如按钮或者文本框背景,也经常用于布局的背景,其用法不难但是功能作用却很强大。在开发的过程中有时会突然忘记了属性,因此在这就记录一下,方便以后查看也省的专门为了某个属性而再去搜索。下面属性中的integer或者float都仅仅代表数值。为此也简单写了四个例子说明下,各个图形中的有些属性并不是必须的,只是为了展示属性的具体效果。

    <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"

        android:dither="false|true"            //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。

        android:shape="rectangle|line|oval|ring"//分别为矩形、线、椭圆、环。默认为矩形rectangle

        android:innerRadius="integer"          // shape为ring时可用,内环半径

        android:innerRadiusRatio="float"        // shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖

        android:thickness="integer"            // shape为ring时可用,环的厚度

        android:thicknessRatio="float"          // shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖

        android:tint="color"                    // 给shape着色

        android:tintMode="src_in|src_atop|src_over|add|multiply|screen" // 着色类型

        android:useLevel="false|true"          // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用

        android:visible="false|true"

        >

        <!-- 圆角 -->

        <corners

            android:radius="integer"            // 圆角半径,该值设置时下面四个属性失效

            android:bottomLeftRadius="integer"  // 左下角圆角半径

            android:bottomRightRadius="integer" // 右下角圆角半径

            android:topLeftRadius="integer"    // 左上角圆角半径

            android:topRightRadius="integer"    // 右上角圆角半径

            />

        <!-- 渐变 -->

        <gradient

            android:useLevel="false|true"      // 与上面shape中该属性的一致

            android:type="linear|radial|sweep"  // 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear

            android:angle="integer"            // 渐变角度,当上面type为线性渐变linear时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针

            android:centerColor="color"        // 渐变中间位置颜色

            android:startColor="color"          // 渐变开始位置颜色

            android:endColor="color"            // 渐变结束位置颜色

            android:centerX="float"            // type为放射性渐变radial时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置

            android:centerY="float"            // type为放射性渐变radial时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置

            android:gradientRadius="integer"    // type为放射性渐变radial时有效,渐变的半径

            />

        <!-- 内边距 -->

        <padding

            android:bottom="integer"  // 设置底部边距

            android:left="integer"    // 左边边距

            android:right="integer"  // 右边

            android:top="integer"    // 顶部

        />

        <!-- 大小 -->

        <size

            android:height="integer"  // 宽度

            android:width="integer"  // 高度

            />

        <!-- 填充 -->

        <solid

            android:color="color"    // shape的填充色

            />

        <!-- 描边 -->

        <stroke

            android:color="color"      // 描边的颜色

            android:width="integer"    // 描边的宽度

            android:dashGap="integer"  // 虚线间隔

            android:dashWidth="integer" // 虚线宽度

        />

    </shape>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    画个简单的环,这里因为设置了innerRadius属性和thickness属性具体值,所以innerRadiusRatio属性和thicknessRatio属性会被覆盖,可以删除,但记得添加useLevel属性并设为false,不然有可能图形不显示(o(╯□╰)o因为我就是这样的)

    <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"

        android:shape="ring"

        android:innerRadius="50dp"

        android:innerRadiusRatio="5"

        android:thickness="120dp"

        android:thicknessRatio="8"

        android:useLevel="false"

        >

        <gradient

            android:type="linear"

            android:useLevel="false"

            android:angle="45"

            android:startColor="@color/colorAccent"

            android:centerColor="@android:color/black"

            android:endColor="@color/colorPrimary"/>

    </shape>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    画个简单的椭圆

    <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"

        android:shape="oval"

        >

    <size

        android:width="50dp"

        android:height="100dp"/>

    <gradient

        android:type="radial"

        android:centerX="1"

        android:centerY="0.5"

        android:startColor="@color/colorPrimary"

        android:centerColor="@color/colorAccent"

        android:endColor="@color/colorPrimaryDark"

        android:gradientRadius="50dp"

        />

    </shape>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    画个简单的线

    <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"

        android:shape="line"

        >

        <stroke

            android:width="5dp"

            android:color="@color/colorAccent"

            android:dashGap="3dp"

            android:dashWidth="50dp"/>

    </shape>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    这里注意一下:由于android系统在3.0之后会使用硬件加速,会导致虚线在模拟器上显示正常,但是真机上显示依然是实线,所以需在使用时添加android:layerType="software"属性,使其能在真机上显示正常。

    画个矩形

    <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"

        >

        <solid

            android:color="@android:color/white"/>

        <gradient

            android:type="sweep"

            android:startColor="@android:color/holo_blue_bright"

            android:endColor="@android:color/holo_green_light"

            />

        <stroke

            android:width="5dp"

            android:color="@color/colorAccent"

            android:dashGap="3dp"

            android:dashWidth="50dp"/>

        <corners

            android:radius="50dp"/>

    </shape>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    ---------------------

    作者:ionetwogo

    来源:CSDN

    原文:https://blog.csdn.net/qq_15128547/article/details/56680494

    版权声明:本文为博主原创文章,转载请附上博文链接!

    相关文章

      网友评论

          本文标题:Android中Shape的属性说明及其使用

          本文链接:https://www.haomeiwen.com/subject/tqyirctx.html