Bitmap九宫格

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class Bitmap9 extends Bitmap
    {
        private var rect:Rectangle;

        private var _w:Number=0;

        private var _h:Number=0;

        private var bd1:BitmapData;
        private var bd2:BitmapData;
        private var bd3:BitmapData;

        private var bd4:BitmapData;
        private var bd5:BitmapData;
        private var bd6:BitmapData;

        private var bd7:BitmapData;
        private var bd8:BitmapData;
        private var bd9:BitmapData;

        public function Bitmap9(bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
        {
            super(bitmapData, pixelSnapping, smoothing);
            setSize(bitmapData.width,bitmapData.height);
        }

        override public function set width(value:Number):void
        {
            _w=value;
            changeSize();
        }

        override public function get width():Number
        {
            return _w;
            changeSize();
        }

        override public function set height(value:Number):void
        {
            _h=value;
            changeSize();
        }

        override public function get height():Number
        {
            return _h;
            changeSize();
        }

        public function setSize(_w:Number, _h:Number):void
        {
            this._w=_w;
            this._h=_h;

            changeSize();
        }

        private function changeSize():void
        {
            if (rect != null)
            {
                
                var cw:Number=_w - bd1.width - bd3.width;
                var ch:Number=_h - bd1.height - bd7.height;
                
                if(cw<=0 && ch<=0) return ;
                
                this.bitmapData.dispose();
                this.bitmapData=new BitmapData(_w,_h,true,0x00000000);
                
                var _bd2:BitmapData=new BitmapData(cw,bd2.height);
                _bd2.draw(bd2,new Matrix(cw/bd2.width,0,0,1));
                
                var _bd4:BitmapData=new BitmapData(bd4.width,ch);
                _bd4.draw(bd4,new Matrix(1,0,0,ch/bd4.height));
                
                var _bd5:BitmapData=new BitmapData(cw,ch);
                _bd5.draw(bd5,new Matrix(cw/bd5.width,0,0,ch/bd5.height));
                
                var _bd6:BitmapData=new BitmapData(bd6.width,ch);
                _bd6.draw(bd6,new Matrix(1,0,0,ch/bd6.height));

                var _bd8:BitmapData=new BitmapData(cw,bd8.height);
                _bd8.draw(bd8,new Matrix(cw/bd8.width,0,0,1));
                
                this.bitmapData.copyPixels(bd1, new Rectangle(0, 0, bd1.width, bd1.height), new Point);
                this.bitmapData.copyPixels(_bd2, new Rectangle(0, 0, _bd2.width, _bd2.height), new Point(rect.x, 0));
                this.bitmapData.copyPixels(bd3, new Rectangle(0, 0, bd3.width, bd3.height), new Point(bd1.width + _bd2.width, 0));

                this.bitmapData.copyPixels(_bd4, new Rectangle(0, 0, _bd4.width, _bd4.height), new Point(0, rect.y));
                this.bitmapData.copyPixels(_bd5, new Rectangle(0, 0, _bd5.width, _bd5.height), new Point(rect.x, rect.y));
                this.bitmapData.copyPixels(_bd6, new Rectangle(0, 0, _bd6.width, _bd6.height), new Point(bd4.width +_bd5.width, rect.y));

                this.bitmapData.copyPixels(bd7, new Rectangle(0, 0, bd7.width, bd7.height), new Point(0, bd1.height + _bd4.height));
                this.bitmapData.copyPixels(_bd8, new Rectangle(0, 0, _bd8.width, _bd8.height), new Point(rect.x, _bd2.height + _bd5.height));
                this.bitmapData.copyPixels(bd9, new Rectangle(0, 0, bd9.width, bd9.height), new Point(bd7.width + _bd8.width, bd3.height + _bd6.height));
            }
            else
            {
                super.width=_w;
                super.height=_h;
            }
        }

        override public function set scale9Grid(innerRectangle:Rectangle):void
        {
            rect=innerRectangle;
            Grid9();
        }

        override public function get scale9Grid():Rectangle
        {
            return rect;
        }


        private function Grid9():void
        {
            if (bitmapData && rect)
            {
                bd1=new BitmapData(rect.x, rect.y, true, 0x00000000);
                bd1.copyPixels(bitmapData, new Rectangle(0, 0, bd1.width, bd1.height), new Point());

                bd2=new BitmapData(rect.width, rect.y, true, 0x00000000);
                bd2.copyPixels(bitmapData, new Rectangle(rect.x, 0, bd2.width, bd2.height), new Point());

                bd3=new BitmapData(bitmapData.width - rect.right, rect.y, true, 0x00000000);
                bd3.copyPixels(bitmapData, new Rectangle(rect.right, 0, bd3.width, bd3.height), new Point());

                bd4=new BitmapData(rect.x, rect.height, true, 0x00000000);
                bd4.copyPixels(bitmapData, new Rectangle(0, rect.y, bd4.width, bd4.height), new Point());

                bd5=new BitmapData(rect.width, rect.height, true, 0x00000000);
                bd5.copyPixels(bitmapData, new Rectangle(rect.x, rect.y, bd5.width, bd5.height), new Point());

                bd6=new BitmapData(bitmapData.width - rect.right, rect.height, true, 0x00000000);
                bd6.copyPixels(bitmapData, new Rectangle(rect.right, rect.y, bd6.width, bd6.height), new Point());

                bd7=new BitmapData(rect.x, bitmapData.height - rect.bottom, true, 0x00000000);
                bd7.copyPixels(bitmapData, new Rectangle(0, rect.bottom, bd7.width, bd7.height), new Point());

                bd8=new BitmapData(rect.width, bitmapData.height - rect.bottom, true, 0x00000000);
                bd8.copyPixels(bitmapData, new Rectangle(rect.x, rect.bottom, bd8.width, bd8.height), new Point());

                bd9=new BitmapData(bitmapData.width - rect.right, bitmapData.height - rect.bottom, true, 0x00000000);
                bd9.copyPixels(bitmapData, new Rectangle(rect.right, rect.bottom, bd9.width, bd9.height), new Point());
            }
            
            
            
            
        }

    }
}

 

 

 

 

 

posted @ 2012-12-07 20:05  ndljava  阅读(342)  评论(0编辑  收藏  举报