项目中涉及到用Flex画虚线,网上参考一些,稍加改动记录下来。
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3 backgroundGradientColors="[0xFFFFFF, 0xd5f3fa]" creationComplete="myInit()">
4
5 <mx:Script>
6 <![CDATA[
7 import mx.controls.*;
8 import mx.core.*;
9
10 private var lines : Sprite = new Sprite ();
11 private var component: UIComponent = new UIComponent();
12
13 private function drawDashed(graphics:Graphics,p1:Point,p2:Point,length:Number=5,gap:Number=5):void {
14 graphics.lineStyle(2,0xc4a00a,1);
15 var max:Number = Point.distance(p1,p2);
16 var l:Number = 0;
17 var p3:Point;
18 var p4:Point;
19 while(l<max) {
20 p3 = Point.interpolate(p2,p1,l/max);
21 l+=length;
22 if(l>max)
23 l=max;
24 p4 = Point.interpolate(p2,p1,l/max);
25 lines.graphics.moveTo(p3.x,p3.y);
26 lines.graphics.lineTo(p4.x,p4.y);
27 l+=gap;
28 }
29 component.addChild(lines);
30 this.addChild(component);
31 }
32
33 private function myInit() : void {
34 var first : Point = new Point(100,100);
35 var second : Point = new Point(300,300);
36
37 drawDashed(lines.graphics,first,second);
38 }
39 ]]>
40 </mx:Script>
41
42</mx:Application>