[as3]圆形的进度条
AS Code
package { import flash.display.Sprite; import flash.events.Event; import flash.display.Shape; import flash.text.TextField; [SWF(width = 550, height = 440, backgroundColor = 0xffffff, frameRate = 24)] /** * ... * @author spark.fandlr@gmail.com */ public class Main extends Sprite { private var _shape:Shape; private var an:Number = 0; private const r:Number = 50; private var _txt:TextField; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point _shape = new Shape; _shape.x = stage.stageWidth / 2; _shape.y = stage.stageHeight / 2; _txt = new TextField; _txt.x = _shape.x; _txt.y = _shape.y; addChild(_shape); addChild(_txt); addEventListener(Event.ENTER_FRAME, fdraw); } private function fdraw(event:Event):void { an ++; if (an >= 360) { removeEventListener(Event.ENTER_FRAME, fdraw); } else { drawShan(an); } } private function drawShan(angle:Number):void { _shape.graphics.clear(); _shape.graphics.lineStyle(2, 0x0000ff); _shape.graphics.beginFill(0xff0000, 0.5); var p:Number = angle / 180 * Math.PI; _shape.graphics.moveTo(0, 0); _shape.graphics.lineTo(r * Math.cos(p), -r * Math.sin(p)); var part:int = Math.floor(angle / 45); var remain:Number = angle - part * 45; var hp:Number = remain / 180 * Math.PI / 2; var hr:Number = Math.abs(r / Math.cos(hp)); _shape.graphics.curveTo(hr * Math.cos(p - hp), - hr * Math.sin(p - hp), r * Math.cos(p - hp * 2), - r * Math.sin(p - hp * 2)); switch(part) { case 7: _shape.graphics.curveTo(Math.tan(Math.PI / 8) * r, r , 0, r ); case 6: _shape.graphics.curveTo(-Math.tan(Math.PI/8)*r, r, -Math.sin(Math.PI/4) * r , Math.sin(Math.PI / 4) * r); case 5: _shape.graphics.curveTo(-r, Math.tan(Math.PI / 8) * r , -r , 0 ); case 4: _shape.graphics.curveTo( -r , -Math.tan(Math.PI/8)*r, -Math.sin(Math.PI/4) * r , -Math.sin(Math.PI / 4) * r ); case 3: _shape.graphics.curveTo( -Math.tan(Math.PI / 8) * r , -r , 0, -r); case 2: _shape.graphics.curveTo( Math.tan(Math.PI/8)*r, -r, Math.sin(Math.PI/4) * r , -Math.sin(Math.PI / 4) * r ); case 1: _shape.graphics.curveTo(r, -Math.tan(Math.PI/8)*r,r, 0 ); } _shape.graphics.lineTo(0, 0); _shape.graphics.endFill(); _txt.text = int(angle / 360 * 100).toString() + "%"; } } }

Leave a Reply