代码:

   
   
   
   
  1. package contr    
  2. {    
  3.     import flash.geom.Point;  
  4.       
  5.     import mx.controls.Alert;  
  6.     import mx.controls.Text;  
  7.     import mx.graphics.SolidColorStroke;  
  8.       
  9.     import spark.components.BorderContainer;  
  10.     import spark.primitives.Line;  
  11.  
  12.     public class Ruler extends BorderContainer    
  13.     {    
  14.         private var _trunkLine:Line;    
  15.           
  16.         /**设置标尺的长度*/  
  17.         private var _lineLength:Number;  
  18.           
  19.         /**设置最小单位*/  
  20.         private var _spacing:Number;  
  21.           
  22.         /**设置隔几个刻度显示一次*/  
  23.         private var _scale:Number;  
  24.           
  25.         /**标尺是横向还是纵向*/  
  26.         private var _type:String = "horizontal";  
  27.           
  28.         /**标尺的位置,包括上下左右*/  
  29.         private var _position:String = "top";  
  30.  
  31.         [Inspectable(enumeration="top,bottom,left,right")]  
  32.         public function get position():String  
  33.         {  
  34.             return _position;  
  35.         }  
  36.           
  37.         public function set position(value:String):void  
  38.         {  
  39.             _position = value;  
  40.         }  
  41.  
  42.         public function get scale():Number  
  43.         {  
  44.             return _scale;  
  45.         }  
  46.  
  47.         public function set scale(value:Number):void  
  48.         {  
  49.             _scale = value;  
  50.         }  
  51.  
  52.         public function get spacing():Number  
  53.         {  
  54.             return _spacing;  
  55.         }  
  56.  
  57.         public function set spacing(value:Number):void  
  58.         {  
  59.             _spacing = value;  
  60.         }  
  61.  
  62.         [Inspectable(enumeration="vertical,horizontal")]  
  63.         public function get type():String  
  64.         {  
  65.             return _type;  
  66.         }  
  67.           
  68.         public function set type(value:String):void  
  69.         {  
  70.             _type = value;  
  71.         }  
  72.         public function Ruler()    
  73.         {    
  74.             super();       
  75.               
  76.         }  
  77.  
  78.         public function get lineLength():Number  
  79.         {  
  80.             return _lineLength;  
  81.         }  
  82.           
  83.         public function set lineLength(value:Number):void  
  84.         {  
  85.             _lineLength = value;  
  86.         }  
  87.           
  88.           
  89.           
  90.         protected override function createChildren():void{  
  91.             super.updateDisplayList(unscaledWidth,unscaledHeight);  
  92.             this.alpha = 0.5;    
  93.             this.setStyle("borderColor","red");    
  94.             this.width = 0;    
  95.             this.height = 0;    
  96.             _trunkLine = new Line();    
  97.             _trunkLine.stroke = new SolidColorStroke(0x000000);    
  98.             this.x = 0;    
  99.             this.y = 0;  
  100.             if(position == "top"){  
  101.                 _trunkLine.xFrom = 25;    
  102.                 _trunkLine.yFrom = 25;  
  103.             }else if(position == "bottom"){  
  104.                 _trunkLine.xFrom = 25;    
  105.                 _trunkLine.yFrom = parentApplication.height-100;  
  106.             }else if(position == "left"){  
  107.                 _trunkLine.xFrom = 25;    
  108.                 _trunkLine.yFrom = 25;  
  109.             }else if(position == "right"){  
  110.                 _trunkLine.xFrom = 600;    
  111.                 _trunkLine.yFrom = 25;  
  112.             }  
  113.             if(type =="horizontal"){  
  114.                 _trunkLine_trunkLine.xTo = _trunkLine.xFrom + lineLength;    
  115.                 _trunkLine_trunkLine.yTo = _trunkLine.yFrom;    
  116.                 this.addElement(_trunkLine);    
  117.                 for(var i:Number=0;i<=lineLength/spacing;i++){    
  118.                 var _line:Line = new Line();    
  119.                 var _ySpacing:Number = 5;    
  120.                 _line.stroke = new SolidColorStroke(0x000000);    
  121.                 _line.xFrom = i * spacing + _trunkLine.xFrom;    
  122.                 _line.yFrom = _trunkLine.yFrom;    
  123.                 _line_line.xTo = _line.xFrom;    
  124.                 var _label:spark.components.Label = new spark.components.Label();    
  125.                 _label.text = String(i * 10);    
  126.                 _label.y = _trunkLine.yFrom - 20;  
  127.                 if(i%10==0){    
  128.                     _ySpacing = 15;                           
  129.                 }else if(i%5==0){    
  130.                     _ySpacing = 10;    
  131.                 }    
  132.                 if(i%scale==0){  
  133.                     _label.x = i * spacing + _trunkLine.xFrom+1;  
  134.                     this.addElement(_label);  
  135.                 }  
  136.                 _line.yTo = _trunkLine.yFrom - _ySpacing;    
  137.                 this.addElement(_line);  
  138.                 }  
  139.             }else{  
  140.                 _trunkLine = new Line();    
  141.                 _trunkLine.stroke = new SolidColorStroke(0x000000);      
  142.                 _trunkLine_trunkLine.xTo = _trunkLine.xFrom;   
  143.                 _trunkLine_trunkLine.yTo = _trunkLine.yFrom + lineLength;    
  144.                 this.addElement(_trunkLine);    
  145.                   
  146.                 for(var j:Number = 0;j<=lineLength/spacing;j++){    
  147.                     var _xSpacing:Number = 5;    
  148.                     var _lineV:Line = new Line();    
  149.                     _lineV.stroke = new SolidColorStroke(0x000000);   
  150.                     var _labelV:spark.components.Label = new spark.components.Label();    
  151.                     _labelV.text = String(j * 10);    
  152.                     _labelV.x = (_trunkLine.xFrom - 20);    
  153.                     if(j%10==0){    
  154.                         _xSpacing = 15;    
  155.                     }else if(j%5==0){    
  156.                         _xSpacing = 10;   
  157.                     }  
  158.                     if(j%scale==0){  
  159.                         _labelV.y = j * spacing+1 + + _trunkLine.yFrom;  
  160.                         this.addElement(_labelV);  
  161.                     }  
  162.                     _lineV.xFrom = _trunkLine.xFrom - _xSpacing;    
  163.                     _lineV.yFrom = j * spacing + _trunkLine.yFrom;    
  164.                     _lineV.xTo = _trunkLine.xFrom    
  165.                     _lineV.yTo = j * spacing + _trunkLine.yFrom;    
  166.                     this.addElement(_lineV);  
  167.                 }  
  168.             }  
  169.               
  170.             }  
  171.         }  
  172.     }   

使用:

   
   
   
   
  1. xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                     xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                     xmlns:mx="library://ns.adobe.com/flex/mx" 
  5.                     xmlns:con="contr.*" 
  6.                     xmlns:logic="com.runqianapp.fastreport.view.logic.*"    backgroundAlpha="0.0" 
  7.                     width="100%" height="100%" fontFamily="宋体"  creationComplete="init()"> 
  8.     <fx:Declarations> 
  9.          
  10.     fx:Declarations> 
  11.     <mx:HBox width="100%" height="100%" paddingLeft="20" paddingTop="20" horizontalGap="0"> 
  12.         <con:Ruler lineLength="500" spacing="10" scale="5" type="vertical"/> 
  13.     mx:HBox> 
  14.       
  15. mx:Application> 

效果:

flex 学习笔记 as自定义标尺组件_第1张图片