AS3使用位图实现画图

通过graphics使用BitmapData实现画图。

经过测试效率没有Bitmap效率好。

如果不进行缩放,旋转等操作和直接使用Bitmap效率相当。

另外,Matrix的很多操作效率都不是很好。比如旋转。

package  {

    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class Symbol extends Shape {

        protected var originalRect:Rectangle;

        private var _anchorPt:Point = new Point();

        public function get anchorPt():Point {
            return _anchorPt;
        }

        private var _width:Number = 100;
        private var _height:Number = 100;

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

        override public function set width(value:Number):void {
            _width = value;
            _scaleX = originalRect.width / value;
            draw()
        }

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

        override public function set height(value:Number):void {
            _height = value;
            _scaleY = originalRect.height / value;
            draw()
        }

        private var _scaleX:Number = 1;

        override public function set scaleX(value:Number):void {
            _scaleX = value;
            _width = originalRect.width * value;
            draw()
        }

        override public function get scaleX():Number {
            return _scaleX;
        }

        override public function set scaleY(value:Number):void {
            _scaleY = value;
            _height = originalRect.height * value;
            draw()
        }

        override public function get scaleY():Number {
            return _scaleY;
        }
        private var _scaleY:Number = 1;

        override public function set scaleZ(value:Number):void {
            throw new Error("没有实现scaleZ");
        }

        /**
         * 锚点
         * @param value
         *
         */
        public function set anchorPt(value:Point):void {
            _anchorPt = value;
            draw()
        }

        public function get bmd():BitmapData {
            return _bmd;
        }

        public function set bmd(value:BitmapData):void {
            _bmd = value;
            if (value == null) {
                originalRect.width = 100;
                originalRect.height = 100;
                graphics.clear();
            } else {
                originalRect.width = value.width;
                originalRect.height = value.height;
                _width = originalRect.width * scaleX;
                _height = originalRect.height * scaleY;
                draw()
            }
        }
        private var _bmd:BitmapData;

        protected var _x:Number = 0;

        override public function get x():Number {
            return _x;
        }

        override public function set x(value:Number):void {
            _x = value;
            draw()
        }

        protected var _y:Number = 0;

        override public function get y():Number {
            return _y;
        }

        override public function set y(value:Number):void {
            _y = value;
            draw()
        }

        private var _rotation:Number = 0;
        private var _radian:Number = 0;

        override public function set rotation(value:Number):void {
            _rotation = value % 360;
            _radian = Math.PI * value / 180;
            draw()
        }

        override public function get rotation():Number {
            return _rotation;
        }

        override public function set rotationX(value:Number):void {
            throw new Error("没有实现rotationX属性");
        }

        override public function set rotationY(value:Number):void {
            throw new Error("没有实现rotationY属性");
        }

        override public function set rotationZ(value:Number):void {
            throw new Error("没有实现rotationZ属性");
        }

        public function Symbol(bitmapData:BitmapData = null, smoothing:Boolean = false) {
            super();
            originalRect = new Rectangle();
            this.bmd = bitmapData;
            this.smoothing = smoothing;
        }
        private var smoothing:Boolean;

        protected function draw():void {
            if (bmd != null) {
                var rect:Rectangle;
                var anchor:Point;
                if (scaleX != 1 || scaleY != 1) {
                    rect = new Rectangle(0, 0, originalRect.width * scaleX, originalRect.height * scaleY);
                    anchor = new Point(anchorPt.x * scaleX, anchorPt.y * scaleY);
                } else {
                    rect = originalRect;
                    anchor = anchorPt;
                }
                //顶点坐标
                var vertexList:Array = [];
                var p0:Point = new Point(-anchor.x, -anchor.y);
                vertexList[0] = p0;
                vertexList[1] = new Point(p0.x + rect.width, p0.y)
                vertexList[2] = new Point(p0.x + rect.width, p0.y + rect.height);
                vertexList[3] = new Point(p0.x, p0.y + rect.height);

                var i:int = 0;
                if (_rotation != 0) {
                    for (i = 0; i < 4; i++) {
                        vertexList[i] = rotatePoint(_radian, vertexList[i]);
                    }
                }
                //找到旋转后新的外接矩形
                var leftTopPt:Point = vertexList[0].clone();
                var rightBottomPt:Point = vertexList[0].clone();
                for (i = 0; i < 4; i++) {
                    var pt:Point = vertexList[i];
                    if (pt.x < leftTopPt.x) {
                        leftTopPt.x = pt.x;
                    }
                    if (pt.y < leftTopPt.y) {
                        leftTopPt.y = pt.y;
                    }
                    if (pt.x > rightBottomPt.x) {
                        rightBottomPt.x = pt.x;
                    }
                    if (pt.y > rightBottomPt.y) {
                        rightBottomPt.y = pt.y;
                    }
                }
                _height = rightBottomPt.y - leftTopPt.y;
                _width = rightBottomPt.x - leftTopPt.x;
                //
                var matrix:Matrix = new Matrix();
                if (scaleX != 1 || scaleY != 1)
                    matrix.scale(scaleX, scaleY);
                matrix.translate(-anchor.x, -anchor.y);
                if (_rotation != 0)
                    matrix.rotate(_radian);
                matrix.translate(_x, _y);

                graphics.clear();
                graphics.beginBitmapFill(bmd, matrix, false, smoothing);
                graphics.drawRect(leftTopPt.x + _x, leftTopPt.y + _y, _width, _height);
                graphics.endFill();
            }
        }

        public function rotatePoint(radian: Number, pt: Point, center: Point = null):Point {
            if (center == null) {
                center = new Point();
            }
            var matrix:Matrix = new Matrix(1, 0, 0, 1, pt.x - center.x, pt.y - center.y);
            matrix.rotate(radian);
            return new Point(matrix.tx + center.x, matrix.ty + center.y);
        }

    }
}
 

 

转载于:https://www.cnblogs.com/gw-north/articles/4605871.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值