gpt4 book ai didi

java - 获取 Android ProgressBar 中 secondaryProgress 的宽度

转载 作者:行者123 更新时间:2023-11-30 09:28:40 25 4
gpt4 key购买 nike

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

@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);
}

我试图将文本定位在次要进度中,但不确定如何获取次要进度的当前宽度,基本上是当前进度的宽度。

最佳答案

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

@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);
}

关于java - 获取 Android ProgressBar 中 secondaryProgress 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13918947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com