Android资源知识(二)之Shape Drawable的使用

This is a generic shape defined in XML.

    这是google官方对资源Shape Drawable的定义,意思就是:这是定义在XML文件中的一类形状。通过Shape Drawable(以下简称Shape)可以使控件四个角变成圆角;设置控件的背景;设置控件内容到控件边界的距离……
    首先,贴出官方语法示例,代码如下:

<?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="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"] />
    <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的XML文件要放在路径res/drawable/filename.xml下。
2、XML文件名“filename”可以被用作资源ID。资源的引用方式如下:

    (1)Java代码里: R.drawable.filename
    (2)XML文件里: @[package:]drawable/filename

一、Shape元素

    Shape是根元素,使用方式如android:shape=["rectangle" | "oval" | "line" | "ring"]。其中:
(1)shape=rectangle为矩形,同时也是默认值。
(2)shape=oval为椭圆。
(3)shape=line为水平线条,该值需要和<stroke>元素配合使用。
(4)shape=ring为圆环。

    如下属性只有当shape=ring时才有效:
(a)innerRadius:内圆环半径大小,默认值为9
(b)innerRadiusRatio:内圆环半径的比率(比例)
(c)thickness:圆环的厚度,即外圆环与内圆环之间的距离,默认值为3
(d)thicknessRatio:圆环厚度的比率(比例)
(e)useLevel:为true时表示使用LevelListDrawable,当shape=ring时须设置为false

二、corners元素

    corners可以将矩形变为圆角矩形,该元素只有当shape=rectangle时才有效。radius为矩形4个直角设置圆角时的半径,若用topLeftRadius,topRightRadius,bottomLeftRadius,bottomRightRadius属性设置圆角时,必须为每个属性都赋值且大于1。

三、size元素

    size元素是shape的大小,height是高度,width是宽度。

四、solid元素

    solid元素用来指定shape的填充色,属性为android:color=颜色值。

五、stroke元素

    stroke元素是用来设置shape的描边属性。width是描边的宽度,color是描边的颜色,dashGap是虚线点之间的间隔,dashWidth是虚线的宽度,该属性只有当设置了dashGap属性时才有效。

六、gradient元素

    gradient指定shape的渐变色。
(1)angle是渐变角度,0表示从左到右,90表示从下到上,并且,angle的值必须是45的倍数,默认值为0。
(2)startColor为起始颜色,endColor为终止颜色,centerColor取值必须在startColor和endColor之间
(3)type为渐变类型,一共有3种渐变类型,分别是linear,radial,sweep。其中,linear为默认类型,当type=radial时,必须指定gradientRadius属性值,否则将解析出错。
(4)centerX是相对于X轴方向的渐变位置,centerY是相对于Y轴方向的渐变位置,二者的取值为 (0 - 1.0)

七、使用方法

    1、在XML文件中的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

   2、在代码中引用

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

最终效果图如下:


kinbos shape用例.png


文/kinbos(简书作者)
原文链接:http://www.jianshu.com/p/7e6489c1104d
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值