android 进度条中间有文本渲染问题
android progress bar with text inside in center render issue
我创建了一个带有文本的自定义进度条。解决方案工作正常,但现在文本被创建到进度条的顶部而不是中心。
当 Activity 暂停并恢复时。进度条再次呈现,然后文本出现在中心。
请在下面查看我的代码。
public class TextProgressBar extends ProgressBar {
private String text;
private Paint textPaint;
public TextProgressBar(Context context) {
super(context);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
//textPaint.setShadowLayer(1,1,1,Color.WHITE);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
public TextProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
textPaint.setTextSize(getHeight() * 0.8f);
int x = (getWidth()/2) - bounds.centerX();
int y = (getHeight()/2) - bounds.centerY();
canvas.drawText(text, x, y, textPaint);
}
public synchronized void setText(String text) {
this.text = text;
drawableStateChanged();
}
public synchronized void setPercentText(int percentText){
this.text = percentText + " %";
}
public void setTextColor(int color) {
textPaint.setColor(color);
drawableStateChanged();
}
}
加载后的显示方式
重新创建后的显示方式
在 inflation 之后尝试 requestLayout()
。它将再次强制 measure()
View
。查看 View
.
的生命周期
可能有点晚了,但在获取文本边界之前设置文本大小。
...
textPaint.setTextSize(getHeight() * 0.8f);
textPaint.getTextBounds(text, 0, text.length(), bounds);
...
我创建了一个带有文本的自定义进度条。解决方案工作正常,但现在文本被创建到进度条的顶部而不是中心。 当 Activity 暂停并恢复时。进度条再次呈现,然后文本出现在中心。 请在下面查看我的代码。
public class TextProgressBar extends ProgressBar {
private String text;
private Paint textPaint;
public TextProgressBar(Context context) {
super(context);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
//textPaint.setShadowLayer(1,1,1,Color.WHITE);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
public TextProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
text = "";
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
textPaint.setTextSize(getHeight() * 0.8f);
int x = (getWidth()/2) - bounds.centerX();
int y = (getHeight()/2) - bounds.centerY();
canvas.drawText(text, x, y, textPaint);
}
public synchronized void setText(String text) {
this.text = text;
drawableStateChanged();
}
public synchronized void setPercentText(int percentText){
this.text = percentText + " %";
}
public void setTextColor(int color) {
textPaint.setColor(color);
drawableStateChanged();
}
}
加载后的显示方式
重新创建后的显示方式
在 inflation 之后尝试 requestLayout()
。它将再次强制 measure()
View
。查看 View
.
可能有点晚了,但在获取文本边界之前设置文本大小。
...
textPaint.setTextSize(getHeight() * 0.8f);
textPaint.getTextBounds(text, 0, text.length(), bounds);
...