DrawRoundRect大小的问题

时间:2011-08-01 13:31:10

标签: actionscript-3 graphics mobile drawing

我正在尝试使用ActionScript 3.0创建自定义按钮。我正在起诉一个圆形的矩形作为背景,但我的尺寸有问题。

这是我的自定义按钮类:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Shape;

    public class customButton extends Sprite
    {
        private var background:Shape;
        public var bgColor:uint;
        public var borderColor:uint;
        public var borderSize:uint;
        public var cornerRadius:uint;
        private var label:TextField;

        public function customButton(text:String)
        {
            super();

            this.opaqueBackground = 0xFF0000;

            background = new Shape();
            borderSize = 1;
            borderColor = 0x666666;
            bgColor = 0xFFCC00;
            cornerRadius = 9;

            label = new TextField();
            label.text = text;

            var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0;
            format.size = 38;
            format.underline = true;

            label.defaultTextFormat = format;

            addChild(background);
            addChild(label);

            buttonMode = true;
            mouseChildren = false;
        }

        public function draw():void
        {
            background.graphics.lineStyle(borderSize, borderColor);
            background.graphics.beginFill(bgColor);
            background.graphics.drawRoundRect(0, 0, this.width, this.height cornerRadius);
            background.graphics.endFill();
        }
    }
}

这是用于显示按钮的代码:

    public function Test01()
    {
        super();

        // support autoOrients
        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;

        button = new customButton("Button");
        button.x = 200;
        button.y = 300;
        button.width = 200;
        button.height = 100;
        button.draw();

        addChild(button);
    }

如果我将此尺寸设置为按钮:

button.width = 200;
button.height = 100;

我得到以下内容:

enter image description here

但我将其设置为按钮大小:

button.width = 40; button.height = 20;

(此大小与customButton类中使用的大小相同)。我明白了:

enter image description here

我不知道为什么当我使用(40,20)的尺寸时,我会得到一个小于那个尺寸的矩形。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

它正在发生,因为您将宽度直接设置为Sprite,它会更改精灵的大小,而不是您正在绘制的背景的大小。

在你的customButton类中添加一些代码:

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

override public function get width():Number { return _width; }      
override public function set width(value:Number):void 
{
    _width = value;
    draw ();
}

override public function get height():Number { return _height; }        
override public function set height(value:Number):void 
{
    _height = value;
    draw ();
}

private function draw():void
{
    background.graphics.clear ()
    background.graphics.lineStyle(borderSize, borderColor);
    background.graphics.beginFill(bgColor);
    background.graphics.drawRoundRect(0, 0, _width, _height, cornerRadius);
    background.graphics.endFill();
}

使用此代码,您每次都可以设置背景大小,并且不会影响其他组件。

答案 1 :(得分:0)

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Shape;

    public class customButton extends Sprite
    {
        private var background:Shape;
        public var bgColor:uint;
        public var borderColor:uint;
        public var borderSize:uint;
        public var cornerRadius:uint;
        public var bWidth:Number;
        public var bHeight:Number;
        private var label:TextField;

        public function customButton(text:String, bW:Number, bH:Number)
        {
            super();
            this.bWidth = bW;
            this.bHeight = bH;

            background = new Shape();
            borderSize = 1;
            borderColor = 0x666666;
            bgColor = 0xFFCC00;
            cornerRadius = 9;

            label = new TextField();
            label.text = text;

            var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0;
            format.size = 38;
            format.underline = true;

            label.defaultTextFormat = format;

            addChild(background);
            addChild(label);

            buttonMode = true;
            mouseChildren = false;
            background.graphics.lineStyle(borderSize, borderColor);
            background.graphics.beginFill(bgColor);
            background.graphics.drawRoundRect(0, 0, bWidth, bHeight, cornerRadius);
            background.graphics.endFill();
        }

    }
}

试试这个

答案 2 :(得分:0)

你的roundRect类大小已经修复,所以当你增加宽度时它没有做任何改变。

为宽度(Bwidth)和高度(Bheight)创建两个公共参数并访问它。

喜欢

button.Bwidth = 100;