Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)

默认的进度条(ProgressBar)是没有文本进度提示的,为了更直观显示进度,自定义了ProgressBar控件来显示的文本进度:
也就是说给ProgressBar添加文本显示的进度条(带%100显示的字样)非常给力,

 参考这里:http://orgcent.com/android-text-progressbar/
我不废话直接上代码:

/*****
 * 
 * @author lixiaodaoaaa
 *         http://weibo.com/lixiaodaoaaa
 *         http://t.qq.com/lixiaodaoaaa
 * 
 */
public class TextProgressBar extends ProgressBar
{
	private String str;
	private Paint mPaint;

	public TextProgressBar(Context context)
	{
		super(context);
		initText();
	}

	public TextProgressBar(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		initText();
	}

	public TextProgressBar(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initText();
	}

	@Override
	public void setProgress(int progress)
	{
		setText(progress);
		super.setProgress(progress);

	}

	@Override
	protected synchronized void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		Rect rect = new Rect();
		this.mPaint.getTextBounds(this.str, 0, this.str.length(), rect);
		int x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
		int y = (getHeight() / 2) - rect.centerY();// 让显示的字体处于中心位置;;
		canvas.drawText(this.str, x, y, this.mPaint);
	}

	// 初始化,画笔
	private void initText()
	{
		this.mPaint = new Paint();
		this.mPaint.setAntiAlias(true);// 设置抗锯齿;;;;
		this.mPaint.setColor(Color.WHITE);
	}

	// 设置文字内容
	private void setText(int progress)
	{
		int i = (int) ((progress * 1.0f / this.getMax()) * 100);
		this.str = String.valueOf(i) + "%";
	}
}


 

 

 


 

 

 

你可能感兴趣的:(Android开发)