获取Android ProgressBar中secondaryProgress的宽度 [英] Get the width of a the secondaryProgress within a Android ProgressBar

查看:570
本文介绍了获取Android ProgressBar中secondaryProgress的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文字的进度条,我在其中覆盖了onDraw,如下所示:

I have a progress bar with text in which I have overridden the onDraw like so:

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(textColor);
    textPaint.setTextSize(textSize);
    Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);

    float fx = getX();
    float fy = getY();

    int x = getWidth() / 2 - bounds.centerX();
    int y = getHeight() / 2 - bounds.centerY();
    canvas.drawText(text, x, y, textPaint);
}

我试图将文本放在次要进度内但不是很确定如何得到次要进度的当前宽度,基本上是当前进度的宽度。

I am trying to position the text inside of the secondary progress but not really sure how get the the current width of the secondary progress, basically the width of the current progress.

推荐答案

现在我刚用了一个解决方法,基本上我得到进度的百分比,然后乘以密度。我需要在进度条中对齐文本20dp:

For now I just used a work around, basically I get the percentage of the progress and then multiply it by the density. I needed to align the text 20dp within the progress bar:

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(textColor);
    textPaint.setTextSize(textSize);
    Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);

    float density = getContext().getResources().getDisplayMetrics().density;
    float percentage = getProgress() / 100.0f;
    float x = getWidth() * percentage - (20 * density);
    float y = getHeight() / 2 - bounds.centerY();
    canvas.drawText(text, x, y, textPaint);
}

这篇关于获取Android ProgressBar中secondaryProgress的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆