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

时间:2014-05-27 16:29:14   收藏:0   阅读:1470
bubuko.com,布布扣
/** 
 * 带文本提示的进度条 
 */
public class TextProgressBar extends ProgressBar { 
    private String text; 
    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.text, 0, this.text.length(), rect); 
        int x = (getWidth() / 2) - rect.centerX(); 
        int y = (getHeight() / 2) - rect.centerY(); 
        canvas.drawText(this.text, 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.text = String.valueOf(i) + "%"; 
    } 
}
bubuko.com,布布扣

 

Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!