zoukankan      html  css  js  c++  java
  • flash 中位图九宫格的用法

    flash里位图是无法使用scale9Grid这个属性,这是我整理的一个,原理很简单,用程序把位图切成九块,装里一个Sprite里,然后重写Sprite的width和height这两个方法,根据改变大小来重新设置位图的各个位置…这样就实现了位图的不变形缩放….

    代码如下:

    /**
    * @project dynasty
    * @author *

    * @copyright
    * @document
    * @history
    * create

    * 位图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;
            }
        }
    }

    本文章来自www.21shipin.com  21视频教程网
    Flash 中位图九宫格的用法_Flash AS教程 原文链接:http://www.21shipin.com/html/93006.shtml

  • 相关阅读:
    jquery获取tr并更改tr内容
    jquery获取元素索引值index()
    禁止apache显示目录索引 apache禁止列目录
    mysql启动错误之mysql启动报1067错误如何解决
    Expo大作战(四)--快速用expo构建一个app,expo中的关键术语
    Expo大作战(三)--针对已经开发过react native项目开发人员有针对性的介绍了expo,expo的局限性,开发时项目选型注意点等
    Expo大作战(二)--expo的生命周期,expo社区交流方式,expo学习必备资源,开发使用expo时关注的一些问题
    Expo大作战(一)--什么是expo,如何安装expo clinet和xde,xde如何使用
    Linux(CentOS)之-性能监控
    [转]winform程序textbox滚动条保持在最下面 内容不闪烁
  • 原文地址:https://www.cnblogs.com/happysky97/p/1939277.html
Copyright © 2011-2022 走看看