Flex 界面初始化 自定义 预加载 类!
说明:
自定义界面初始化过程提示:初始化...,初始化完毕,加载完毕!
ZPreloader.as
package com.command { import flash.display.Graphics; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.ProgressEvent; import flash.text.TextField; import flashx.textLayout.formats.Float; import mx.controls.Alert; import mx.core.FlexGlobals; import mx.events.CloseEvent; import mx.events.FlexEvent; import mx.managers.PopUpManager; import mx.preloaders.DownloadProgressBar; import spark.components.Application; public class ZPreloader extends DownloadProgressBar { private var progress:TextField; private var _preloader:Sprite; public function ZPreloader() { super(); progress = new TextField(); progress.width = 200; progress.height = 45; // progress.x = 300; // progress.y = 300; addChild(progress); } private function centerPreloader():void { if ((stage.stageWidth > 0) && (_preloader)) { x = (stage.stageWidth / 2) - (progress.width / 2); y = (stage.stageHeight / 2) - (progress.height / 2); _preloader.visible = true; } } override public function set preloader(s:Sprite):void { _preloader = s; _preloader.addEventListener(ProgressEvent.PROGRESS, progHandler); _preloader.addEventListener(Event.COMPLETE, compHandler); _preloader.addEventListener(FlexEvent.INIT_COMPLETE, initCompHandler); _preloader.addEventListener(FlexEvent.INIT_PROGRESS, initProgHandler); } private function progHandler(event:ProgressEvent):void { // var wid:Number = Application(FlexGlobals.topLevelApplication).width; // var hgt:Number = Application(FlexGlobals.topLevelApplication).height; centerPreloader(); var procNum:Number = event.bytesLoaded / event.bytesTotal; var curP:Number = procNum * 200; progress.text = int(procNum * 100) + "%"; var g:Graphics = this.graphics; //绘图区域 g.clear(); g.beginFill(0x88ff22); g.drawRect(0,20, 200, 10); g.endFill(); g.beginFill(0x5085D6); g.drawRect(0,20, curP, 10); g.endFill(); } private function compHandler(event:Event):void { progress.text = "加载完毕"; } private function initCompHandler(event:FlexEvent):void { progress.text = "初始化完毕"; dispatchEvent(new Event(Event.COMPLETE)); } private function initProgHandler(event:FlexEvent):void { progress.text = "初始化..."; } } }
使用方法:
<!-- 在Application根节点添加属性 --> preloader="com.command.ZPreloader"