kimi
2021-02-18 0ac056bb5b4c567293482286c80a1b83a467cd33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mylhyl.circledialog.view;
 
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
 
import androidx.cardview.widget.CardView;
 
import com.mylhyl.circledialog.internal.BuildView;
import com.mylhyl.circledialog.internal.CircleParams;
import com.mylhyl.circledialog.internal.Controller;
import com.mylhyl.circledialog.params.CloseParams;
import com.mylhyl.circledialog.view.listener.ButtonView;
import com.mylhyl.circledialog.view.listener.CloseView;
 
/**
 * view的层次结构
 * <pre>
 * CardView
 *    ╚--LinearLayout
 *          ╚--TitleView
 *          ╚--BodyView
 *          ╚--ButtonView
 * </pre>
 * Created by hupei on 2017/3/29.
 */
 
abstract class AbsBuildView implements BuildView {
    private static final double COS_45 = Math.cos(Math.toRadians(45));
    protected Context mContext;
    protected CircleParams mParams;
    protected ViewGroup mRoot;
    private LinearLayout mRootCardViewByLinearLayout;
    private TitleView mTitleView;
    private ButtonView mButtonView;
 
    public AbsBuildView(Context context, CircleParams params) {
        this.mContext = context;
        this.mParams = params;
    }
 
    protected final View layoutInflaterFrom(int resource) {
        return LayoutInflater.from(mContext).inflate(resource, mRootCardViewByLinearLayout, false);
    }
 
    protected final void addViewByBody(View child) {
        ViewParent parent = child.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(child);
        }
        mRootCardViewByLinearLayout.addView(child);
    }
 
    @Override
    public void buildRootView() {
        createLinearLayout();
        if (Controller.SDK_LOLLIPOP) {
            CardView cardView = createCardView();
            cardView.addView(mRootCardViewByLinearLayout);
            if (mParams.closeParams == null) {
                mRoot = cardView;
            } else {
                LinearLayout rootLayout = new LinearLayout(mContext);
                rootLayout.setBackgroundColor(Color.TRANSPARENT);
                rootLayout.setOrientation(LinearLayout.VERTICAL);
                rootLayout.addView(cardView);
                mRoot = rootLayout;
            }
        } else {
            mRoot = mRootCardViewByLinearLayout;
        }
    }
 
    @Override
    public void buildTitleView() {
        if (mParams.titleParams != null) {
            mTitleView = new TitleView(mContext, mParams);
            mRootCardViewByLinearLayout.addView(mTitleView);
        }
    }
 
    @Override
    public ButtonView buildButton() {
        mButtonView = new ConfirmButton(mContext, mParams);
        if (!mButtonView.isEmpty()) {
            DividerView dividerView = new DividerView(mContext, LinearLayout.HORIZONTAL);
            mRootCardViewByLinearLayout.addView(dividerView);
        }
        mRootCardViewByLinearLayout.addView(mButtonView.getView());
        return mButtonView;
    }
 
    @Override
    public CloseView buildCloseImgView() {
        CloseParams closeParams = mParams.closeParams;
        CloseImgView closeView = new CloseImgView(mContext, closeParams);
        LinearLayout.LayoutParams layoutParamsClose = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //位置
        if (closeParams.closeGravity == CloseParams.CLOSE_TOP_LEFT
                || closeParams.closeGravity == CloseParams.CLOSE_BOTTOM_LEFT) {
            layoutParamsClose.gravity = Gravity.LEFT;
        } else if (closeParams.closeGravity == CloseParams.CLOSE_TOP_CENTER
                || closeParams.closeGravity == CloseParams.CLOSE_BOTTOM_CENTER) {
            layoutParamsClose.gravity = Gravity.CENTER_HORIZONTAL;
        } else {
            layoutParamsClose.gravity = Gravity.RIGHT;
        }
        closeView.setLayoutParams(layoutParamsClose);
        if (closeParams.closeGravity == CloseParams.CLOSE_TOP_LEFT
                || closeParams.closeGravity == CloseParams.CLOSE_TOP_CENTER
                || closeParams.closeGravity == CloseParams.CLOSE_TOP_RIGHT) {
            mRoot.addView(closeView, 0);
        } else {
            mRoot.addView(closeView);
        }
        return closeView;
    }
 
    @Override
    public final View getRootView() {
        return mRoot;
    }
 
    @Override
    public void refreshTitle() {
        if (mTitleView != null) {
            mTitleView.refreshText();
        }
    }
 
    @Override
    public final void refreshButton() {
        if (mButtonView != null) {
            mButtonView.refreshText();
        }
    }
 
    protected CardView createCardView() {
        int radius = Controller.dp2px(mContext, mParams.dialogParams.radius);
 
        CardView cardView = new CardView(mContext);
        cardView.setCardElevation(0f);
 
        if (Controller.SDK_LOLLIPOP) {
            cardView.setCardBackgroundColor(Color.TRANSPARENT);
        } else {
            cardView.setPreventCornerOverlap(false);
            cardView.setCardBackgroundColor(mParams.dialogParams.backgroundColor);
            cardView.setUseCompatPadding(true);
            int contentPadding = (int) Math.ceil(radius - COS_45 * radius);
            cardView.setContentPadding(contentPadding, contentPadding, contentPadding, contentPadding);
        }
        cardView.setRadius(radius);
        return cardView;
    }
 
    protected LinearLayout createLinearLayout() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        return mRootCardViewByLinearLayout = linearLayout;
    }
}