Android开发之Shape和Selector、Layer-list的详解


转载请备注出自于:http://blog.csdn.net/qq_22118507/article/details/51505844

三者的使用的方法:
Java代码中:R.drawable.文件的名称 
XML中:Android:background="@drawable/文件的名称"
1.Shape
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.   
  3.  <!--     
  4.     android:shape=["rectangle" | "oval" | "line" | "ring"]    
  5.     shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring) 下面的属性只    android:shape="ring" 时可用:    
  6.     android:innerRadius         尺寸,内环的半径。    
  7.     android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,例如,如果android:innerRadiusRatio,表示                                 内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.    
  8.     android:thickness           尺寸,环的厚度    
  9.     android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果               android:thicknessRatio="2"那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是                                 3.    
  10.     android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.    
  11.   -->    
  12. <shape    
  13.     xmlns:android="http://schemas.android.com/apk/res/android"    
  14.     android:shape="rectangle">    
  15.         
  16.     <!--    
  17.         圆角    
  18.         android:radius              整型 半径    
  19.         android:topLeftRadius       整型 左上角半径    
  20.         android:topRightRadius      整型 右上角半径    
  21.         android:bottomLeftRadius    整型右下角半径    
  22.         android:bottomRightRadius   整型左下角半径  
  23.   
  24. 这里有个地方需要注意,bottomleftradius是右下角,而不是左下角,这个不要记错了
    还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。
  25.      -->    
  26.      <corners      
  27.         android:radius="8dp"    
  28.         android:topLeftRadius="5dp"    
  29.         android:topRightRadius="15dp"    
  30.         android:bottomLeftRadius="20dp"    
  31.         android:bottomRightRadius="25dp"      
  32.         />    
  33.          
  34.      <!--    
  35.         渐变色    
  36.         android:startColor  颜色值                             起始颜色    
  37.         android:endColor    颜色值                             结束颜色    
  38.         android:centerColor 整型                               渐变中间颜色,即开始颜色与结束颜色之间的颜色    
  39.         android:angle       整型                               渐变角度
  40.  
  41.         使用android:angle的注意事项:
  42.  angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍

          另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientradius="50"。

       android:angle网 上有各种说法,这里,我说说自己的实验结果,渐变的时候,最原始的,即android:angle=“0”时,是从左到右,按照开始颜色到结束颜色来渲染 的,android:angle=“90”是从上到下来渲染的,android:angle=“180”是从右到左来渲染的,android:angle=“360”和android:angle=“0”是一样的,所以这里应该是这样的,渲染时按照最原始的渲染色板(把控件内部看作一块可以绕中心旋转的板子)围绕控件中心来旋转相应的度数,即android:angle里面的值就是所需要旋转的角度,只是这个旋转角度必须是45的整数倍)

        
  43.         android:type        ["linear" | "radial" | "sweep"] 渐变类型(取值:linear、radial、sweep)    
  44.                             linear 线性渐变,这是默认设置    
  45.                             radial 放射性渐变,以开始色为中心。    
  46.                             sweep 扫描线式的渐变。  
  47.   
  48.        android:useLevel     ["true" | "false"]                如果要使用LevelListDrawable对象,就要设置为            true。设置为true无渐变。false有渐变色  
  49.   
  50.        android:gradientRadius 整型                            渐变色半径.当 android:type="radial" 时才使用。            单独使用 android:type="radial"会报错。    
  51.        android:centerX      整型                              渐变中心X点座标的相对位置    
  52.        android:centerY      整型                              渐变中心Y点座标的相对位置    
  53.     -->    
  54.     <gradient    
  55.         android:startColor="#FFFF0000"    
  56.         android:endColor="#80FF00FF"    
  57.         android:angle="45"    
  58.         />     
  59.             
  60.     <!--    
  61.         内边距,即内容与边的距离     
  62.         android:left    整型 左内边距    
  63.         android:top     整型 上内边距    
  64.         android:right   整型 右内边距    
  65.         android:bottom  整型 下内边距    
  66.       -->    
  67.      <padding     
  68.          android:left="10dp"    
  69.          android:top="10dp"    
  70.          android:right="10dp"    
  71.          android:bottom="10dp"    
  72.          />    
  73.          
  74.     <!--     
  75.         size 大小    
  76.         android:width   整型 宽度    
  77.         android:height  整型 高度    
  78.     -->    
  79.     <size    
  80.         android:width="600dp"    
  81.         />    
  82.         
  83.     <!--    
  84.         内部填充    
  85.         android:color   颜色值 填充颜色    
  86.     -->    
  87.     <solid     
  88.         android:color="#ffff9d77"    
  89.         />    
  90.         
  91.      <!--    
  92.         描边    
  93.         android:width       整型  描边的宽度    
  94.         android:color       颜色值     描边的颜色    
  95.         android:dashWidth   整型  表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线。    
  96.         android:dashGap     整型  表示描边为虚线时,虚线之间的间隔 即“ - - - - ”    
  97.      -->    
  98.      <stroke     
  99.         android:width="2dp"    
  100.         android:color="#dcdcdc"      
  101.         />     
  102. </shape>  
2.Selector

根据不同的选定状态来定义不同的现实效果,分为四大属性:

android:state_selected 是选中

android:state_focused 是获得焦点

android:state_pressed 是点击

android:state_enabled 是设置是否响应事件,指所有事件

另:

android:state_window_focused 默认时的背景图片


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>       
  2. <selector xmlns:Android="http://schemas.android.com/apk/res/android">     
  3. <!-- 默认时的背景图片-->      
  4. <item Android:drawable="@drawable/pic1" />        
  5. <!-- 没有焦点时的背景图片 -->      
  6. <item   
  7.    Android:state_window_focused="false"        
  8.    android:drawable="@drawable/pic_blue"   
  9.    />   <!--获得焦点时的图片背景-->      
    <item   
       Android:state_focused="true"   
       Android:drawable="@drawable/pic_green"   
       />      
  10. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->      
  11. <item   
  12.    Android:state_focused="true"   
  13.    android:state_pressed="true"     
  14.    android:drawable"@drawable/pic_red"   
  15.    />     
  16. <!-- 触摸模式下单击时的背景图片-->      
  17. <item   
  18.    Android:state_focused="false"   
  19.    Android:state_pressed="true"     
  20.    Android:drawable="@drawable/pic_pink"   
  21.    />      
  22. <!--选中时的图片背景-->      
  23. <item   
  24.    Android:state_selected="true"   
  25.    android:drawable="@drawable/pic_orange"   
  26.    />       
  27. <!--获得焦点时的图片背景-->      
  28. <item   
  29.    Android:state_focused="true"   
  30.    Android:drawable="@drawable/pic_green"   
  31.    />       
  32. </selector>   


3.layer-list

3.1多图层叠加效果(1)

                          

四个角都是圆角的效果。如果让UI设计人员直接出图,可能会更简单一些。但是我们使用android中layer-list多图层叠加效果同样可以实现。

我们把它拆分为三个部分,第一个部分是最顶端的那一行(我这里称为顶部),第二部分是中间部分(中间部分不需要圆角效果),第三部分是底部。

顶部的实现:

顶部是一个有灰色边框但无下边框,带圆角,白色背景的长方体。实现效果如下:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    <item >  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"  
                android:bottomRightRadius="0.1dp" android:bottomLeftRadius="0.1dp" />  
            <stroke android:width="1dp" android:color="#ffa8abad" />  
        </shape>  
    </item>  
     <item android:top="1dp" android:left="1dp" android:right="1dp">  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"  
                android:bottomRightRadius="0.1dp" android:bottomLeftRadius="0.1dp" />  
            <stroke android:width="1dp" android:color="#ffffffff" />  
        </shape>  
    </item> 
</layer-list>

中间部分是一个不带圆角 白色背景 灰色边框 无下边框 长方体.实现效果如下:

<?xml version="1.0" encoding="UTF-8"?>  
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  <item>  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <stroke  
                android:width="1dp"  
                android:color="#ffa8abad" />  
        </shape>  
    </item> 
     <item  
        android:left="1dp"  
        android:right="1dp"  
        android:top="1dp">  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <stroke  
                android:width="1dp"  
                android:color="#ffffffff" />  
        </shape>  
    </item> 
</layer-list>

底部是一个具有底部圆角,白色背景,灰色边框的长方体,实现效果如下:

<?xml version="1.0" encoding="UTF-8"?>  
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    <item>  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <corners android:topLeftRadius="0.1dp" android:topRightRadius="0.1dp"  
                android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />  
            <stroke android:width="1dp" android:color="#ffa8abad" />  
        </shape>  
    </item>  
   <item android:top="1dp" android:bottom="1dp" android:left="1dp" android:right="1dp">  
        <shape>  
            <solid android:color="#FFFFFF" />  
            <corners android:topLeftRadius="0.1dp" android:topRightRadius="0.1dp"  
                android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />  
            <stroke android:width="1dp" android:color="#ffffffff" />  
        </shape>  
    </item>
</layer-list>

3.3多图层叠加效果(2)

<?xml version="1.0" encoding="UTF-8"?>    
  1.  <layer-list    
  2.    xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <!--图片1-->  
  4.      <item android:id="@+id/user_faceback_drawable"  
  5.            android:drawable="@drawable/faceback" />    
  6.     <!--图片2-->  
  7.      <item android:id="@+id/user_face_drawable"   
  8.            android:drawable="@drawable/h001"     
  9.            android:left="10.0dip"   
  10.            android:top="18.0dip"   
  11.            android:right="25.0dip"   
  12.            android:bottom="35.0dip" />    
  13.  </layer-list>   
  14. <!--2个图片的叠加-->  

    效果图如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章