/src/com/macro/gUI/ani/MovieClipSerials.as

https://github.com/macrohard/gUI
ActionScript | 87 lines | 49 code | 16 blank | 22 comment | 3 complexity | 76b5e37607f25b77a82060e6a13bf5d4 MD5 | raw file
  1. package com.macro.gUI.ani
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.MovieClip;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.geom.Matrix;
  9. import flash.geom.Point;
  10. import flash.geom.Rectangle;
  11. /**
  12. * 矢量动画MovieClip转位图序列化
  13. * @author Macro <macro776@gmail.com>
  14. *
  15. */
  16. public class MovieClipSerials
  17. {
  18. private var _mc:MovieClip;
  19. private var _frameNum:int;
  20. private var _scale:Point;
  21. private var _detectWidth:int;
  22. private var _detectHeight:int;
  23. private var _serials:Vector.<Frame>;
  24. /**
  25. * MovieClip转位图序列化。
  26. * 由于可能使用了滤镜、嵌套等方式,因此在初期一次性获取不可行,需要在运行时逐帧处理,
  27. * 一旦取得完整的帧后,MovieClip对象将被释放
  28. * @param mc 用于转换的动画影片剪辑
  29. * @param frameNum 总帧数。由于MovieClip可能嵌套,自动探测帧数不可行。
  30. * @param scale 缩放比,默认原大
  31. * @param detectWidth 探测宽度,默认值200
  32. * @param detectHeight 探测高度,默认值200
  33. *
  34. */
  35. public function MovieClipSerials(mc:MovieClip, frameNum:int, scale:Point = null, detectWidth:int = 200, detectHeight:int = 200)
  36. {
  37. _mc = mc;
  38. _frameNum = frameNum;
  39. _serials = new Vector.<Frame>(frameNum, true);
  40. _scale = (scale == null ? new Point(1, 1) : scale);
  41. _detectWidth = detectWidth;
  42. _detectHeight = detectHeight;
  43. }
  44. /**
  45. * 获取对应帧的位图对象
  46. * @param playHead 播放头位置,从1开始计数
  47. * @return
  48. *
  49. */
  50. public function getFrame(playHead:int):Frame
  51. {
  52. var f:int = playHead % _frameNum;
  53. var temp:BitmapData = new BitmapData(_detectWidth, _detectHeight, true, 0);
  54. var m:Matrix = new Matrix(_scale.x, 0, 0, _scale.y);
  55. var r:Rectangle;
  56. var frame:Frame = _serials[f];
  57. if (frame == null)
  58. {
  59. _mc.gotoAndStop(f);
  60. temp.fillRect(temp.rect, 0);
  61. temp.draw(_mc, m);
  62. r = temp.getColorBoundsRect(0xFF000000, 0x00000000, false);
  63. frame = new Frame(r.width, r.height);
  64. frame.copyPixels(temp, r, new Point(), null, null, true);
  65. frame.offsetPoint = r.topLeft;
  66. _serials[f] = frame;
  67. }
  68. return frame;
  69. }
  70. }
  71. }