Canvas应该是大家在开发flex接触比较多控件吧,但是它的功能比较单一,很多人都要给予2次开发,我也是一样,为了做好延时的flex控件异步加载。一个巨大的flex整体切割成为很多很小很小的swf,就要做了一个延时加载。每个Canvas都有。
代码如下:
import flash.utils.setTimeout;
import org.bytearray.gif.player.GIFPlayer;
//自动刷新
public var autoRefreshPolicy="off";
public var autoRefreshTime:String="60000";
public var autoRefreshMethod:Function;
private var loading:Image;
private var myGIFPlayer:GIFPlayer=new GIFPlayer(true);
public var gifUrl:String="resource/image/config/canvasLoading.gif";
//移动性
private var oldIndex:int=0;
public var moveable:Boolean=false;
public function loadInit():void
{
loading=new Image;
loading.width=50;
loading.height=50;
loading.x=0;
loading.y=0;
var request:URLRequest=new URLRequest(gifUrl);
myGIFPlayer.load(request);
myGIFPlayer.visible=true;
loading.addChild(myGIFPlayer);
this.addChild(loading);
this.enabled=false;
if (autoRefreshPolicy == "on")
{
setTimeout(autoRefresh, 800);
}
this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, dragBegin);
this.addEventListener(flash.events.MouseEvent.MOUSE_UP, dragEnd);
}
public function autoRefresh():void
{
var time:Number=Number(autoRefreshTime) * 1000;
var timer:Timer=new Timer(time);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
public function onTimer(event:TimerEvent):void
{
if (autoRefreshMethod != null)
{
autoRefreshMethod.call(this);
}
else
{
Alert.show("自动刷新函数为空");
}
}
public function startLoading():void
{
loading.visible=true;
this.enabled=false;
}
public function completeLoading():void
{
loading.visible=false;
this.enabled=true;
}
private function dragBegin(event:MouseEvent):void
{
if (moveable == true)
{
oldIndex=this.parent.getChildIndex(this);
this.parent.setChildIndex(this, this.parent.numChildren - 1);
this.startDrag(false);
}
}
private function dragEnd(event:MouseEvent):void
{
if (moveable == true)
{
this.parent.setChildIndex(this, oldIndex);
this.stopDrag();
}
}
//延迟执行
protected function delayFunction(closure:Function, delay:Number, ... arguments):void
{
setTimeout(closure, delay, arguments);
}
]]-->
</mx:Script>
</mx:Canvas>
截图: