티스토리 뷰
참조 : http://www.cubetray.com/custompreloader2/Source/index.html
----------------------------------------------------------------------------------------------------
[ CustomProgressBar.mxml ]
< ?xml version="1.0" encoding="utf-8"?>
< mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preloader="com.preloader.Preloader" viewSourceURL="Source/index.html">
<mx:Script>
<![CDATA[
[ Embed("file1.mp3") ]
public var file0:Class;
]]>
</mx:Script>
< /mx:Application>
----------------------------------------------------------------------------------------------------
[ com.preloader.Preloader.as ]
package com.preloader
{
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));
}
}
}
----------------------------------------------------------------------------------------------------
[ com.preloader.ProgressBar.as ]
package com.preloader
{
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.utils.ByteArray;
import mx.graphics.codec.PNGEncoder;
public class ProgressBar extends Loader
{
// Logo picture
[Embed(source="../../Images/Logo.png")]
[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 = 321; // Logo picture width
private static var LeftMargin: int = 1; // Left Margin
private static var RightMargin: int = 2; // 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 = 158; // Logo picture height
private static var ProgressHeight: int = 30; // Progress bar height
private static var ProgressBarBackground: uint = 0xFFFFFF;
private static var progressBarOuterBorder: uint = 0x323232;
private static var ProgressBarColor: uint = 0xB5D8FB;
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;
graph.drawRoundRect(px, py, containerWidth, ProgressHeight, 6);
containerWidth -= 4;
var progressWidth: Number = containerWidth * m_Progress / 100;
graph.beginFill(ProgressBarColor);
graph.lineStyle(1, ProgressBarInnerColor, 1, true);
graph.drawRoundRect(px + 2, py + 2, progressWidth, ProgressHeight - 4, 6);
//Construct the Text Field Object and put the progress value in it
var textField: TextField = new TextField();
textField.htmlText = "<b>" + m_Progress.toFixed(0) + "%</b>";
//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) / 2, py + (ProgressHeight - textBitmapData.height) / 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);
//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;
}
}
}
'Language > Flex' 카테고리의 다른 글
[ Flex ][ UIComponent ] TextInput (0) | 2015.11.29 |
---|---|
[ Flex ][ UIComponent ] Button (0) | 2015.11.29 |
[ Flex ][ Source ] Control mxml (0) | 2015.11.29 |
[ Flex ][ Source ] MouseEventMOUSE_WHEEL 포커스 버그 (0) | 2015.11.29 |
[ Flex ][ Source ] 다중 파일 다운로드 / 업로드 (0) | 2015.11.29 |