码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-05-27 16:29:14      阅读:1470      评论:0      收藏:0      [点我收藏+]

标签:android   style   c   class   blog   code   

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

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

标签:android   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/linximeng/p/3753108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!