Android——自定義ProgressBar顯示文字(有缺陷)

一、效果

在這裏插入圖片描述

二、代碼

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();
        //因爲繪製時需要縮放二分之一,所以這裏放大一倍使用32
        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);
        //讓顯示的內容居中,注意,這裏是先執行了translate後執行了scale
        canvas.translate(getHeight() >> 1, getHeight() >> 1);
        //繪製原本的ProgressBar
        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);

        //讓所有子View計算自己的尺寸
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //獲取子View的尺寸
        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;

        //計算子View的佈局
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            //獲取子View的尺寸
            int childwidth = child.getMeasuredWidth();
            int childheight = child.getMeasuredHeight();
            //爲了view水平居中而計算的左邊距
            int childleft = (viewWidth - childwidth) / 2;
            child.layout(childleft, childtop, childleft + childwidth, childtop + childheight);
            childtop += childheight;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章