带延时加载,自动刷新和拖动控制的Canvas

来源:互联网 发布:贵州大数据企业排名 编辑:程序博客网 时间:2024/05/22 10:25

  Canvas应该是大家在开发flex接触比较多控件吧,但是它的功能比较单一,很多人都要给予2次开发,我也是一样,为了做好延时的flex控件异步加载。一个巨大的flex整体切割成为很多很小很小的swf,就要做了一个延时加载。每个Canvas都有。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"  
  3.            initialize="loadInit()"  
  4.            backgroundColor="0xffffff">  
  5.     <mx:Script>  
  6.         <!--[CDATA[  
  7.             import mx.controls.Image;  
  8.             import mx.controls.Alert;  
  9.   
  10.             import flash.utils.setTimeout;  
  11.   
  12.             import org.bytearray.gif.player.GIFPlayer;  
  13.   
  14.             //自动刷新   
  15.             public var autoRefreshPolicy="off";  
  16.             public var autoRefreshTime:String="60000";  
  17.             public var autoRefreshMethod:Function;  
  18.   
  19.             private var loading:Image;  
  20.             private var myGIFPlayer:GIFPlayer=new GIFPlayer(true);  
  21.   
  22.             public var gifUrl:String="resource/image/config/canvasLoading.gif";  
  23.   
  24.             //移动性   
  25.             private var oldIndex:int=0;  
  26.             public var moveable:Boolean=false;  
  27.   
  28.             public function loadInit():void  
  29.             {  
  30.                 loading=new Image;  
  31.                 loading.width=50;  
  32.                 loading.height=50;  
  33.                 loading.x=0;  
  34.                 loading.y=0;  
  35.   
  36.                 var request:URLRequest=new URLRequest(gifUrl);  
  37.                 myGIFPlayer.load(request);  
  38.                 myGIFPlayer.visible=true;  
  39.   
  40.                 loading.addChild(myGIFPlayer);  
  41.                 this.addChild(loading);  
  42.                 this.enabled=false;  
  43.   
  44.                 if (autoRefreshPolicy == "on")  
  45.                 {  
  46.                     setTimeout(autoRefresh, 800);  
  47.                 }  
  48.   
  49.                 this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, dragBegin);  
  50.                 this.addEventListener(flash.events.MouseEvent.MOUSE_UP, dragEnd);  
  51.             }  
  52.   
  53.             public function autoRefresh():void  
  54.             {  
  55.                 var time:Number=Number(autoRefreshTime) * 1000;  
  56.                 var timer:Timer=new Timer(time);  
  57.                 timer.addEventListener(TimerEvent.TIMER, onTimer);  
  58.                 timer.start();  
  59.             }  
  60.   
  61.   
  62.             public function onTimer(event:TimerEvent):void  
  63.             {  
  64.                 if (autoRefreshMethod != null)  
  65.                 {  
  66.                     autoRefreshMethod.call(this);  
  67.                 }  
  68.                 else  
  69.                 {  
  70.                     Alert.show("自动刷新函数为空");  
  71.                 }  
  72.             }  
  73.   
  74.   
  75.             public function startLoading():void  
  76.             {  
  77.                 loading.visible=true;  
  78.                 this.enabled=false;  
  79.             }  
  80.   
  81.             public function completeLoading():void  
  82.             {  
  83.                 loading.visible=false;  
  84.                 this.enabled=true;  
  85.             }  
  86.   
  87.             private function dragBegin(event:MouseEvent):void  
  88.             {  
  89.                 if (moveable == true)  
  90.                 {  
  91.                     oldIndex=this.parent.getChildIndex(this);  
  92.                     this.parent.setChildIndex(thisthis.parent.numChildren - 1);  
  93.                     this.startDrag(false);  
  94.                 }  
  95.             }  
  96.   
  97.             private function dragEnd(event:MouseEvent):void  
  98.             {  
  99.                 if (moveable == true)  
  100.                 {  
  101.                     this.parent.setChildIndex(this, oldIndex);  
  102.                     this.stopDrag();  
  103.                 }  
  104.             }  
  105.   
  106.             //延迟执行   
  107.             protected function delayFunction(closure:Function, delay:Number, ... arguments):void  
  108.             {  
  109.                 setTimeout(closure, delay, arguments);  
  110.             }  
  111.         ]]-->  
  112.     </mx:Script>  
  113. </mx:Canvas>