一、效果
二、代碼
public class PZHelp_ProgressBar extends ProgressBar {
Paint paint;
Rect rect;
String string = "請稍等,正在加載......";
int viewwidth, viewheight;
public PZHelp_ProgressBar(Context context) {
super(context);
initView();
}
public PZHelp_ProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public PZHelp_ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
void setText(String string){
this.string = string;
}
void initView() {
paint = new Paint();
rect = new Rect();
paint.setTextSize(32);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.getTextBounds(string, 0, string.length(), rect);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
viewwidth = getMeasuredWidth() + rect.width();
viewheight = Math.max(getMeasuredHeight(), rect.height());
setMeasuredDimension(viewwidth, viewheight);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.scale(0.5f, 0.5f);
canvas.translate(getHeight() >> 1, getHeight() >> 1);
super.onDraw(canvas);
canvas.drawText(string, (getWidth() + getHeight()) >> 1, (getMeasuredHeight() + rect.height()) >> 1, paint);
}
}
三、總結
- 最初是因爲官方推薦使用 ProgressBar 來代替 ProgressDialog,而 ProgressBar 並沒有添加文字的功能,所以才自定義的。
- ProgressDialog是繼承dialog,使用的是window,能夠做到隔絕頁面的用戶操作,而ProgressBar 沒有,但如果我也繼續寫一個window,那爲何不直接用ProgressDialog呢?似乎有點雞肋。
- 就當練習自定義View了,我還是接着用我 的dialog吧,下面是順帶的ViewGroup代碼
四、附
<com.example.myview_tets.MyViewGroup
android:id="@+id/myViewGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請稍等,正在加載......" />
</com.example.myview_tets.MyViewGroup>
public class MyViewGroup extends ViewGroup {
int viewWidth, viewHeight;
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
viewWidth = 0;viewHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
viewWidth = Math.max(viewWidth, child.getMeasuredWidth());
viewHeight = viewHeight + child.getMeasuredHeight();
}
setMeasuredDimension(viewWidth, viewHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childtop = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int childwidth = child.getMeasuredWidth();
int childheight = child.getMeasuredHeight();
int childleft = (viewWidth - childwidth) / 2;
child.layout(childleft, childtop, childleft + childwidth, childtop + childheight);
childtop += childheight;
}
}
}