x.mxml
<?xml version="1.0" encoding="utf-8"?>
<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"
preloader="LoadingExampleProgressBar"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
</s:Application>
ProgressBar.as
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.utils.ByteArray;
import mx.controls.Label;
import mx.graphics.codec.PNGEncoder;
public class ProgressBar extends Loader
{
// Logo picture
[Bindable]
private var logoClass: Class;
private var Logo: Bitmap;
private var m_Ready: Boolean;
private var m_Progress: Number;
private var m_BitmapData: BitmapData;
// Constant to change to fit your own layout
private static var ProgressWidth: int = 150; // Progress bar width
private static var PictureWidth: int = 48; // Logo picture width
private static var LeftMargin: int = 1; // Left Margin
private static var RightMargin: int = 1; // Right Margin
private static var Spacing: int = 10; // Spacing between logo and progress bar
private static var TopMargin: int = 1; // Top Margin
private static var BottomMargin: int = 1; // Bottom Margin
private static var PictureHeight: int = 48; // Logo picture height
private static var ProgressHeight: int = 12; // Progress bar height
private static var ProgressBarBackground: uint = 0xFFFFFF;
private static var progressBarOuterBorder: uint = 0x323232;
private static var ProgressBarColor: uint = 0xFF4707;
private static var ProgressBarInnerColor: uint = 0xFFFFFF;
public function ProgressBar(): void{
super();
m_Progress = 0;
Logo = new logoClass as Bitmap;
this.addEventListener(Event.RENDER, renderEventHandler);
}
private function renderEventHandler(event: Event): void{
}
public function refreshProgressBar(): void{
m_BitmapData = drawProgress(); // Create the bitmapdata object
var encoder: PNGEncoder = new PNGEncoder();
var byteArray: ByteArray = encoder.encode(m_BitmapData); // Encode the bitmapdata to a bytearray
this.loadBytes(byteArray); // Draw the bitmap on the loader object
}
public function getWidth(): Number{
return LeftMargin + PictureWidth + Spacing + ProgressWidth + RightMargin;
}
public function getHeight(): Number{
return TopMargin + PictureHeight + BottomMargin;
}
private function drawProgress(): BitmapData{
// Create the bitmap class object
var bitmapData: BitmapData = new BitmapData(getWidth(), getHeight(), true, 0);
// Draw the Progress Bar
var sprite: Sprite = new Sprite();
var graph: Graphics = sprite.graphics;
// Draw the progress bar background
graph.beginFill(ProgressBarBackground);
graph.lineStyle(1, progressBarOuterBorder, 1, true);
var containerWidth: Number = ProgressWidth;
var px: int = getWidth() - RightMargin - ProgressWidth;
var py: int = (getHeight() - ProgressHeight)*2/3;
graph.drawRoundRect(px, py, containerWidth, ProgressHeight, 0);
containerWidth -= 4;
var progressWidth: Number = containerWidth * m_Progress / 100;
graph.beginFill(ProgressBarColor);
graph.lineStyle(1, ProgressBarInnerColor, 1, true);
graph.drawRoundRect(px+1, py+1, progressWidth, ProgressHeight-2, 0);
//Construct the Text Field Object and put the progress value in it
var textField: TextField = new TextField();
//textField.background = true;
//textField.border = true;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0x000000;
format.size = 10;
//format.bold = true;
textField.defaultTextFormat = format;
textField.text = m_Progress.toFixed(0) + "%";
//Create a BitmapData object and take the Width and height of the TextField text.
var textBitmapData: BitmapData = new BitmapData(textField.textWidth + 5, textField.textHeight);
//Set the BitmapData object background color to the background color of the progress bar
textBitmapData.floodFill(0, 0, ProgressBarBackground);
//Draw the TextFiel object in the BitmapData object
textBitmapData.draw(textField);
//Construct the matrix object
//The matrix object is used to place the text
var textBitmapMatrix: Matrix = new Matrix();
textBitmapMatrix.translate(px + (containerWidth-textBitmapData.width) / 2, py + (ProgressHeight - textBitmapData.height) / 2-2);
var textField1: TextField = new TextField();
var format1:TextFormat = new TextFormat();
format1.color = 0x0b333c;
textField1.defaultTextFormat = format1;
textField1.text = "系统正在装载,请稍候";
textField1.autoSize = TextFieldAutoSize.LEFT;
//Create a BitmapData object and take the Width and height of the TextField text.
var textBitmapData1: BitmapData = new BitmapData(textField1.textWidth + 5, textField1.textHeight);
//Set the BitmapData object background color to the background color of the progress bar
textBitmapData1.floodFill(0, 0, ProgressBarBackground);
//Draw the TextFiel object in the BitmapData object
textBitmapData1.draw(textField1);
//Construct the matrix object
//The matrix object is used to place the text
var textBitmapMatrix1: Matrix = new Matrix();
textBitmapMatrix1.translate(px, 2);
//Draw the sprite object on the parent BitmapData
bitmapData.draw(sprite);
//Draw the text on the parent BitmapData
bitmapData.draw(textBitmapData, textBitmapMatrix, null, null, null, false);
bitmapData.draw(textBitmapData1, textBitmapMatrix1, null, null, null, false);
//Draw the Logo
bitmapData.draw(Logo.bitmapData, null, null, null, null, true);
return bitmapData;
}
public function set ready(value: Boolean): void{
m_Ready = value;
this.visible = !value;
}
public function get ready(): Boolean{
return m_Ready;
}
public function set progress(value: Number): void{
m_Progress = value;
}
public function get progress(): Number{
return m_Progress;
}
}
}
Preloader.as
package
{
import mx.preloaders.DownloadProgressBar;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.events.Event;
import mx.events.FlexEvent;
import mx.managers.SystemManager;
import mx.managers.BrowserManager;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Preloader extends DownloadProgressBar
{
public var m_Progress: ProgressBar;
private var m_Timer: Timer;
public function Preloader(): void{
super();
var browser: BrowserManager;
m_Progress = new ProgressBar;
this.addChild(m_Progress);
m_Timer = new Timer(1);
m_Timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
m_Timer.start();
}
override public function set preloader(value:Sprite):void{
value.addEventListener(ProgressEvent.PROGRESS, progressEventHandler);
value.addEventListener(Event.COMPLETE, completeEventHandler);
value.addEventListener(FlexEvent.INIT_PROGRESS, initProgressEventHandler);
value.addEventListener(FlexEvent.INIT_COMPLETE, initCompleteEventHandler);
}
private function progressEventHandler(event: ProgressEvent): void{
var progress: Number = event.bytesLoaded / event.bytesTotal * 100;
if (m_Progress){
m_Progress.progress = progress;
}
}
private function timerEventHandler(event: TimerEvent): void{
this.stage.addChild(this);
var width: Number = this.stage.stageWidth * 40 / 100; // Get 40% for the Stage width
// Set the Position of the Progress bar to the middle of the screen
m_Progress.x = (this.stage.stageWidth - m_Progress.getWidth()) / 2;
m_Progress.y = (this.stage.stageHeight - m_Progress.getHeight()) / 2;
m_Progress.refreshProgressBar();
}
private function completeEventHandler(event: Event): void{
var i: int = 0;
}
private function initProgressEventHandler(event: FlexEvent): void{
var i: int = 0;
}
private function initCompleteEventHandler(event: FlexEvent): void{
m_Progress.ready = true;
m_Timer.stop();
this.dispatchEvent(new Event(Event.COMPLETE));
}
}
}
LoadingExampleProgressBar.as
package
{
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 LoadingExampleProgressBar extends DownloadProgressBar
{
private var logo:Loader;
private var txt:TextField;
private var _preloader:Sprite;
public function LoadingExampleProgressBar()
{
logo = new Loader();
logo.load(new URLRequest("essential_flex2.jpg"));
addChild(logo);
var style:TextFormat = new TextFormat(null,null,0xFFFFFF,null,null,null,null,null,"center");
txt = new TextField();
txt.defaultTextFormat = style;
txt.width = 200;
txt.selectable = false;
txt.height = 20;
addChild(txt);
super();
}
//最重要的代码就在这里..重写preloader,让swf执行加载的时候~进行你希望的操作~
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 - 40)/2;
logo.y = (stage.stageHeight - 80)/2;
txt.x = (stage.stageWidth - 200)/2;
txt.y = logo.y + 40+5;
graphics.clear();
graphics.beginFill(0x333333);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
graphics.endFill();
}
private function load_progress(e:ProgressEvent):void{
txt.text = "正在加载..."+int(e.bytesLoaded/e.bytesTotal*100)+"%";
}
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()
//最后这个地方需要dpe一个Event.COMPLETE事件..表示加载完毕让swf继续操作~
dispatchEvent(new Event(Event.COMPLETE))
}
}
}