一、效果
二、代码
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
;
}
}
}