<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="application1_initializeHandler(event)"
preloader="com.wks.OverWriteLoadBar"
skinClass="com.wks.skins.MyApplicationSkin"
xmlns:widget="widget.*" xmlns:local="*">
</s:Application>
package com.wks
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.TextField;
import flash.text.TextFormat;
import mx.events.FlexEvent;
import mx.preloaders.DownloadProgressBar;
public class OverWriteLoadBar extends DownloadProgressBar
{
private var logo:Loader;
private var txt:TextField;
private var _preloader:Sprite;
public function OverWriteLoadBar()
{
logo = new Loader();
logo.load(new URLRequest("images/logo.png")); //其中中間顯示的圖片
addChild(logo);
var style:TextFormat = new TextFormat(null,null,0x000000,null,null,null,null,null,"center");
txt = new TextField(); //提示信息
txt.defaultTextFormat = style;
txt.width = 200;
txt.selectable = false;
txt.height = 20;
addChild(txt);
super();
}
override public function set preloader(value:Sprite):void{
_preloader = value
_preloader.addEventListener(ProgressEvent.PROGRESS,load_progress);
_preloader.addEventListener(Event.COMPLETE,load_complete);
_preloader.addEventListener(FlexEvent.INIT_PROGRESS,init_progress);
_preloader.addEventListener(FlexEvent.INIT_COMPLETE,init_complete);
stage.addEventListener(Event.RESIZE,resize)
resize(null);
}
private function remove():void{
_preloader.removeEventListener(ProgressEvent.PROGRESS,load_progress);
_preloader.removeEventListener(Event.COMPLETE,load_complete);
_preloader.removeEventListener(FlexEvent.INIT_PROGRESS,init_progress);
_preloader.removeEventListener(FlexEvent.INIT_COMPLETE,init_complete);
stage.removeEventListener(Event.RESIZE,resize)
}
private function resize(e:Event):void{
logo.x = (stage.stageWidth - 302)/2;
logo.y = (stage.stageHeight - 102)/2;
txt.x = (stage.stageWidth - 150)/2;
txt.y = logo.y + 40+102;
graphics.clear();
graphics.beginFill(0xEEFAFF); //加載時候的背景顏色
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight); //設置寬高,這裏設置和舞臺高寬一樣
graphics.endFill();
}
//四個階段,加載,加載完成,初始化,初始化完成。
private function load_progress(e:ProgressEvent):void{
var progress:Number = 0;
if ( e.bytesTotal > 0.1 )
{
// 張小寧注:這裏要防範被零除
progress = e.bytesLoaded/e.bytesTotal*100;
}
txt.text = "正在加載..." + String(int(progress)) + "%";
}
private function load_complete(e:Event):void{
txt.text = "加載完畢!"
}
private function init_progress(e:FlexEvent):void{
txt.text = "正在初始化..."
}
private function init_complete(e:FlexEvent):void{
txt.text = "初始化完畢!"
remove()
dispatchEvent(new Event(Event.COMPLETE))
}
}
}
/**控制application的高度和寬度只需要設置application下第一個ui的minheight和minwidth即可*/
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/11/03/adding-scroll-bars-to-an-spark-application-container-in-flex-4/ -->
<s:Skin name="MyApplicationSkin"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
alpha.disabledStates="0.5">
<!-- states -->
<s:states>
<s:State name="normal" stateGroups="normalStates" />
<s:State name="disabled" stateGroups="disabledStates" />
<s:State name="normalWithControlBar" stateGroups="normalStates" />
<s:State name="disabledWithControlBar" stateGroups="disabledStates" />
</s:states>
<fx:Metadata>
[HostComponent("spark.components.Application")]
</fx:Metadata>
<fx:Script fb:purpose="styling">
<![CDATA[
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
]]>
</fx:Script>
<!-- fill -->
<!---
A rectangle with a solid color fill that forms the background of the application.
The color of the fill is set to the Application's backgroundColor property.
-->
<s:Group left="0" right="0" top="0" bottom="0">
<s:layout>
<s:VerticalLayout gap="0" verticalAlign="middle" />
</s:layout>
<s:Scroller id="contentScroller" width="100%" height="100%">
<s:Group id="contentGroup" minWidth="0" minHeight="0" />
</s:Scroller>
</s:Group>
</s:Skin>