4,328
社区成员
发帖
与我相关
我的任务
分享
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
/**
* 用圆环显示百分比的一个组件。
* 注意:可以写得更复杂一些,比如:控制圆环是否显示线条,允许用户圆环的颜色,百分比文本的颜色、字体,圆环内外径的比例等等。
* @author Administrator
*/
public class RingProgress extends Sprite
{
/**
* 进度。范围:[0,1]
* @default 0
*/
private var _percent:Number=0;
/**
* 进度条圆环外径大小
* @default 50
*/
private var _size:Number=50;
/**
* 进度条圆环的内径
* @default 外径的60%
*/
private var _smallSize:Number=30;
/**
* 用于显示百分比的文本框
*/
private var tf_percent:TextField;
public function RingProgress(size:Number=50)
{
this._size=size;
this._smallSize=this._size*0.6;
tf_percent=new TextField();
tf_percent.autoSize=TextFieldAutoSize.CENTER;
tf_percent.selectable=false;
var foramt:TextFormat=new TextFormat();
foramt.align=TextFormatAlign.CENTER;
tf_percent.defaultTextFormat=foramt;
tf_percent.text=this._percent.toFixed(2) + "%";
this.addChild(tf_percent);
draw();
}
/**
* 显示百分比,绘制图形。
*/
private function draw():void
{
var format:TextFormat=tf_percent.defaultTextFormat;
format.size=this.size * 0.3;
tf_percent.defaultTextFormat=format;
var value:Number=this._percent * 100;
tf_percent.text=value.toFixed(2) + "%";
tf_percent.x=tf_percent.width / 2 * -1;
tf_percent.y=tf_percent.height / 2 * -1;
var g:Graphics=this.graphics;
g.clear();
if (this.percent > 0)
{
//g.lineStyle(1, 0xFF0000);
g.lineStyle();
g.beginFill(0x00CCCC);
g.moveTo(this._size, 0);
var rad:Number;
var i:Number;
var angle:Number=Math.PI * 2 * percent;
var end:int=int(Math.round(angle * 1000));
for (i=1; i <= end; i++)
{
rad=i / 1000;
g.lineTo(this._size * Math.cos(rad), this._size * Math.sin(rad));
}
g.lineTo(this._smallSize * Math.cos(rad), this._smallSize * Math.sin(rad));
for (i=end - 1; i >= 0; i--)
{
rad=i / 1000;
g.lineTo(this._smallSize * Math.cos(rad), this._smallSize * Math.sin(rad));
}
g.lineTo(this._size, 0);
g.endFill();
}
}
/**
* 进度条大小
* @default 50
*/
public function get size():Number
{
return _size;
}
/**
* @private
*/
public function set size(value:Number):void
{
this._size=value;
this._smallSize=this._size*0.6;
draw();
}
/**
* 进度。范围:[0,1]
*/
public function get percent():Number
{
return _percent;
}
/**
* @private
*/
public function set percent(value:Number):void
{
if (value > 1)
{
value=1;
}
else if (value < 0)
{
value=0;
}
_percent=value;
draw();
}
}
}