播放位图

 

package com.xlands.avatar
{

	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import org.aswing.AsWingManager;
	import org.aswing.JFrame;
	
	public class BitmapDataExample extends Sprite {
		
		private var url:String = "img.png";
		
		private var _timer:Timer = new Timer(250, 0);;
		
		private var _bitMap:Bitmap = new Bitmap();
	
		private var _h_pos:int = 0;
		
		private var _v_pos:int  = 0;
		
		private var _playerBitmapManager:PlayerBitmapManager;
		
		private var _loader:Loader;
		
		private var _request:URLRequest;
		
		private var _frm:JFrame;
		
		/** 横向切割数 */
		private var _hNum:int;
		
		/** 纵向切割数 */
		private var _vNum:int;
		

		
		
		public function BitmapDataExample($hNum:int = 7, $vNum:int = 6) {
			
			AsWingManager.initAsStandard(this);
			
			_hNum = $hNum;
			_vNum = $vNum;
			
			
			_frm = new JFrame(null, "浏览");
			_frm.setSizeWH(200, 200);
			
			_playerBitmapManager = new PlayerBitmapManager(_hNum, _vNum);
			
			configureAssets();
		}
		
		
		private function configureAssets():void {
			_loader = new Loader();
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			_request = new URLRequest(url);
			
			_loader.load(_request);
		
		}
		
		
		private function completeHandler(event:Event):void {
		
			var loader:Loader = Loader(event.target.loader);
			var image:Bitmap = Bitmap(loader.content);
			
			_playerBitmapManager.addSpriteBitmap(url, image);
			
			
			_frm.getContentPane().addChild(_bitMap);
			_frm.show();
			
			
			_timer.addEventListener(TimerEvent.TIMER, __onTimerHandler);
			_timer.start();
			
			
			
		}

		private function __onTimerHandler(event:TimerEvent):void{
			
			if(_h_pos == _vNum){
				_timer.stop();
			}else{
			
				_bitMap.bitmapData = _playerBitmapManager.getSpriteBitmap("img.png", _h_pos, _v_pos);
				
				if(++_v_pos == _hNum){
					++_h_pos;
					_v_pos = 0;
				}
			}
			
		}
		
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("Unable to load image: " + url);
		}
	}

}

 其中用到的工具类PlayerBitmapManager.as

package com.xlands.avatar
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import org.aswing.util.HashMap;
	
	/**
	 * 人物形象图像数据管理者
	 * @author senkay
	 */ 
	public class PlayerBitmapManager
	{
		private var _hNum:int;
		
		private var _vNum:int;
		
		
		private static var bitmaps:HashMap;
		private static var bitNumMaps:HashMap;
		private static var localplayerPath:String;
		
		public function PlayerBitmapManager($hNum:int, $vNum:int){
			this._hNum = $hNum;
			this._vNum = $vNum;
		}
		
		
		private function getBitmaps():HashMap{
			if (bitmaps == null){
				bitmaps = new HashMap();
			}
			return bitmaps;
		}
		
		private function getBitNumMaps():HashMap{
			if (bitNumMaps == null){
				bitNumMaps = new HashMap();
			}
			return bitNumMaps;
		}
		
		public function hasSpriteBitmap(path:String):Boolean{
			return getBitmaps().containsKey(path);
		}
		
		public function addSpriteBitmapCout(path:String):void{
			getBitNumMaps().put(path,  (getBitNumMaps().get(path) as int)+1);
		}
		
		/**
		 * 加入人物形象数据
		 * @parm path 路径
		 * @bitmap 人物形象数据
		 */ 
		public function addSpriteBitmap(path:String, bitmap:Bitmap):void{
			var time:Number = getTimer();
			if (!getBitmaps().containsKey(path)){
				if (bitmap.width==0 || bitmap.height==0) return;
				var bitmapdata:BitmapData = bitmap.bitmapData;
				var sWidth:Number = Math.floor(bitmap.width/_hNum);
				var sHeight:Number =  Math.floor(bitmap.height/_vNum);
				var bitarray:Array = new Array();
				for (var i:int=0; i<_vNum; i++){
					var arr:Array = new Array();
					for (var j:int=0; j<_hNum; j++){
						if (i == 5 && j == 2){
							continue;
						}else{
							try{
								var bmd:BitmapData = new BitmapData(sWidth,sHeight,true,0x00FFFFFF);						
								bmd.copyPixels(bitmapdata, new Rectangle(sWidth*j, sHeight*i, sWidth, sHeight), new Point(0,0));
							}catch(e:Error){
								trace("the copyPixels is error:"+e);
								return;
							}
							arr.push(bmd);
						}				
					}
					bitarray.push(arr);
					getBitNumMaps().put(path, 1);
				}
				getBitmaps().put(path, bitarray);
			}else{
				getBitNumMaps().put(path,  (getBitNumMaps().get(path) as int)+1);
			}
			//trace("@@@@@@@@@@@@:"+(getTimer()-time));			
		}
		
		/**
		 * 根据形象路径和坐标返回形象数据
		 * @parm path 路径
		 * @parm h_pos,v_pos 数组坐标
		 * @return BitmapData 形象数据
		 */  
		public function getSpriteBitmap(path:String, h_pos:int, v_pos:int):BitmapData{
			if (getBitmaps().containsKey(path)){
				return (getBitmaps().get(path) as Array)[h_pos][v_pos] as BitmapData;
			}else{
				return null;
			}
		}
		
		/**
		 * 删除形象数据
		 * @parm path 形象路径
		 */ 
		public function delSpriteBitmap(path:String):void{
			if (getBitmaps().containsKey(path)){
				var num:int = getBitNumMaps().get(path);
				if (num > 1){
					getBitNumMaps().put(path,  num-1);
				}else{
					getBitmaps().remove(path);
					getBitNumMaps().remove(path);
				}
			}
		}
		
		/**
		 * 清除所有的形象数据
		 */ 
		public function clearAllSprite():void{
			var bitarray:Array = getBitmaps().get(localplayerPath);
			getBitmaps().clear();
			getBitNumMaps().clear();
			if (bitarray){
				getBitmaps().put(localplayerPath, bitarray);
				getBitNumMaps().put(localplayerPath, 1);
			}
		}
		
		/**
		 * 设置主人物的路径,用以清楚数据时候恢复
		 */ 
		public function setLocalPlayerPath(path:String):void{
			localplayerPath = path;
		}
		
		/**
		 * 查询形象数据状态
		 * @return String 状态
		 */
		public function getDebugInfo():String{
			return "形象数据状态:数量="+getBitmaps().keys().length;
		}
	}
}

 

 

你可能感兴趣的:(.net,Flash,J#)