+ -
当前位置:首页 → 问答吧 → 位图的九宫格(flash用)

位图的九宫格(flash用)

时间:2009-03-09

来源:互联网

转载请留下原文地址:http://blog.crazypig.org/?p=16

最近做游戏,发现flash里位图是无法使用scale9Grid这个属性,,,没办法,只好自己写了一个,,,原理很简单,,用程序把位图切成九块,,装里一个Sprite里,然后重写Sprite的width和height这两个方法,根据改变大小来重新设置位图的各个位置…这样就实现了位图的不变形缩放….
ps:近期我会把以前做的一些小东西的源码放出来,供大家学习使用…

严重声明,,,
发现好多人都不知道这个问题,,用FLASH编译,位图使用scale9Grid也没用,
复制内容到剪贴板
代码:
/**
* @project dynasty
* @author 郭晟 or 猪哥靓
* @copyright
* @document
* @history
* create 2009-2-20
* 位图Scale9Grid功能
**/
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, 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;
        }
    }
}

附件

位图Scale9Grid.swf (3.21 KB)

2009-3-9 13:15, 下载次数: 479

演示

BitmapScale9Grid.rar (7.82 KB)

2009-3-9 13:15, 下载次数: 183

源码

作者: wnosidw   发布时间: 2009-03-09

支持楼主

作者: cc1007   发布时间: 2009-03-09

厉害,值得研究。,

作者: byd168   发布时间: 2009-03-10

不错哦~~ 对我的帮助非常大

作者: zhang3968   发布时间: 2009-03-12

距离flash编程越来越遥远了 哎1~~~

作者: loversnie   发布时间: 2009-03-12

谢谢。不错的效果

作者: jinyuz   发布时间: 2009-03-12

好东西,多谢分享

作者: k5angle   发布时间: 2009-03-12

我也遇到问题,搜索到这了。
刚想夸奖楼主,可是我下一个网页却搜到了另外的结果:
在Flash里不是不可以,只是跟在Flex里方式不一样而已。
AS3里,显示对象拥有 scale9Grid 这个属性,就是他了。

作者: Jubu   发布时间: 2009-05-05

发现确实不可以直接用scale9Grid。不错,向楼主学习了

作者: HBrO   发布时间: 2009-05-05

如果scale9Grid为new Rect ( -10,-10 , 199, 200 )
这样也行吗?

作者: enc0717   发布时间: 2009-05-06

负数有必要吗?
这只是一个很简单的类,只要最基础的功能,,很多功能都需要去扩展...

[ 本帖最后由 wnosidw 于 2009-5-11 11:15 编辑 ]

作者: wnosidw   发布时间: 2009-05-11

感谢楼主提供.

作者: shutaocg   发布时间: 2009-05-15

感谢楼主,原来好像收藏过一个矢量的

作者: South123   发布时间: 2009-05-15

谢谢楼主,正在找这方面的东东呢

作者: bfsjj   发布时间: 2009-05-15

不错。顶一个。

作者: menuhinlove   发布时间: 2009-05-15

真的有用吗

作者: yaydaniel   发布时间: 2010-02-02

good  !

作者: guanlan10   发布时间: 2010-06-11

位图的九宫格,有个简单的方法,就是将位图转换成元件,然后将元件中的位图分离,将分别切出9块成组,就可以了。

附件

BitmapScale9Grid.rar (12.59 KB)

2010-6-12 10:17, 下载次数: 24

作者: yaonai2003   发布时间: 2010-06-12

提醒:最后回贴距现在 139 天,请不要无意义回复

用了楼主的扩展再加上flash ide本身的九宫格操作:让不支持9宫格的模式支持了9宫格
任何元件基于此类就不必写什么代码, 直接支持9宫格(前提,仅内置一个显示对象,且不需要命名)
package {
       import engine.BitmapScale9Grid;
       import flash.display.Bitmap;
       import flash.display.BitmapData;
       import flash.display.DisplayObject;
       import flash.display.Sprite;
       import flash.geom.Rectangle;
       /**
        * ...
        * @author Zszen John
        */
       public class Sprite9GridScaleWithBmp extends Sprite {
              private var displayisplayObject;
              private var grid9scale:BitmapScale9Grid
              private var bmp:Bitmap
              public function Sprite9GridScaleWithBmp() {
                     display = removeChildAt(0) as DisplayObject
                     var bd:BitmapData = new BitmapData(display.width,display.height,true,0)
                     bmp = new Bitmap(bd)
                     bd.draw(display)
                     display = null
                     var grid:Rectangle = scale9Grid
                     grid9scale = new BitmapScale9Grid(bmp, grid.top, grid.bottom, grid.left, grid.right)
                     addChild(grid9scale)
              }
              public function destroy():void {
                     bmp.bitmapData.dispose()
                     bmp.bitmapData = null
                     bmp = null
                     removeChild(grid9scale)
                     scale9Grid = null
              }
              override public function get width():Number { return grid9scale.width; }
              override public function set width(value:Number):void {
                     grid9scale.width = value;
              }
              override public function get height():Number { return grid9scale.height; }
              override public function set height(value:Number):void {
                     grid9scale.height = value;
              }
       }
}

作者: zszen   发布时间: 2010-10-28