简单自定义水平的ProgressBar带文字

首先来看看实图


这里实现原理就是继承ProgressBar重写onDraw方法,在ondraw里面用画笔绘制text


首先初始化paint


首先获取手机屏幕的宽高,根据手机分辨率来定标准字体大小,这里采用的720x1080(比较通用)



获取进度,设置文字内容





最后就是onDraw绘制了

@Override
protected synchronized void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Rect rect = new Rect();
    this.mPaint.getTextBounds(text, 0, text.length(), rect);
    this.mPaint.getTextBounds(text1, 0, text1.length(), rect);
    
    int progress = getProgress();
    int width = getWidth();
    
/**以免getmax为0
    if (getMax() != 0)
    {
        int start = width / getMax() * progress;
        int end = width / getMax() * (getMax() - progress);
        int x_two = ((end / 2) + start) - rect.centerX();
        int x_one = (start / 2) - rect.centerX();
        int y = (getHeight() / 2) - rect.centerY();
        if (getProgress() == 0)
        {
            canvas.drawText(this.text1, x_two, y, this.mPaint);
        }
        else
        {
            canvas.drawText(this.text, x_one, y, this.mPaint);
            canvas.drawText(this.text1, x_two, y, this.mPaint);
        }
    }
}
到这里就完成了。
代码下载地址http://download.csdn.net/download/lmy545x/10038852


   











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值