我已经创建了一个自定义进度条,里面有文字。解决方案工作正常但现在文本被创建到进度条顶部而不是中心。 当活动暂停并恢复时。进度条再次渲染,然后文本出现在中心。 请参阅下面的代码。
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();
}
}
加载时的显示方式
重新创建后的显示方式