【问题标题】:Get the width of a the secondaryProgress within a Android ProgressBar获取 Android ProgressBar 中的 secondaryProgress 的宽度
【发布时间】:2012-12-17 17:24:45
【问题描述】:

我有一个带有文本的进度条,其中我已经像这样覆盖了 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);
}

我正在尝试将文本定位在辅助进度中,但不确定如何获取辅助进度的当前宽度,基本上是当前进度的宽度。

【问题讨论】:

    标签: java android android-progressbar


    【解决方案1】:

    现在我只是使用了一个变通方法,基本上我得到了进度的百分比,然后将它乘以密度。我需要在进度条内对齐文本 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2012-05-20
      • 1970-01-01
      • 2013-05-31
      相关资源
      最近更新 更多