zoukankan      html  css  js  c++  java
  • Android-----使用Button特效 selector+shape

    当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、stroke、corners等。

      我们就以目前系统的Button的selector为例说下:

    Java代码  
    1. <shape>  
    2.             <gradient  
    3.                 android:startColor="#ff8c00"  
    4.                 android:endColor="#FFFFFF"  
    5.                 android:angle="270" />  
    6.             <stroke  
    7.                 android:width="2dp"  
    8.                 android:color="#dcdcdc" />  
    9.             <corners  
    10.                 android:radius="2dp" />  
    11.             <padding  
    12.                 android:left="10dp"  
    13.                 android:top="10dp"  
    14.                 android:right="10dp"  
    15.                 android:bottom="10dp" />  
    16.         </shape>  

    对于上面,这条shape的定义,分别为渐变,在gradient中startColor属性为开始的颜色,endColor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。

     对于一个Button完整的定义可以为:

    Java代码  
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <selector  
    3.     xmlns:android="http://www.norkoo.com">  
    4.     <item android:state_pressed="true" >  
    5.         <shape>  
    6.             <gradient  
    7.                 android:startColor="#ff8c00"  
    8.                 android:endColor="#FFFFFF"  
    9.                 android:angle="270" />  
    10.             <stroke  
    11.                 android:width="2dp"  
    12.                 android:color="#dcdcdc" />  
    13.             <corners  
    14.                 android:radius="2dp" />  
    15.             <padding  
    16.                 android:left="10dp"  
    17.                 android:top="10dp"  
    18.                 android:right="10dp"  
    19.                 android:bottom="10dp" />  
    20.         </shape>  
    21.     </item>  
    22.   
    23.     <item android:state_focused="true" >  
    24.         <shape>  
    25.             <gradient  
    26.                 android:startColor="#ffc2b7"  
    27.                 android:endColor="#ffc2b7"  
    28.                 android:angle="270" />  
    29.             <stroke  
    30.                 android:width="2dp"  
    31.                 android:color="#dcdcdc" />  
    32.             <corners  
    33.                 android:radius="2dp" />  
    34.             <padding  
    35.                 android:left="10dp"  
    36.                 android:top="10dp"  
    37.                 android:right="10dp"  
    38.                 android:bottom="10dp" />  
    39.         </shape>  
    40.     </item>  
    41.   
    42.     <item>          
    43.         <shape>  
    44.             <gradient  
    45.                 android:startColor="#ff9d77"  
    46.                 android:endColor="#ff9d77"  
    47.                 android:angle="270" />  
    48.             <stroke  
    49.                 android:width="2dp"  
    50.                 android:color="#fad3cf" />  
    51.             <corners  
    52.                 android:radius="2dp" />  
    53.             <padding  
    54.                 android:left="10dp"  
    55.                 android:top="10dp"  
    56.                 android:right="10dp"  
    57.                 android:bottom="10dp" />  
    58.         </shape>  
    59.     </item>  
    60. </selector>  

    注意!提示大家,以上几个item的区别主要是体现在state_pressed按下或state_focused获得焦点时,当当来判断显示什么类型,而没有state_xxx属性的item可以看作是常规状态下。

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

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

            <item

                android:color="hex_color"

                android:state_pressed=["true" | "false"]

                android:state_focused=["true" | "false"]

                android:state_selected=["true" | "false"]

                android:state_active=["true" | "false"]

                android:state_checkable=["true" | "false"]

                android:state_checked=["true" | "false"]

                android:state_enabled=["true" | "false"]

                android:state_window_focused=["true" | "false"] />

        </selector>

    Elements:

    <selector>

    必须。必须是根元素。包含一个或多个<item>元素。

                  Attributes:

                      xmlns:android

                                String,必须。定义XML的命名空间,必须是

                                “http://schemas.android.com/apk/res/android”.

        <item>

                  定义特定状态的color,通过它的特性指定。必须是<selector>的子元素。

                  Attributes:

                      android:color

                                16进制颜色。必须。这个颜色由RGB值指定,可带Alpha。

                                这个值必须以“#”开头,后面跟随Alpha-Red-Green-Blue信息:

    l   #RGB

    l   #ARGB

    l   #RRGGBB

    l   #AARRGGBB

        android:state_pressed

    Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

                      android:state_focused

    Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。

                      android:state_selected

    Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。

                      android:state_checkable

    Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

                      android:state_checked

                                Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

                      android:state_enabled

    Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

                      android:window_focused

    Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。

    注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

    Examples:

    XML文件保存在res/color/button_text.xml

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

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

        <item android:state_pressed="true"

              android:color="#ffff0000"/> <!-- pressed -->

        <item android:state_focused="true"

              android:color="#ff0000ff"/> <!-- focused -->

        <item android:color="#ff000000"/> <!-- default -->

    </selector>

        这个Layout XML会应用ColorStateList到一个View上:

    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/button_text"

        android:textColor="@color/button_text" />

    来源(一):http://www.norkoo.com/show/Mobile_Technology/android/haihkhjijkijajkihgcdkiibkbcee.aspx

    来源(二):http://www.cnblogs.com/xirihanlin/archive/2010/06/11/1756121.html

  • 相关阅读:
    MySQL一键安装
    架构师学习之路
    (转)Shell常用的特殊位置参数变量说明
    (转)Http状态码301和302概念简单区别及企业应用案例
    (转)nginx location在配置中的优先级
    (转)最新版 nginx内置变量 大全
    (转)nginx 常用模块整理
    (转)haproxy启动故障:Starting proxy:cannot bind socke
    keepalived脑裂问题查找
    (转)Apache和Nginx运行原理解析
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/4013761.html
Copyright © 2011-2022 走看看