flex 定义为组件定义皮肤有一下方式  :

一、    图形皮肤

  如下所示:

 

 
  
  1. mx|Button{  
  2.     upSkin:Embed(source="assets/btnup.png");  
  3.     overSkin:Embed(source="assets/btndown.png");  
  4.     downSkin:Embed(source="assets/btnup.png");  
  5.     focusSkin:Embed(source="assets/btnup.png");  
  6.     disabledSkin:Embed(source="assets/btnup.png");  
  7.     selectedUpSkin:Embed(source="assets/btndown.png");  
  8.     selectedDownSkin:Embed(source="assets/btndown.png");  
  9.     selectedOverSkin:Embed(source="assets/btnup.png");  
  10.     selectedDisabledSkin:Embed(source="assets/btndown.png");  

在css文件中,为button控件的八个对应属性添加图形皮肤。(spark 的button没有八个皮肤属性。。。)

二、  程序皮肤:

        1.    .mxml文件程序皮肤:

         

 
  
  1. s|Button{  
  2.     skin:Cla***eference("skin.ButtonSkin");  

                Buttonskin.mxml文件:

 

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <!--  
  4.  
  5.     ADOBE SYSTEMS INCORPORATED  
  6.     Copyright 2008 Adobe Systems Incorporated  
  7.     All Rights Reserved.  
  8.  
  9.     NOTICE: Adobe permits you to use, modify, and distribute this file  
  10.     in accordance with the terms of the license agreement accompanying it.  
  11.  
  12. --> 
  13.  
  14. <!--- The default skin class for the Spark Button component.    
  15.  
  16.        @see spark.components.Button  
  17.           
  18.       @langversion 3.0  
  19.       @playerversion Flash 10  
  20.       @playerversion AIR 1.5  
  21.       @productversion Flex 4  
  22. --> 
  23. <s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009"   
  24.              xmlns:s="library://ns.adobe.com/flex/spark"   
  25.              xmlns:fb="http://ns.adobe.com/flashbuilder/2009" 
  26.              minWidth="21" minHeight="21"   
  27.              alpha.disabled="0.5"> 
  28.        
  29.     <fx:Metadata> 
  30.         <![CDATA[   
  31.         /**   
  32.          * @copy spark.skins.spark.ApplicationSkin#hostComponent  
  33.          */  
  34.         [HostComponent("spark.components.Button")]  
  35.         ]]> 
  36.     </fx:Metadata> 
  37.       
  38.     <fx:Script fb:purpose="styling"> 
  39.         <![CDATA[           
  40.             import spark.components.Group;  
  41.             /* Define the skin elements that should not be colorized.   
  42.             For button, the graphics are colorized but the label is not. */  
  43.             static private const exclusions:Array = ["labelDisplay"];  
  44.               
  45.             /**   
  46.              * @private  
  47.              */       
  48.             override public function get colorizeExclusions():Array {return exclusions;}  
  49.               
  50.             /**  
  51.              * @private  
  52.              */  
  53.             override protected function initializationComplete():void  
  54.             {  
  55.                 useChromeColor = true;  
  56.                 super.initializationComplete();  
  57.             }    
  58.               
  59.             /**  
  60.              *  @private  
  61.              */  
  62.             override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void  
  63.             {  
  64.                 var cr:Number = getStyle("cornerRadius");  
  65.                   
  66.                 if (cornerRadius != cr)  
  67.                 {  
  68.                     cornerRadius = cr;  
  69.                     shadow.radiusX = cornerRadius;  
  70.                     fill.radiusX = cornerRadius;  
  71.                     lowlight.radiusX = cornerRadius;  
  72.                     highlight.radiusX = cornerRadius;  
  73.                     border.radiusX = cornerRadius;  
  74.                 }  
  75.                   
  76.                 if (highlightStroke) highlightStroke.radiusX = cornerRadius;  
  77.                 if (hldownstroke1) hldownstroke1.radiusX = cornerRadius;  
  78.                 if (hldownstroke2) hldownstroke2.radiusX = cornerRadius;  
  79.                   
  80.                 super.updateDisplayList(unscaledWidth, unscaledHeight);  
  81.             }  
  82.               
  83.             private var cornerRadius:Number = 2;  
  84.                                    
  85.         ]]>          
  86.     </fx:Script> 
  87.           
  88.     <!-- states --> 
  89.     <s:states> 
  90.         <s:State name="up" /> 
  91.         <s:State name="over" /> 
  92.         <s:State name="down" /> 
  93.         <s:State name="disabled" /> (定义了四种状态)
  94.     </s:states> 
  95.       
  96.     <!-- layer 1: shadow --> 
  97.     <!--- @private --> 
  98.     <s:Rect id="shadow" left="-1" right="-1" top="-1" bottom="-1" radiusX="2"> 
  99.         <s:fill> 
  100.             <s:LinearGradient rotation="90"> 
  101.                 <s:GradientEntry color="0x000000"   
  102.                                  color.down="0xFFFFFF" 
  103.                                  alpha="0.01" 
  104.                                  alpha.down="0" /> 
  105.                 <s:GradientEntry color="0x000000"   
  106.                                  color.down="0xFFFFFF"   
  107.                                  alpha="0.07" 
  108.                                  alpha.down="0.5" /> 
  109.             </s:LinearGradient> 
  110.         </s:fill> 
  111.     </s:Rect> 
  112.       
  113.     <!-- layer 2: fill --> 
  114.     <!--- @private --> 
  115.     <s:Rect id="fill" left="1" right="1" top="1" bottom="1" radiusX="2"> 
  116.         <s:fill> 
  117.             <s:LinearGradient rotation="90"> 
  118.                 <s:GradientEntry color="0xFFFFFF"   
  119.                                  color.over="0xBBBDBD"   
  120.                                  color.down="0xAAAAAA"   
  121.                                  alpha="0.85" /> 
  122.                 <s:GradientEntry color="0xD8D8D8"   
  123.                                  color.over="0x9FA0A1"   
  124.                                  color.down="0x929496"   
  125.                                  alpha="0.85" /> 
  126.             </s:LinearGradient> 
  127.         </s:fill> 
  128.     </s:Rect> 
  129.  
  130.     <!-- layer 3: fill lowlight --> 
  131.     <!--- @private --> 
  132.     <s:Rect id="lowlight" left="1" right="1" top="1" bottom="1" radiusX="2"> 
  133.         <s:fill> 
  134.             <s:LinearGradient rotation="270"> 
  135.                 <s:GradientEntry color="0x000000" ratio="0.0" alpha="0.0627" /> 
  136.                 <s:GradientEntry color="0x000000" ratio="0.48" alpha="0.0099" /> 
  137.                 <s:GradientEntry color="0x000000" ratio="0.48001" alpha="0" /> 
  138.             </s:LinearGradient> 
  139.         </s:fill> 
  140.     </s:Rect> 
  141.       
  142.     <!-- layer 4: fill highlight --> 
  143.     <!--- @private --> 
  144.     <s:Rect id="highlight" left="1" right="1" top="1" bottom="1" radiusX="2"> 
  145.         <s:fill> 
  146.             <s:LinearGradient rotation="90"> 
  147.                 <s:GradientEntry color="0xFFFFFF" 
  148.                                  ratio="0.0" 
  149.                                  alpha="0.33"   
  150.                                  alpha.over="0.22"   
  151.                                  alpha.down="0.12"/> 
  152.                 <s:GradientEntry color="0xFFFFFF" 
  153.                                  ratio="0.48" 
  154.                                  alpha="0.33" 
  155.                                  alpha.over="0.22" 
  156.                                  alpha.down="0.12" /> 
  157.                 <s:GradientEntry color="0xFFFFFF" 
  158.                                  ratio="0.48001" 
  159.                                  alpha="0" /> 
  160.             </s:LinearGradient> 
  161.         </s:fill> 
  162.     </s:Rect> 
  163.       
  164.     <!-- layer 5: highlight stroke (all states except down) --> 
  165.     <!--- @private --> 
  166.     <s:Rect id="highlightStroke" left="1" right="1" top="1" bottom="1" radiusX="2" excludeFrom="down"> 
  167.         <s:stroke> 
  168.             <s:LinearGradientStroke rotation="90" weight="1"> 
  169.                 <s:GradientEntry color="0xFFFFFF" alpha.over="0.22" /> 
  170.                 <s:GradientEntry color="0xD8D8D8" alpha.over="0.22" /> 
  171.             </s:LinearGradientStroke> 
  172.         </s:stroke> 
  173.     </s:Rect> 
  174.       
  175.     <!-- layer 6: highlight stroke (down state only) --> 
  176.     <!--- @private --> 
  177.     <s:Rect id="hldownstroke1" left="1" right="1" top="1" bottom="1" radiusX="2" includeIn="down"> 
  178.         <s:stroke> 
  179.             <s:LinearGradientStroke rotation="90" weight="1"> 
  180.                 <s:GradientEntry color="0x000000" alpha="0.25" ratio="0.0" /> 
  181.                 <s:GradientEntry color="0x000000" alpha="0.25" ratio="0.001" /> 
  182.                 <s:GradientEntry color="0x000000" alpha="0.07" ratio="0.0011" /> 
  183.                 <s:GradientEntry color="0x000000" alpha="0.07" ratio="0.965" /> 
  184.                 <s:GradientEntry color="0x000000" alpha="0.00" ratio="0.9651" /> 
  185.             </s:LinearGradientStroke> 
  186.         </s:stroke> 
  187.     </s:Rect> 
  188.     <!--- @private --> 
  189.     <s:Rect id="hldownstroke2" left="2" right="2" top="2" bottom="2" radiusX="2" includeIn="down"> 
  190.         <s:stroke> 
  191.             <s:LinearGradientStroke rotation="90" weight="1"> 
  192.                 <s:GradientEntry color="0x000000" alpha="0.09" ratio="0.0" /> 
  193.                 <s:GradientEntry color="0x000000" alpha="0.00" ratio="0.0001" /> 
  194.             </s:LinearGradientStroke> 
  195.         </s:stroke> 
  196.     </s:Rect> 
  197.  
  198.     <!-- layer 7: border - put on top of the fill so it doesn't disappear when scale is less than 1 --> 
  199.     <!--- @private --> 
  200.     <s:Rect id="border" left="1" right="1" top="1" bottom="1" width="69" height="20" radiusX="2"> 
  201.         <s:stroke> 
  202.             <s:LinearGradientStroke rotation="90" weight="1"> 
  203.                 <s:GradientEntry color="0x000000"   
  204.                                  alpha="0.5625" 
  205.                                  alpha.down="0.6375" /> 
  206.                 <s:GradientEntry color="0x000000"   
  207.                                  alpha="0.75"   
  208.                                  alpha.down="0.85" /> 
  209.             </s:LinearGradientStroke> 
  210.         </s:stroke> 
  211.     </s:Rect> 
  212.       
  213.     <!-- layer 8: text --> 
  214.     <!--- @copy spark.components.supportClasses.ButtonBase#labelDisplay  --> 
  215.     <s:Label id="labelDisplay" 
  216.              textAlign="center" 
  217.              maxDisplayedLines="1" 
  218.              horizontalCenter="0" verticalCenter="1" verticalAlign="middle" 
  219.              left="10" right="10" top="2" bottom="2" 
  220.              backgroundColor="#C7D2F2"> 
  221.     </s:Label> 
  222.       
  223. </s:SparkButtonSkin> 

        2 .     .as文件程序皮肤:

     从别的地方拷贝的 ,我自己没有做,就那么个意思,可以用as写皮肤:

 

 
  
  1. 1 package com.jiangzone  
  2. 2 {  
  3. 3     import mx.skins.Border  
  4. 4     import mx.core.EdgeMetrics;  
  5. 5     import mx.core.Container;  
  6. 6     import mx.graphics.RectangularDropShadow;  
  7. 7      
  8. 8     public class MyPanelBorderSkin extends Border {  
  9. 9          
  10. 10         public function MyPanelBorderSkin():void {  
  11. 11          }  
  12. 12          
  13. 13         /**  
  14. 14           * 该方法必需要覆盖,如果你要自定义自已的皮肤的话,  
  15. 15           * 该方法当在控件更新外观时将会被自动调用  
  16. 16           * 会传入两个参数数,第一个是Width,第二个是Height,即是该控件的宽与高  
  17. 17           * */  
  18. 18          override protected function updateDisplayList(w:Number,h:Number):void {  
  19. 19             super.updateDisplayList(w,h);  
  20. 20              
  21. 21              var ba:uint = 1;                //backgroundAlpha     背景透明度  
  22. 22              var bg:uint = 0xffffff;            //backgroundColor     背景颜色  
  23. 23              graphics.clear();                //graphics这个属性是父类里已经提供了的  
  24. 24              var p:Container = parent as Container;        //获取该皮肤所应用在的父容器,这里为Panel  
  25. 25              
  26. 26             //这里需要注意,一定要判断父容器是否已被设置,在文章里作解释  
  27. 27             if(p){  
  28. 28                 //获取容器定义的区域边界信息对象             
  29. 29                  var vm:EdgeMetrics = p.viewMetrics;         
  30. 30                 //设置四个角的圆度  
  31. 31                  var radiusContent:Object = {tl:vm.top,tr:0,bl:0,br:vm.top};     
  32. 32                 //标题栏圆度     
  33. 33                  var radiusTitle:Object = {tl:vm.top,tr:0,bl:0,br:0};         
  34. 34                 //画一个圆角矩形,整个背景  
  35. 35                 this.drawRoundRect(0,0,w,h,radiusContent,bg,ba);  
  36. 36                 //画一个圆角矩形,标题栏     
  37. 37                 this.drawRoundRect(0,0,w,vm.top,radiusTitle,0xff0000,.7);  
  38. 38                 //画一个圆角矩形,标题栏的那个高光水晶条  
  39. 39                 this.drawRoundRect(0,0,w,vm.top / 2,radiusTitle,0xffffff,.3);  
  40. 40                  
  41. 41                 //下面是画阴影的。  
  42. 42                  var dropShadow:RectangularDropShadow = new RectangularDropShadow();  
  43. 43                  dropShadow.distance = 8;  
  44. 44                  dropShadow.angle = 60;  
  45. 45                  dropShadow.color = 0x000000;  
  46. 46                  dropShadow.alpha = 0.4;  
  47. 47          
  48. 48                  dropShadow.tlRadius = radiusContent.tl;  
  49. 49                  dropShadow.trRadius = radiusContent.tr;  
  50. 50                  dropShadow.blRadius = radiusContent.bl;  
  51. 51                  dropShadow.brRadius = radiusContent.br;  
  52. 52          
  53. 53                  dropShadow.drawShadow(graphics, 0, 0, w, h);  
  54. 54              }  
  55. 55          }  
  56. 56      }  
  57. 57 } 

其实关键不就是程序皮肤的设计。。

嗯 flex4中关键就是spark.skins.SparkSkin这个类  以及\Flex4API\spark\skins\spark

里面的一些默认皮肤的学习,修改以及设计。