How to Create a Bitmap(Matrix Graphics) Progress Bar in Flex

lime-apple-128x128 Usually we could use percentage or progress bar to show how the progress going on. But some cases such as the file divided into some same size of the block and deal with these blocks. Then using matrix graphics to indicate the progress is very direct-viewing. Following let’s try to do it.

First let’s see the screen shot of the program or run the Demo (switch effect by click)

Search-256x256 Demo | DownloadDownload Full Project

clip_image002 

Program Outline: Package single small icon as Class and Control these icon by relevant Then realize the switch function in different status.

First prepare one small icon.

We could create icon by Flash CS3

New File KUI.fla, new add MovieClip name “Cell” in DB.

clip_image004

Then draw following graph in work area.

clip_image006

Add highlight effect:

clip_image008

Ok, now one of the icon statuses came out. We name it as “default”.

Then add more statuses in film frame:

Definite blue status as “start”

clip_image010

Definite Green status as “success”

clip_image012

Definite Yellow status as “warning”

clip_image014

Definite Red status as “error”

clip_image016

We add “Stop();” in first frame to avoid film auto playing.

Write frame tag for every frame according the name of status:

clip_image018

Then, find Cell in library panel and export as SWC

clip_image020

Ok, now we have finished the work of icon. Open the Flex Builder 3, create new project KMatrix.

clip_image022

Finished, create new folder lib. Move Cell.swc into lib:

clip_image024

Open the project property panel, click ActionScript Build Path, switch to Library path, click Add SWC button, select the Cell.swc in pop-up panel:

clip_image026

Ok, as the following image:

clip_image028

Then we have add the resource into the project. Next we will go to the step of program.

New add document com/kingnare/controls, and new add class MyMatrix in it.

clip_image030

Control the Internal multi-cell by the class MyMatrix

Code as the following ( view the source file )

  1. package com.kingnare.controls
  2. {
  3. import flash.display.Sprite;
  4.  
  5. public class MyMatrix extends Sprite
  6. {
  7. private var list:Array;
  8. private var maxWidth:uint;
  9. private var maxHeight:uint;
  10. private var _columnNum:uint;
  11. private var _hDis:uint;
  12. private var _vDis:uint;
  13.  
  14. public function MyMatrix()
  15. {
  16. super();
  17.  
  18. _hDis = 1;
  19. _vDis = 1;
  20. _columnNum = 50;
  21. maxWidth = 1;
  22. maxHeight = 1;
  23. }
  24.  
  25. private function create(num:uint):void
  26. {
  27. if(num < 0) return;
  28. list = [];
  29.  
  30. for(var i:uint = 0;i<num;i++)
  31. {
  32.  
  33. var cell:Cell = new Cell();
  34. list.push(cell);
  35. }
  36.  
  37. updateLayout();
  38. }
  39.  
  40. private function updateLayout():void
  41. {
  42. var wNum:uint = 0;
  43. var hNum:uint = 0;
  44. var beginX:int = 0;
  45. var beginY:int = 0;
  46. var len:int = list.length;
  47.  
  48. for(var i:uint = 0;i<len;i++)
  49. {
  50. var cell:Cell = list[i] as Cell;
  51.  
  52. if(cell)
  53. {
  54. cell.x = beginX+wNum*(cell.width+_hDis);
  55. cell.y = beginY+hNum*(cell.height+_vDis);
  56.  
  57. if(wNum >= _columnNum-1)
  58. {
  59. wNum = 0;
  60. hNum++;
  61. }
  62. else
  63. {
  64. wNum++;
  65. }
  66. }
  67. }
  68.  
  69. maxWidth = _columnNum*(list[0].width + _hDis);
  70. maxHeight = (hNum+1)*(list[0].height + _vDis);
  71. drawMatrix();
  72. }
  73.  
  74. private function drawMatrix():void
  75. {
  76. var i:uint = 0;
  77.  
  78. while (list && i < list.length)
  79. {
  80. var cell:Cell = list[i] as Cell;
  81.  
  82. if(!this.contains(cell))
  83. {
  84. addChild(cell);
  85. }
  86.  
  87. i++;
  88. }
  89. }
  90.  
  91. public function clear():void
  92. {
  93. while(this.numChildren>0)
  94. {
  95. this.removeChildAt(0);
  96. }
  97.  
  98. while(list && list.length>0)
  99. {
  100. var cell:Cell = list.pop() as Cell;
  101. cell = null;
  102. }
  103. }
  104.  
  105. public function set count(value:uint):void
  106. {
  107. if(!isNaN(value))
  108. {
  109. clear();
  110.  
  111. if(value > 0)
  112. {
  113. create(value);
  114. }
  115. }
  116. }
  117.  
  118. public function get count():uint
  119. {
  120. return list.length;
  121. }
  122.  
  123. public function set columnNum(value:uint):void
  124. {
  125. _columnNum = value;
  126. updateLayout();
  127. }
  128.  
  129. public function get columnNum():uint
  130. {
  131. return _columnNum;
  132. }
  133.  
  134. public function set hDis(value:uint):void
  135. {
  136. _hDis = value;
  137. updateLayout();
  138. }
  139.  
  140. public function get hDis():uint
  141. {
  142. return _hDis;
  143. }
  144.  
  145. public function set vDis(value:uint):void
  146. {
  147. _vDis = value;
  148. updateLayout();
  149. }
  150.  
  151. public function get vDis():uint
  152. {
  153. return _vDis;
  154. }
  155. }
  156. }

Using create to new add Cell and save into the list. Layout it by updateLayout and add the Cell into the show list by drawMatrix.

Then we add test code into the main program KMatrix.

  1. package {
  2. import com.kingnare.controls.MyMatrix;
  3. import flash.display.Sprite;
  4. import flash.display.StageAlign;
  5. import flash.display.StageScaleMode;
  6.  
  7. [SWF(width="700", height="2000", backgroundColor="#333333", framerate="30")]
  8.  
  9. public class KMatrix extends Sprite
  10. {
  11.  
  12. public function KMatrix()
  13. {
  14. stage.scaleMode = StageScaleMode.NO_SCALE;
  15. stage.align = StageAlign.TOP_LEFT;
  16.  
  17. var m:MyMatrix = new MyMatrix();
  18.  
  19. addChild(m);
  20.  
  21. m.count = 300;
  22. m.columnNum = 50;
  23. m.x = 50;
  24. m.y = 50;
  25. }
  26. }
  27. }

We got the result after running:

clip_image032

It is 12 line and 50, good performance and low occupy.

Add the number to 10000, output normally but the CPU will fluctuate in a range and the value will be increase a lot compared with previous. This is because a large number of Cell objects respond to the default event. Then following we will introduce how to solve this problem by using of bitmap, and this is also the focus of this article.

Don’t add these Cell objects into the show list, but mapping these Cell objects as a bitmap. When one Cell object updated, we re-map this Cell object’s bitmap area.

In this way, the thousands of Cell could be deal with as a bitmap.

Then let’s revise the MyMatrix code:

First declare the variable:

  1. private var bitmap:Bitmap;
  2. private var bitmapData:BitmapData;
  3. private var matrix:Matrix;

Do initialization in the Constructor

  1. matrix = new Matrix();
  2.  
  3. bitmapData = new BitmapData(1, 1, true, 0);
  4. bitmap = new Bitmap(bitmapData);
  5.  
  6. addChild(bitmap);

Update the draw method, draw all the cell objects.

  1. private function drawMatrix():void
  2. {
  3. bitmapData = new BitmapData(maxWidth, maxHeight, true, 0×00FFFFFF);
  4.  
  5. var i:uint = 0;
  6.  
  7. while (list && i < list.length)
  8. {
  9. matrix.tx = list[i].x;
  10. matrix.ty = list[i].y;
  11.  
  12. bitmapData.draw(list[i], matrix);
  13. i++;
  14. }
  15.  
  16. bitmap.bitmapData = bitmapData;
  17. }

How to control the status of the Cell? We need add two methode.

  1. public function setStatus(number:uint, status:String):void
  2. {
  3.  
  4. if(number < list.length)
  5. {
  6. var child:Cell = list[number] as Cell;
  7.  
  8. if(child)
  9. {
  10. child.gotoAndStop(status);
  11.  
  12. matrix.tx = child.x;
  13. matrix.ty = child.y;
  14.  
  15. bitmapData.draw(child, matrix);
  16. }
  17. }
  18. }
  19.  
  20. public function getStatus(number:uint):String
  21. {
  22. if(number < list.length)
  23. {
  24. var child:Cell = list[number] as Cell;
  25.  
  26. if(child)
  27. {
  28. return child.currentFrameLabel;
  29. }
  30. }
  31.  
  32. return MyMatrix.DEFAULT_FRAME;
  33. }

Click here to see the Downloadfull source of MyMatrix.

Ok, now let’s do the test. Revise the main project.

  1. var m:MyMatrix = new MyMatrix();
  2.  
  3. addChild(m);
  4.  
  5. m.count = 10000;
  6. m.columnNum = 50;
  7.  
  8. m.x = 50;
  9. m.y = 50;
  10.  
  11. m.setStatus(0, MyMatrix.DEFAULT_FRAME);
  12. m.setStatus(1, MyMatrix.START_FRAME);
  13. m.setStatus(2, MyMatrix.SUCCESS_FRAME);
  14. m.setStatus(3, MyMatrix.ERROR_FRAME);
  15. m.setStatus(4, MyMatrix.WARNING_FRAME);

Run the result:

clip_image034

Even if fast move in the matrix, the occupy of the Mouse & CPU is low.

We also could let it dancing.

Click here to see the Downloadfull source of KMatrix

Then the finial running result as following:

clip_image002[1]

Now it could be used as the progress bar for a project. In this tutorial we didn’t introduce how to add the scrollbar. I want to leave this function to you own. :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

3 Responses to “How to Create a Bitmap(Matrix Graphics) Progress Bar in Flex”

  1. [...] 本文作者:auzn  首发英文版:How to Create a Bitmap(Matrix Graphics) Progress Bar in Flex [...]

  2. [...] How to Create a Bitmap(Matrix Graphics) Progress Bar in Flex [...]

  3. I appreciate so much your effort, thnks

Leave a Reply