Drawable资源——ShapeDrawable资源

来源:互联网 发布:主播音效软件 编辑:程序博客网 时间:2024/04/29 03:06

Drawable资源——ShapeDrawable资源

1,认识

ShapeDrawable资源:
在Android开发中可以去定义各种各样的形状。
用于定义一个基本的几何图形(如矩形、圆形、线条等)。
创建XML文件:
定义ShapeDrawable的XML文件的根本元素是<shape.../>,
文件位置 ︰
res/drawable/filename.xml
文件名用作资源 id。
语法:
    <?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>

2,各属性注解

     (1)<shape> 属性
xmlns:android
 字符串。所需。定义 XML 命名空间,
必须是"http://schemas.android.com/apk/res/android". 
android:shape   指定哪种类型的几何图形。
"rectangle"     一个矩形,填充包含的视图。这是默认的形状。
"oval"     椭圆形的形状,相互吻合包含视图的尺寸。
"line"     包含视图的宽度的水平线。这种形状要求<stroke>元素,
               定义线的宽度。
"ring"     一个环的形状。
   示例:android:shape=["rectangle(长方形、矩形)
                 "|"oval(椭圆形)"|"line(线性)"|"ring(环形)"] 
下面的属性只有在android:shape="ring时可用:
android:innerRadius         
              圆环的内半径,和android:innerRadiusRatio同时存在时,
              以innerRadius为准
android:innerRadiusRatio 
              内半径占整个Drawable宽度的比例,默认值为9,。如果为n,
              ,那么内半径=宽度/n.          
android:thickness              
              圆环的厚度,即外半径减去内半径的值,和android:thicknessRatio
              同时存在时,以android:thickness为准。
android:thicknessRatio 
               厚度占整个Drawable宽度的比例,默认值为3.如果为n,那么
               厚度 =宽度/n.
android:useLevel 
              一般为false,否则有可能无法达到预期的显示效果,除非当做
              是LevelListDrawable使用时值为true
(2)<corners>  属性
         表示Shape的四个角的角度,只适用于矩形。
圆角 android:radius为角的弧度,值越大角越圆。
        还可以把四个角设定成不同的角度。
属性
android:radius                             四个角的弧度,例如5dp
android:topLeftRadius                左上角的弧度,例如5dp
android:topRightRadius             右上角的弧度,例如5dp
android:bottomLeftRadius         左下角的弧度,例如5dp
android:bottomRightRadius      右下角的弧度例如5dp
注意:  同时设置五个属性,则Radius属性无效       数值为整型  

(3)<gradient> 
指定形状的渐变颜色。
      它与<solid>标签是互斥的,其中solid表示纯色填充,而gradient则表示渐变效果。
属性 ︰
android:angle    
 渐变角度,以度为单位。默认值为 0。(当angle=0时,渐变色
是从左向右。 然后逆时针方向转,当angle=90时为从下往上。
angle必须为45的整数倍)
android:centerX
渐变中心横坐标的相对位置 
android:centerY
渐变中心纵坐标的相对位置,渐变的中心点会影响渐变的具体效果
android:centerColor
渐变的中间颜色,作为十六进制值或颜色资源的可选颜色.
android:endColor
渐变的结束颜色,作为十六进制值或颜色资源的可选颜色.
android:startColor
渐变的起始颜色,作为十六进制值或颜色资源的可选颜色.
android:type
 渐变模式,默认的为线性渐变linear  
"linear"     线性渐变。这是默认设置。
"radial"     径向渐变。开始是中心颜色。
"sweep"     扫线渐变。
android:gradientRadius
渐变色半径.当 android:type="radial" 时才使用。单独使用 
android:type="radial"会报错。 浮点数值
android:useLevel  
如果要使用LevelListDrawable对象,就要设置为true。
设置为true无渐变。false有渐变色 

(4)<padding> 属性
padding 内边距,即内容与边的距离  数值为整型
android:left   左边距
android:top    上边距
android:right  右边距
android:bottom   底部边距


(5)<size>属性
形状的大小。
属性 ︰
android:height   高度.
android:width    宽度
注 ︰
对于Shape来说,默认情况下它是没有固定宽/高这个概念的,但通过size设置后,shape就有了所谓的宽/高,但是作为View的背景时,shape还会被拉伸或缩小为View的大小。

     (6)<solid>
            固体的颜色来填充形状。
            属性:
                     android:color
                          填充的颜色。要将作为十六进制值或颜色资源应用于形状的颜色.

(7)<stroke>
描边形状线条的。
属性 ︰
android:width        描边的宽度 ,越大则shape的边缘线就会看起来越粗
android:color         描边的颜色  
android:dashGap
组成虚线的线段之间的间隔,间隔越大则虚线看起来空隙越大 即。仅当android:dashWidth设置才有效。
android:dashWidth
组成虚线的线段的宽度, 值为0时,表示为实线。值大于0则为虚线。仅当android:dashGap设置才有效。

注意:只要android:dashGap和android:dashWidth有任何一个为0,那么虚线效果将不能生效。

3,示例代码

(1)shape_drawable_useing_1.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 设置填充颜色 -->
    <solid android:color="#fff"/>
    <!-- 设置四周的内边距 -->
    <padding android:left="7dp" 
        android:top="7dp" 
        android:right="7dp" 
        android:bottom="7dp" />
    <!-- 设置边框 -->
    <stroke android:width="3dip" android:color="#ff0" />
</shape>

(2)shape_drawable_useing_2.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>

(3)shape_drawable_useing_3.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <!-- 定义填充渐变颜色 -->
    <gradient 
        android:startColor="#ff0" 
        android:endColor="#00f" 
        android:angle="45"
        android:type="sweep"/> 
    <!-- 设置内填充 -->
    <padding android:left="7dp" 
        android:top="7dp" 
        android:right="7dp" 
        android:bottom="7dp" />
    <!-- 设置圆角矩形 -->
    <corners android:radius="8dp" /> 
</shape>

0 0