加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

BitmapScale9Grid【九宫格类】

发布时间:2020-12-15 18:17:20 所属栏目:百科 来源:网络整理
导读:项目中也在使用的九宫格类~~~ package{import flash.display.Sprite;import flash.display.Bitmap;import flash.display.BitmapData;import flash.geom.Rectangle;import flash.geom.Point;public class BitmapScale9Grid extends Sprite{private var source

项目中也在使用的九宫格类~~~

package
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	
	public class BitmapScale9Grid extends Sprite
	{
		private var source : Bitmap;
		private var scaleGridTop : Number;
		private var scaleGridBottom : Number;
		private var scaleGridLeft : Number;
		private var scaleGridRight : Number;
		
		private var leftUp : Bitmap;
		private var leftCenter : Bitmap;
		private var leftBottom : Bitmap;
		private var centerUp : Bitmap;
		private var center : Bitmap;
		private var centerBottom : Bitmap;
		private var rightUp : Bitmap;
		private var rightCenter : Bitmap;
		private var rightBottom : Bitmap;
		
		private var _width : Number;
		private var _height : Number;
		
		private var minWidth : Number;
		private var minHeight : Number;
		
		public function BitmapScale9Grid(source:Bitmap,scaleGridTop:Number,scaleGridBottom:Number,scaleGridLeft:Number,scaleGridRight:Number ) 
		{
			this.source = source;
			this.scaleGridTop = scaleGridTop;
			this.scaleGridBottom = scaleGridBottom;
			this.scaleGridLeft = scaleGridLeft;
			this.scaleGridRight = scaleGridRight;
			init();
			
		}
		
		private function init() : void {
			_width = source.width;
			_height = source.height;
			
			leftUp = getBitmap(0,scaleGridLeft,scaleGridTop);
			this.addChild(leftUp);
			
			leftCenter = getBitmap(0,scaleGridTop,scaleGridBottom - scaleGridTop);
			this.addChild(leftCenter);
			
			leftBottom = getBitmap(0,scaleGridBottom,source.height - scaleGridBottom);
			this.addChild(leftBottom);
			
			centerUp = getBitmap(scaleGridLeft,scaleGridRight - scaleGridLeft,scaleGridTop);
			this.addChild(centerUp);
			
			center = getBitmap(scaleGridLeft,scaleGridBottom - scaleGridTop);
			this.addChild(center);
			
			centerBottom = getBitmap(scaleGridLeft,source.height - scaleGridBottom);
			this.addChild(centerBottom);
			
			rightUp = getBitmap(scaleGridRight,source.width - scaleGridRight,scaleGridTop);
			this.addChild(rightUp);
			
			rightCenter = getBitmap(scaleGridRight,scaleGridBottom - scaleGridTop);
			this.addChild(rightCenter);
			
			rightBottom = getBitmap(scaleGridRight,source.height - scaleGridBottom);
			this.addChild(rightBottom);
			
			minWidth = leftUp.width + rightBottom.width;
			minHeight = leftBottom.height + rightBottom.height;
		}
		
		private function getBitmap(x:Number,y:Number,w:Number,h:Number) : Bitmap {
			var bit:BitmapData = new BitmapData(w,h);
			bit.copyPixels(source.bitmapData,new Rectangle(x,y,w,h),new Point(0,0));
			var bitMap:Bitmap = new Bitmap(bit);
			bitMap.x = x;
			bitMap.y = y;
			return bitMap;
		}
		
		override public function set width(w : Number) : void {
			if(w < minWidth) {
				w = minWidth;
			}
			_width = w;
			refurbishSize();
		}
		
		override public function set height(h : Number) : void {
			if(h < minHeight) {
				h = minHeight;
			}
			_height = h;
			refurbishSize();
		}
		
		private function refurbishSize() : void {
			leftCenter.height = _height - leftUp.height - leftBottom.height;
			leftBottom.y = _height - leftBottom.height;
			centerUp.width = _width - leftUp.width - rightUp.width;
			center.width = centerUp.width;
			center.height = leftCenter.height;
			centerBottom.width = center.width;
			centerBottom.y = leftBottom.y;
			rightUp.x = _width - rightUp.width;
			rightCenter.x = rightUp.x;
			rightCenter.height = center.height;
			rightBottom.x = rightUp.x;
			rightBottom.y = leftBottom.y;
		}
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读