用位图填充的方式取续列图进行划分, 也是一种位图切割,我们可以通过该原理来对单张图片进行角色动作化处理 , 不过有时候使用scrollRect 性能更佳。

AS3代码
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.events.IOErrorEvent;
- import flash.system.System;
- import flash.geom.Rectangle;
- import flash.geom.Point;
-
-
-
-
- public class QBitmapData
- {
-
- private var bufferContainer:DisplayObjectContainer;
- private var imagePath:String;
- private var bitmapArray:Array;
- private var _loader:Loader;
- private var size:Number;
- private var rowNum:uint;
- private var listNum:uint;
- private var count:uint;
- public var callBack:Function;
- public function QBitmapData(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {
-
- bufferContainer = _bufferContainer;
- this.ImagePath = imagePath;
- this.size = size;
- }
- public function set ImagePath(_path:String):void {
-
- var urlRequest:URLRequest = new URLRequest(_path);
- this._loader = new Loader();
-
-
- this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
-
- this._loader.load(urlRequest);
- }
-
-
- private function completeHandler(event:Event):void {
-
- var target:DisplayObject = _loader.content as DisplayObject;
-
- var xpos:Number = -this.size;
- var ypos:Number = 0;
- var bmd:BitmapData = new BitmapData(target.width, target.height, true, 0);
- bmd.draw(target);
- this._loader.unload();
- this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
-
- this.rowNum = Math.floor(bmd.width / this.size);
- this.listNum = Math.floor(bmd.height / this.size);
- this.count = this.rowNum * this.listNum;
-
- bitmapArray = new Array(count);
-
- var rec:Rectangle = new Rectangle(0, 0, this.size, this.size);
- var p:Point = new Point(0, 0);
- for (var i:Number = 0; i < count; i++) {
- var localBMD:BitmapData = new BitmapData(this.size, this.size);
- if (xpos == (bmd.width - this.size)) {
- ypos += this.size;
- xpos = -this.size;
- }
- xpos += this.size;
- rec.x = xpos;
- rec.y = ypos;
- localBMD.copyPixels(bmd, rec, p);
- this.bitmapArray[i] = localBMD;
- }
-
-
- bmd.dispose();
-
- this.callBack(this.bitmapArray,this.Index(0));
- }
-
- public function Index(index:Number):BitmapData {
- if (this.bitmapArray.length<=index) {
- throw new Error("超过索引");
- }
- return this.bitmapArray[index];
- }
-
- public function get BitmapArray():Array {
- return this.bitmapArray;
- }
-
- public function get RowNum():uint {
- return this.rowNum;
- }
- public function get ListNum():uint {
- return this.listNum;
- }
- public function get Count():uint {
- return this.count;
- }
- public function toString() {
- return "lbynet bitamp array";
- }
-
-
- }
-
- }
AS3代码
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.events.IOErrorEvent;
- import flash.system.System;
- import flash.geom.Rectangle;
- import flash.geom.Point;
-
-
-
-
- public class QScrollRect
- {
-
- private var bufferContainer:DisplayObjectContainer;
- private var imagePath:String;
- private var rectArray:Array;
- private var _loader:Loader;
- private var size:Number;
- private var rowNum:uint;
- private var listNum:uint;
- private var count:uint;
- public var callBack:Function;
- public function QScrollRect(imagePath:String, size:Number=16,_bufferContainer:DisplayObjectContainer = null):void {
-
- bufferContainer = _bufferContainer;
- this.ImagePath = imagePath;
- this.size = size;
- }
- public function set ImagePath(_path:String):void {
-
- var urlRequest:URLRequest = new URLRequest(_path);
- this._loader = new Loader();
- this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
- this._loader.load(urlRequest);
- }
-
-
- private function completeHandler(event:Event):void {
-
- var target:DisplayObject = _loader.content as DisplayObject;
-
-
- var xpos:Number = -this.size;
- var ypos:Number = 0;
- var bmd:BitmapData = new BitmapData(target.width, target.height, true, 0);
- bmd.draw(target);
- this._loader.unload();
- this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
-
- this.rowNum = Math.floor(bmd.width / this.size);
- this.listNum = Math.floor(bmd.height / this.size);
- this.count = this.rowNum * this.listNum;
-
- this.rectArray = new Array(count);
- var rec:Rectangle;
- var p:Point = new Point(0, 0);
- for (var i:Number = 0; i < count; i++) {
- rec = new Rectangle(0, 0, this.size, this.size);
- if (xpos == (bmd.width - this.size)) {
- ypos += this.size;
- xpos = -this.size;
- }
- xpos += this.size;
- rec.x = xpos;
- rec.y = ypos;
- this.rectArray[i] = rec;
- }
-
- this.callBack(bmd,this.Index(0));
-
-
- }
-
- public function Index(index:Number):Rectangle {
-
- if (this.rectArray.length <= index) {
- throw new Error("超过索引");
- }
- return this.rectArray[index];
- }
-
- public function get RectArray():Array {
-
- return this.rectArray;
- }
- public function get RowNum():uint {
- return this.rowNum;
- }
- public function get ListNum():uint {
- return this.listNum;
- }
- public function get Count():uint {
- return this.count;
- }
- public function toString() {
- return "lbynet bitamp array";
- }
-
-
- }
-
- }
上面类我们主要存储BitmapData数据,如果是用ScrollRect,那我们存储的是Rectangle数据。
下面是测试:
这个代码高度样式也中文了吧!行号都是中文的,看着难受。
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。