位元影像的九宮縮放

來源:互聯網
上載者:User

AS3中只有向量圖才支援9格縮放, 而位元影像設定了scale9Grid也不起作用;

解決方案:  1, 把位元影像打散成向量圖,然後切成9分,再合并;   
BitmapSlice9.zip.      

我一般都採用這種方式, 但這個方法有個瑕疵,  映像展開後,經常出現虛邊 和 細縫;

2.另一中方法是用程式控制,原理很簡單:用程式把位元影像切成九塊,裝裡一個Sprite裡,然後重寫Sprite的width和height這兩個方法,根據改變大小來重新設定位元影像的各個位置.這樣就實現了位元影像的不變形縮放.

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;                }        }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.