Flash 位图九宫格 -- 组件应用

package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    /**
     *位图九宫格实现
     * @author lxh
     *
     */    
    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, 0, scaleGridLeft, scaleGridTop);
            this.addChild(leftUp);
            
            leftCenter = getBitmap(0, scaleGridTop, scaleGridLeft, scaleGridBottom - scaleGridTop);
            this.addChild(leftCenter);
            
            leftBottom = getBitmap(0, scaleGridBottom, scaleGridLeft, source.height - scaleGridBottom);
            this.addChild(leftBottom);
            
            centerUp = getBitmap(scaleGridLeft, 0, scaleGridRight - scaleGridLeft, scaleGridTop);
            this.addChild(centerUp);
            
            center = getBitmap(scaleGridLeft, scaleGridTop, scaleGridRight - scaleGridLeft, scaleGridBottom - scaleGridTop);
            this.addChild(center);
            
            centerBottom = getBitmap(scaleGridLeft, scaleGridBottom, scaleGridRight - scaleGridLeft, source.height - scaleGridBottom);
            this.addChild(centerBottom);
            
            rightUp = getBitmap(scaleGridRight, 0, source.width - scaleGridRight, scaleGridTop);
            this.addChild(rightUp);
            
            rightCenter = getBitmap(scaleGridRight, scaleGridTop, source.width - scaleGridRight, scaleGridBottom - scaleGridTop);
            this.addChild(rightCenter);
            
            rightBottom = getBitmap(scaleGridRight, scaleGridBottom, source.width - 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;
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值