自定义Shape

来源:互联网 发布:js jc jk jd 编辑:程序博客网 时间:2024/06/11 01:07

 

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">  
  2.     <solid android:color="#f033ff22"/>  
  3.     <stroke android:width="2dp" android:color="#ff1111" android:dashWidth="2dp"  
  4.      android:dashGap="0dp" />  
  5.     <padding android:left="10dp" android:top="10dp"  
  6.             android:right="10dp" android:bottom="10dp" />  
  7.     <corners android:radius="6dp" />  
  8. </shape>   

 

solid中定义的颜色值需要包含alpha值,

stroke可以定义边框的颜色,width为边框的宽度,dashWidth为画笔的宽度,dashGap为画笔的间距

(dashGap为0,则边框为实心的边线)

corners定义四角圆弧的半径。

TIP:

对于使用selector定义不同的状态也可以使用shape替代drawable的属性。

 

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">     
  3.     <item android:state_focused="false"     
  4.         android:drawable="@drawable/gallery_selected_unfocused" />     
  5.     <item android:state_focused="true"     
  6.         android:drawable="@drawable/gallery_selected_focused" />     
  7.     <item android:state_pressed="true"     
  8.         android:drawable="@drawable/gallery_unselected_focused" />     
  9.    <item android:state_selected="true"     
  10.         android:drawable="@drawable/gallery_unselected_focused" />     
  11. </selector>  

 

--->>>

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">     
  3.     <item android:state_focused="false" >  
  4.         <shape>  
  5.             <solid ...>  
  6.             </solid>  
  7.         </shape>  
  8.     </item>  
  9.     <item android:state_focused="true"  >  
  10.         <shape>  
  11.             <solid ...>  
  12.             </solid>  
  13.         </shape>  
  14.     </item>  
  15.     <item android:state_pressed="true" >  
  16.         <shape>  
  17.             <solid ...>  
  18.             </solid>  
  19.         </shape>  
  20.     </item>   
  21. </selector>  
  22.      我们今天来说一说怎么样让你的Button更炫一些,如果把这个添加到你的游戏里,你就会让你的游戏更吸引玩家,android为我们提供了太多难以想象的效果,那么我们就来看看这个效果怎么样来实现吧,我们还是先来看看效果图:
    2.png 

           完事我们就来看看代码吧。让我们尽情的想象我们的界面有多么的炫吧。

    Java代码:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. < selector //view的各种状态(正常无焦点,焦点,onclick按下、放开等等)
    3. xmlns:android="http://schemas.android.com/apk/res/android">
    4. < item android:state_pressed="true" > //选中未up时
    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. < item android:state_focused="true" > //焦点
    23. < shape>
    24. < gradient
    25. android:startColor="#ffc2b7"
    26. android:endColor="#ffc2b7"
    27. android:angle="270" />
    28. < stroke
    29. android:width="2dp"
    30. android:color="#dcdcdc" />
    31. < corners
    32. android:radius="2dp" />
    33. < padding
    34. android:left="10dp"
    35. android:top="10dp"
    36. android:right="10dp"
    37. android:bottom="10dp" />
    38. < /shape>
    39. < /item>
    40. < item> //无焦点
    41. < shape>
    42. < gradient
    43. android:startColor="#ff9d77"
    44. android:endColor="#ff9d77"
    45. android:angle="270" />
    46. < stroke
    47. android:width="2dp"
    48. android:color="#fad3cf" />
    49. < corners
    50. android:radius="2dp" />
    51. < padding
    52. android:left="10dp"
    53. android:top="10dp"
    54. android:right="10dp"
    55. android:bottom="10dp" />
    56. < /shape>
    57. < /item>
    58. < /selector>
    复制代码

           看看我们的android有多么强大。这么炫的Button都能显示出来,android还有什么不能干的那。让我们开发出更炫的。

 

原创粉丝点击