加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex 动态画线

发布时间:2020-12-15 05:04:31 所属栏目:百科 来源:网络整理
导读:由于项目需要,实现画线功能,在网上找了些例子,效果都不满意,所以就自己动手简单写了一个测试的demo。 其实比较简单,下面来看看主要的代码吧。 ?xml version="1.0" encoding="utf-8"? s:View xmlns:fx="http://ns.adobe.com/mxml/2009" ??xmlns:s="libra

由于项目需要,实现画线功能,在网上找了些例子,效果都不满意,所以就自己动手简单写了一个测试的demo。

其实比较简单,下面来看看主要的代码吧。

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
??xmlns:s="library://ns.adobe.com/flex/spark" title="DTLine">

?<fx:Script>
??<![CDATA[
??import mx.controls.*;
??import mx.core.*;
??
??import spark.effects.*;
??import spark.effects.Fade;
??import spark.effects.Move;
??public var lines :Sprite =? new Sprite ();
??public var lines1 :flash.display.Sprite =? new Sprite ();
??
??public var count:int;
??
??public function TestPaint():void
??{
????? lines.graphics.lineStyle(2,0x0099ff,1);
????? DrawLine(lines,lbl,btn1);
????? DrawLine(lines,btn2);
????? DrawLine(lines,btn3);
??
????? count=0;
????? addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
?????
??}
??
??public function mouseDown(event:MouseEvent):void
??{???
??
????? lines.graphics.moveTo( event.localX,event.localY);
??
??}
??
??public function mouseUp(event:MouseEvent):void
??{???
???var comp: UIComponent = new UIComponent();
????? lines.graphics.lineTo(event.localX,event.localY);
????? comp.addChild(lines);
????? this.addElement(comp);
??}
??public function DrawLine(lines:Sprite,uFrom:UIComponent,uTo:UIComponent):void
??{
????? lines.graphics.moveTo(uFrom.x+uFrom.width/2,uFrom.y+uFrom.height/2);
????? lines.graphics.lineTo(uTo.x+uTo.width/2,uTo.y+uTo.height/2);
??}
??
??
??public function btn1_mouSEOver(event:Event):void
??{
????? lines1.graphics.lineStyle(2,0xd43dd6,1);
????? DrawLine(lines1,btn1,btn2);
????? DrawLine(lines1,lbl);
????? var comp: UIComponent = new UIComponent();
????? comp.addChild(lines1);
????? this.addElement(comp);
??}
??public function btn1_mouSEOut(event:Event):void
??{
????? lines1.graphics.clear();
????? DrawLine(lines,btn1);
??}
??
??
??public function Testmove():void
??{
????? var move:Move = new Move();
????? if(btn2.x!=lbl.x)
????? {
????????? move.target=btn2;
????????? move.end();
????????? move.xTo=lbl.x;
????????? move.yTo=lbl.y;
????????? move.play();
????? }
???? else
????? {
????????? move.target=btn2;
????????? move.end();
????????? move.xTo=340;
????????? move.yTo=104;
????????? move.play();
????? }
?? }
?? public function moveprint():void
?? {
??? addEventListener(MouseEvent.MOUSE_DOWN,mouseDown1);
??? addEventListener(MouseEvent.MOUSE_UP,mouseUp1);
???
?? }
???
???public function mouseDown1(event:MouseEvent):void
???{???
????lines.graphics.lineStyle(2,1);
????
????lines.graphics.moveTo(event.localX,event.localY);
????addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
????
????textArea.text="x:"+event.localX.toString()+"Y:"+event.localY.toString();
???}
???
???public function mouseUp1(event:MouseEvent):void
???{???
????this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
????lines.graphics.clear();
???}
???
??public function mouseMove(event:MouseEvent):void
??{
???var comp: UIComponent = new UIComponent();
???lines.graphics.lineTo(event.localX,event.localY);
???comp.addChild(lines);
???this.addElement(comp);
???
???textArea.text+="r"+"x:"+event.localX.toString()+",Y:"+event.localY.toString();
???
???fade.target=comp;
???fade.end();
???fade.play();
??}
???
??private function txtMouSEOver(event:MouseEvent):void
??{
???txt2.text+="r"+"Over_X:"+event.localX.toString()+",Over_Y:"+event.localY.toString();
??}
??private function txtMouSEOut(event:MouseEvent):void
??{
???txt2.text+="r"+"Out_X:"+event.localX.toString()+",Out_Y:"+event.localY.toString();
??}
???
??]]>
?</fx:Script>
?
?<fx:Declarations>
??<!-- 将非可视元素(例如服务、值对象)放在此处 -->
??<s:Fade id="fade" alphaFrom="1" alphaTo="0" duration="500"/>
?</fx:Declarations>

?<s:Button x="176" y="34" label="btn1" id="btn1" mouSEOver="btn1_mouSEOver(event)" mouSEOut="btn1_mouSEOut(event)"/>
?<s:Button x="340" y="104" label="btn4" id="btn2"/>
?<s:Button x="53" y="167" label="btn3" id="btn3"/>
?<s:Button x="131" y="373" label="Paint" click="TestPaint()"/>
?<s:Label x="176" y="169" text="LblMiddle" width="71" height="20" id="lbl"/>
?<s:Button x="303" y="324" label="Move" click="Testmove()"/>
?<s:Button x="132" y="466" label="MovePaint" click="moveprint()"/>
?<s:TextArea x="328" y="426" width="318" height="222" id="textArea" mouSEOver="txtMouSEOver(event)" mouSEOut="txtMouSEOut(event)"/>
?<s:TextArea x="682" y="426" height="223" id="txt2"/>

</s:View> ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读