as3绘制圆角矩形的时候,api没用好。绘制的图形会出现脱节,不平滑问题。那么,绘制的时候,请将笔触采用完整像素打开。如果想在圆角矩形上加点其他东东,就用线条填充吧。
package cn.vini123.tool { import flash.display.Sprite; public class AutoRoundRect extends Sprite { private var _width:Number = 0; private var _height:Number = 0; private var thickness:Number; private var lineColor:int; private var lineAlpha:Number; private var fillColor:int; private var fillAlpha:Number; private var moreWidth:Number; private var moreHeight:Number; private var topLeftRadius:Number; private var topRightRadius:Number; private var bottomLeftRadius:Number; private var bottomRightRadius:Number; /** * * @param thickness 线条大小 * @param lineColor 线条颜色 * @param lineAlpha 线条透明度 * @param fillColor 填充颜色 * @param fillAlpha 填充透明度 * @param containLine 宽高是否包含线条的粗细 * @param topLeftRadius 左上半径 * @param topRightRadius 右上半径 * @param bottomLeftRadius 左下半径 * @param bottomRightRadius 右下半径 * * */ public function AutoRoundRect(thickness:Number , lineColor:int , lineAlpha:Number , fillColor:int , fillAlpha:Number , containLine:Boolean = false, topLeftRadius:Number = 0, topRightRadius:Number = 0 , bottomLeftRadius:Number = 0 , bottomRightRadius:Number = 0) { this.thickness = thickness; this.lineColor = lineColor; this.lineAlpha = lineAlpha; this.fillColor = fillColor; this.fillAlpha = fillAlpha; this.topLeftRadius = topLeftRadius; this.topRightRadius = topRightRadius; this.bottomLeftRadius = bottomLeftRadius; this.bottomRightRadius = bottomRightRadius; moreWidth = 0; moreHeight = 0; if(containLine) { moreWidth = - thickness; moreHeight = - thickness; } } /** * * @param width 宽度 * @param height 高度 * @param topLeftRadius 左上半径 * @param topRightRadius 右上半径 * @param bottomLeftRadius 左下半径 * @param bottomRightRadius 右下半径 * */ public function updateSize(width:int , height:int , topLeftRadius:Number = 0, topRightRadius:Number = 0 , bottomLeftRadius:Number = 0 , bottomRightRadius:Number = 0):void { _width = width; _height = height; this.topLeftRadius = topLeftRadius; this.topRightRadius = topRightRadius; this.bottomLeftRadius = bottomLeftRadius; this.bottomRightRadius = bottomRightRadius; adujstWidth(); adujstHeight(); redraw(); } override public function set width(value:Number):void { if(_width != value) { _width = value + moreWidth; adujstWidth(); if(_height != 0) { redraw(); } } } override public function set height(value:Number):void { if(_height != value) { _height = value + moreHeight; adujstHeight(); if(_width != 0) { redraw(); } } } /** * 纠正宽度 * */ private function adujstWidth():void { if(_width < (topLeftRadius + topRightRadius + moreWidth) || _width < (bottomLeftRadius + bottomRightRadius + moreWidth) ) { _width = Math.max(topLeftRadius + topRightRadius + moreWidth , bottomLeftRadius + bottomRightRadius + moreWidth); } } /** * 纠正高度 * */ private function adujstHeight():void { if(_height < (topLeftRadius + bottomLeftRadius + moreWidth) || _height < (topRightRadius + bottomRightRadius + moreWidth) ) { _height = Math.max(topLeftRadius + bottomLeftRadius + moreWidth , topRightRadius + bottomRightRadius + moreWidth); } } private function redraw():void { this.graphics.clear(); this.graphics.lineStyle(thickness , lineColor , lineAlpha , true); this.graphics.beginFill(fillColor , fillAlpha); this.graphics.moveTo(topLeftRadius , 0); this.graphics.lineTo(_width - topRightRadius , 0); this.graphics.curveTo(_width , 0 , _width , topRightRadius); this.graphics.lineTo(_width , _height - bottomRightRadius); this.graphics.curveTo(_width , _height , _width - bottomRightRadius , _height); this.graphics.lineTo(bottomLeftRadius , _height); this.graphics.curveTo(0 , _height , 0 , _height - bottomLeftRadius); this.graphics.lineTo(0 , topLeftRadius); this.graphics.curveTo(0 , 0 , topLeftRadius , 0); this.graphics.endFill(); } } }
package cn.vini123 { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.TimerEvent; import flash.geom.Point; import flash.utils.Timer; import cn.vini123.tool.AutoRoundRect; [SWF(width = "640" , height="500" , frameRate="30")] public class TestDraw extends Sprite { private var autoRoundRect:AutoRoundRect; private var officialRect:Sprite; private var lineColor:int = 0xcf8bcd; private var fillColor:int = 0xfaecfb; private var repeatedNum:int = 0; private const maxRepeatedNum:int = 8; private var timer:Timer; public function TestDraw() { addEventListener(Event.ADDED_TO_STAGE , addToStageHandler); } private function addToStageHandler(e:Event):void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; initialize(); } private function initialize():void { autoRoundRect = new AutoRoundRect(2 , lineColor , 1 , fillColor , 1 , false , 12 , 0 , 0 , 12); addChild(autoRoundRect); officialRect = new Sprite(); addChild(officialRect); drawOther(); timer = new Timer(5000); timer.addEventListener(TimerEvent.TIMER , timerHandler); timer.start(); timerHandler(); } protected function timerHandler(e:TimerEvent = null):void { redraw(); } private function redraw():void { var autoWidth:int = Math.max(int(stage.stageWidth * 0.5 * Math.random()) , 5); var autoHeight:int = Math.max(int(stage.stageHeight * 0.5 * Math.random()) , 20); var autoHasRadius:String = String(int(9999 * Math.random())); var len:int = autoHasRadius.length; var autoRadiusList:Array = []; var i:int = 0; for(i = 0 ; i < 4 - len ; i++) { autoHasRadius += "0"; } len = autoHasRadius.length; for(i = 0 ; i < len ; i++) { if(int(autoHasRadius.charAt(i)) %2 == 0) { autoRadiusList.push(0); } else { var autoRadius:int = Math.max(int(30 * Math.random()) , 4); autoRadiusList.push(autoRadius); } } officialRect.graphics.clear(); officialRect.graphics.lineStyle(2 , int(0xffffff * Math.random()) , 1 , true); officialRect.graphics.beginFill(int(0xffffff * Math.random())); officialRect.graphics.drawRoundRectComplex(0 , 0 , autoWidth , autoHeight , autoRadiusList[0] , autoRadiusList[1] , autoRadiusList[2] , autoRadiusList[3]); officialRect.graphics.endFill(); autoRoundRect.updateSize(autoWidth , autoHeight , autoRadiusList[0] , autoRadiusList[1] , autoRadiusList[2] , autoRadiusList[3]); timer.stop(); repeatedNum = 0; updateRectPosition(); } private function updateRectPosition():void { if(repeatedNum >= maxRepeatedNum) { timer.start(); timerHandler(); return; } officialRect.x = int((stage.stageWidth - officialRect.width) * Math.random()); officialRect.y = 150 + int((stage.stageHeight - officialRect.height -150) * Math.random()); autoRoundRect.x = int((stage.stageWidth - officialRect.width) * Math.random()); autoRoundRect.y = 150 + int((stage.stageHeight - officialRect.height - 150) * Math.random()); if(officialRect.hitTestObject(autoRoundRect)) { repeatedNum ++ ; updateRectPosition(); return; } timer.start(); } private function drawOther():void { var w1:int = 200; var h1:int = 120; var radius:int = 8; var leftPos:Point = new Point(-12, 30); var sp:Sprite = new Sprite(); sp.graphics.lineStyle(2 , lineColor , 1 , true); sp.graphics.beginFill(fillColor , 1); sp.graphics.moveTo(radius , 0); sp.graphics.lineTo(w1 - radius , 0); sp.graphics.curveTo(w1 , 0 , w1 , radius); sp.graphics.lineTo(w1 , h1 - radius ); sp.graphics.curveTo(w1 , h1 , w1 - radius , h1); sp.graphics.lineTo(radius , h1); sp.graphics.curveTo(0 , h1 , 0 , h1 - radius); sp.graphics.lineTo(0 , leftPos.y + 1); sp.graphics.lineTo(leftPos.x , leftPos.y); sp.graphics.lineTo(0 , leftPos.y - radius); sp.graphics.lineTo(0 , radius); sp.graphics.curveTo(0 , 0 , radius , 0); sp.x = stage.stageWidth * 0.5 - sp.width * 0.5; sp.y = 15 addChild(sp); } } }