2

次のように 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);
}

テキストをセカンダリ プログレス内に配置しようとしていますが、セカンダリ プログレスの現在の幅、基本的に現在のプログレスの幅を取得する方法がよくわかりません。

4

1 に答える 1

2

今のところ、回避策を使用しました。基本的には、進行状況のパーセンテージを取得し、それを密度で乗算します。プログレスバー内でテキストを 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);
}
于 2012-12-19T15:09:17.420 に答える