📄 preloadprogressbar.as
字号:
*/
private function drawProgressBar(percentage:Number):void
{
var g:Graphics = _barSprite.graphics;
g.clear();
var colors:Array = progressBarColors;
var ratios:Array = [ 0, 0xFF ];
var matrix:Matrix = new Matrix();
// Middle
var barWidth:Number = _barRect.width * percentage / 100;
var barWidthSplit:Number = barWidth / 2;
var barHeight:Number = _barRect.height-4;
var barX:Number = calcX(_barRect.x);
var barY:Number = calcY(_barRect.y) + 2;
var barY2:Number;
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .39, .85],
ratios, matrix);
g.drawRect(barX, barY, barWidthSplit, barHeight);
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX + barWidthSplit, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [.85, 1.0],
ratios, matrix);
g.drawRect(barX + barWidthSplit, barY, barWidthSplit, barHeight);
// Outer highlight
barWidthSplit = barWidth / 3;
barHeight = _barRect.height;
barY = calcY(_barRect.y);
barY2 = barY + barHeight - 1;
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .05, .15],
ratios, matrix);
g.drawRect(barX, barY, barWidthSplit, 1);
g.drawRect(barX, barY2, barWidthSplit, 1);
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX + barWidthSplit, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .15, .25],
ratios, matrix);
g.drawRect(barX + barWidthSplit, barY, barWidthSplit, 1);
g.drawRect(barX + barWidthSplit, barY2, barWidthSplit, 1);
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX + barWidthSplit * 2, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .25, .1],
ratios, matrix);
g.drawRect(barX + barWidthSplit * 2, barY, barWidthSplit, 1);
g.drawRect(barX + barWidthSplit * 2, barY2, barWidthSplit, 1);
// Inner highlight
barWidthSplit = barWidth / 3;
barHeight = _barRect.height;
barY = calcY(_barRect.y) + 1;
barY2 = calcY(_barRect.y) + barHeight - 2;
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .15, .30],
ratios, matrix);
g.drawRect(barX, barY, barWidthSplit, 1);
g.drawRect(barX, barY2, barWidthSplit, 1);
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX + barWidthSplit, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .30, .40],
ratios, matrix);
g.drawRect(barX + barWidthSplit, barY, barWidthSplit, 1);
g.drawRect(barX + barWidthSplit, barY2, barWidthSplit, 1);
matrix.createGradientBox(barWidthSplit, barHeight,
0, barX + barWidthSplit * 2, barY);
g.beginGradientFill(GradientType.LINEAR, colors, [ .40, .25],
ratios, matrix);
g.drawRect(barX + barWidthSplit * 2, barY, barWidthSplit, 1);
g.drawRect(barX + barWidthSplit * 2, barY2, barWidthSplit, 1);
}
/**
* Updates the display of the download progress bar
* with the current download information.
* A typical implementation divides the loaded value by the total value
* and displays a percentage.
* If you do not implement this method, you should create
* a progress bar that displays an animation to indicate to the user
* that a download is occurring.
*
* <p>The <code>setProgress()</code> method is only called
* if the application is being downloaded from a remote server
* and the application is not in the browser cache.</p>
*
* @param completed Number of bytes of the application SWF file
* that have been downloaded.
*
* @param total Size of the application SWF file in bytes.
*/
protected function setProgress(completed:Number, total:Number):void
{
if (!isNaN(completed) &&
!isNaN(total) &&
completed >= 0 &&
total > 0)
{
_value = Number(completed);
_maximum = Number(total);
draw();
}
}
/**
* Returns the percentage value of the application loaded.
*
* @param loaded Number of bytes of the application SWF file
* that have been downloaded.
*
* @param total Size of the application SWF file in bytes.
*
* @return The percentage value of the loaded application.
*/
protected function getPercentLoaded(loaded:Number, total:Number):Number
{
var perc:Number;
if (loaded == 0 || total == 0 || isNaN(total) || isNaN(loaded))
return 0;
else
perc = 100 * loaded/total;
if (isNaN(perc) || perc <= 0)
return 0;
else if (perc > 99)
return 99;
else
return Math.round(perc);
}
/**
* @private
* Make the display class visible.
*/
private function show():void
{
_showingDisplay = true;
calcScale();
draw();
_displayTime = getTimer(); // Time when the display is shown.
}
/**
* @private
*/
private function hide():void
{
}
/**
* @private
*/
private function calcX(base:Number):Number
{
return base + _xOffset;
}
/**
* @private
*/
private function calcY(base:Number):Number
{
return base + _yOffset;
}
/**
* @private
* Figure out the scale for the display class based on the stage size.
* Then creates the children subcomponents.
*/
private function calcScale():void
{
if (stageWidth < 160 || stageHeight < 120)
{
scaleX = 1.0;
scaleY = 1.0;
}
else if (stageWidth < 240 || stageHeight < 150)
{
// Scale to appropriate size
createChildren();
var scale:Number = Math.min(stageWidth / 240.0,
stageHeight / 150.0);
scaleX = scale;
scaleY = scale;
}
else
{
createChildren();
}
}
/**
* Defines the algorithm for determining whether to show
* the download progress bar while in the download phase.
*
* @param elapsedTime number of milliseconds that have elapsed
* since the start of the download phase.
*
* @param event The ProgressEvent object that contains
* the <code>bytesLoaded</code> and <code>bytesTotal</code> properties.
*
* @return If the return value is <code>true</code>, then show the
* download progress bar.
* The default behavior is to show the download progress bar
* if more than 700 milliseconds have elapsed
* and if Flex has downloaded less than half of the bytes of the SWF file.
*/
protected function showDisplayForDownloading(elapsedTime:int,
event:ProgressEvent):Boolean
{
return elapsedTime > 700 &&
event.bytesLoaded < event.bytesTotal / 2;
}
/**
* Defines the algorithm for determining whether to show the download progress bar
* while in the initialization phase, assuming that the display
* is not currently visible.
*
* @param elapsedTime number of milliseconds that have elapsed
* since the start of the download phase.
*
* @param count number of times that the <code>initProgress</code> event
* has been received from the application.
*
* @return If <code>true</code>, then show the download progress bar.
*/
protected function showDisplayForInit(elapsedTime:int, count:int):Boolean
{
return elapsedTime > 300 && count == 2;
}
/**
* @private
*/
private function loadBackgroundImage(classOrString:Object):void
{
var cls:Class;
// The "as" operator checks to see if classOrString
// can be coerced to a Class
if (classOrString && classOrString as Class)
{
// Load background image given a class pointer
cls = Class(classOrString);
initBackgroundImage(new cls());
}
else if (classOrString && classOrString is String)
{
try
{
cls = Class(getDefinitionByName(String(classOrString)));
}
catch(e:Error)
{
// ignore
}
if (cls)
{
var newStyleObj:DisplayObject = new cls();
initBackgroundImage(newStyleObj);
}
else
{
// Loading the image is slightly different
// than in Loader.loadContent()... is this on purpose?
// Load background image from external URL
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(
Event.COMPLETE, loader_completeHandler);
loader.contentLoaderInfo.addEventListener(
IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
loader.load(new URLRequest(String(classOrString)), loaderContext);
}
}
}
/**
* @private
*/
private function initBackgroundImage(image:DisplayObject):void
{
addChildAt(image,0);
var backgroundImageWidth:Number = image.width;
var backgroundImageHeight:Number = image.height;
// Scale according to backgroundSize
var percentage:Number = calcBackgroundSize();
if (isNaN(percentage))
{
var sX:Number = 1.0;
var sY:Number = 1.0;
}
else
{
var scale:Number = percentage * 0.01;
sX = scale * stageWidth / backgroundImageWidth;
sY = scale * stageHeight / backgroundImageHeight;
}
image.scaleX = sX;
image.scaleY = sY;
// Center everything.
// Use a scrollRect to position and clip the image.
var offsetX:Number =
Math.round(0.5 * (stageWidth - backgroundImageWidth * sX));
var offsetY:Number =
Math.round(0.5 * (stageHeight - backgroundImageHeight * sY));
image.x = offsetX;
image.y = offsetY;
// Adjust alpha to match backgroundAlpha
if (!isNaN(backgroundAlpha))
image.alpha = backgroundAlpha;
}
/**
* @private
*/
private function calcBackgroundSize():Number
{
var percentage:Number = NaN;
if (backgroundSize)
{
var index:int = backgroundSize.indexOf("%");
if (index != -1)
percentage = Number(backgroundSize.substr(0, index));
}
return percentage;
}
//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------
/**
* Event listener for the <code>ProgressEvent.PROGRESS</code> event.
* This implementation updates the progress bar
* with the percentage of bytes downloaded.
*
* @param event The event object.
*/
protected function progressHandler(event:ProgressEvent):void
{
var loaded:uint = event.bytesLoaded;
var total:uint = event.bytesTotal;
var elapsedTime:int = getTimer() - _startTime;
// Only show the Loading phase if it will appear for awhile.
if (_showingDisplay || showDisplayForDownloading(elapsedTime, event))
{
if (!_startedLoading)
{
show();
label = downloadingLabel;
_startedLoading = true;
}
setProgress(event.bytesLoaded, event.bytesTotal);
}
}
/**
* Event listener for the <code>Event.COMPLETE</code> event.
* The default implementation does nothing.
*
* @param event The event object.
*/
protected function completeHandler(event:Event):void
{
}
/**
* Event listener for the <code>RSLEvent.RSL_PROGRESS</code> event.
* The default implementation does nothing.
*
* @param event The event object.
*/
protected function rslProgressHandler(event:RSLEvent):void
{
}
/**
* Event listener for the <code>RSLEvent.RSL_COMPLETE</code> event.
*
* @param event The event object.
*/
protected function rslCompleteHandler(event:RSLEvent):void
{
label = "Loaded library " + event.rslIndex + " of " + event.rslTotal;
}
/**
* Event listener for the <code>RSLEvent.RSL_ERROR</code> event.
* This event listner handles any errors detected when downloading an RSL.
*
* @param event The event object.
*/
protected function rslErrorHandler(event:RSLEvent):void
{
_preloader.removeEventListener(ProgressEvent.PROGRESS,
progressHandler);
_preloader.removeEventListener(Event.COMPLETE,
completeHandler);
_preloader.removeEventListener(RSLEvent.RSL_PROGRESS,
rslProgressHandler);
_preloader.removeEventListener(RSLEvent.RSL_COMPLETE,
rslCompleteHandler);
_preloader.removeEventListener(RSLEvent.RSL_ERROR,
rslErrorHandler);
_preloader.removeEventListener(FlexEvent.INIT_PROGRESS,
initProgressHandler);
_preloader.removeEventListener(FlexEvent.INIT_COMPLETE,
initCompleteHandler);
if (!_showingDisplay)
{
show();
_showingDisplay = true;
}
label = "RSL Error " + (event.rslIndex + 1) + " of " + event.rslTotal;
}
/**
* @private
* Helper function that dispatches the Complete event to the preloader.
*
* @param event The event object.
*/
private function timerHandler(event:Event = null):void
{
dispatchEvent(new Event(Event.COMPLETE));
}
/**
* Event listener for the <code>FlexEvent.INIT_PROGRESS</code> event.
* This implementation updates the progress bar
* each time the event is dispatched, and changes the text of the label.
*
* @param event The event object.
*/
protected function initProgressHandler(event:Event):void
{
var elapsedTime:int = getTimer() - _startTime;
_initProgressCount++;
if (!_showingDisplay &&
showDisplayForInit(elapsedTime, _initProgressCount))
{
_displayStartCount = _initProgressCount;
show();
}
else if (_showingDisplay)
{
if (!_startedInit)
{
// First init progress event.
_startedInit = true;
label = initializingLabel;
}
var loaded:Number = 100 * _initProgressCount /
(_initProgressTotal - _displayStartCount);
setProgress(loaded, 100);
}
}
/**
* @private
*/
private function initCompleteHandler(event:Event):void
{
var elapsedTime:int = getTimer() - _displayTime;
if (_showingDisplay && (elapsedTime < MINIMUM_DISPLAY_TIME))
{
var timer:Timer = new Timer(MINIMUM_DISPLAY_TIME - elapsedTime, 1);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
else
{
timerHandler();
}
}
/**
* @private
*/
private function loader_completeHandler(event:Event):void
{
var target:DisplayObject = DisplayObject(LoaderInfo(event.target).loader);
initBackgroundImage(target);
}
private function loader_ioErrorHandler(event:IOErrorEvent):void
{
// Swallow the error
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -