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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.application.zhangshi_app_android.widget;
 
import android.content.Context;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;
import android.widget.ImageView;
 
import com.android.app_base.utils.ScreenSizeUtils;
import com.application.zhangshi_app_android.R;
import com.application.zhangshi_app_android.bean.HomeRootBean;
 
import java.util.List;
 
/**
 * @author Ljj
 * @date 2023.08.13. 7:39
 * @desc 双亲节点
 */
public class DoubleNode extends Node{
    private View memberItemView;//成员view
    private ImageView expandIconView;//展开按钮
    private View spouseView;//配偶view
 
    public DoubleNode(Context context,HomeRootBean member) {
        super(context,member);
        member.getSpouse().setSpouse(member);
        //双亲节点 前后路径长度 有两倍的 pathLength
        if (member.getIdentity() > 1 && member.getChildList() != null && member.getChildList().size() > 0){
            //非 第一代 且 有子代, 节点宽度 = view宽度 + 前后路径长度 + 展开按钮宽度
            width = pathLength * 2 + getItemViewWidth() + pathLength * 2 + getExpandIconWidth();
        }else if (member.getIdentity() > 1 && (member.getChildList() == null || member.getChildList().size() == 0)) {
            //非 第一代 且 无子代, 节点宽度 = view宽度 + 前路径长度
            width = getItemViewWidth() + pathLength * 2;
        }else if (member.getIdentity() == 1 && member.getChildList() != null && member.getChildList().size() > 0) {
            //第一代 且 有子代, 节点宽度 = view宽度 + 后路径长度 + 展开按钮宽度
            width = getItemViewWidth() + pathLength * 2+ getExpandIconWidth();
        }else if (member.getIdentity() == 1 && (member.getChildList() == null || member.getChildList().size() == 0)) {
            //第一代 且 无子代, 节点宽度 = view宽度
            width = getItemViewWidth();
        }
        //双亲节点 高度 = view高度 + 配偶view高度 + 配偶view上下间距
        height = getItemViewHeight() * 2 + spouseMargin;
        initView();
    }
 
    private void initView() {
        memberItemView = createItemView(member, true);
        spouseView = createItemView(member.getSpouse(),false);
        expandIconView = createExpandIconView();
        viewList.add(memberItemView);
        viewList.add(spouseView);
        viewList.add(expandIconView);
 
        if (isExpand){
            expandIconView.setBackgroundResource(R.drawable.ic_collapse);
        }else {
            expandIconView.setBackgroundResource(R.drawable.ic_expand);
        }
        expandIconView.setOnClickListener(v -> {
            if (isExpand){
                expandIconView.setBackgroundResource(R.drawable.ic_expand);
                for (Node child : children) {
                    child.setVisible(false);
                }
                isExpand = false;
            }else {
                expandIconView.setBackgroundResource(R.drawable.ic_collapse);
                for (Node child : children) {
                    child.setVisible(true);
                }
                isExpand = true;
            }
            memberItemView.requestLayout();
            spouseView.requestLayout();
        });
    }
 
    @Override
    protected void setViewPosition(int x, int centerY) {
        left = x;
        right = left + width;
        this.centerY = centerY;
        top = centerY - height/2;
        bottom = top + height;
 
        if (member.getIdentity() > 1) {//非 第一代 才有前置连线
            viewLeft = left + 2 * pathLength;
        } else {
            viewLeft = left;
        }
        memberItemView.setX(viewLeft);
        memberItemView.setY(centerY - memberItemView.getMeasuredHeight() - spouseMargin/2f);
        viewRight = viewLeft + memberItemView.getMeasuredWidth();
 
        spouseView.setX(viewLeft);
        spouseView.setY(centerY + spouseMargin/2f);
 
        viewTop = (int) memberItemView.getY();
        viewBottom = (int) (spouseView.getY() + spouseView.getMeasuredHeight());
 
        expandIconView.setX(viewRight + pathLength * 2);
        expandIconView.setY(centerY - expandIconView.getMeasuredHeight()/2f);
 
        setPath();
    }
 
    private void setPath() {
        //setPath 之前,先清空
        prePathList.clear();
        nextPathList.clear();
 
        if (member.getChildList() != null && member.getChildList().size() > 0){//有子节点,才有后置连线,才有展开按钮
            expandIconView.setVisibility(View.VISIBLE);
            float rLeft = viewRight;
            float rRight = viewRight + pathLength;
            float rTop = memberItemView.getY()+memberItemView.getMeasuredHeight()/2f;
            float rBottom = spouseView.getY()+spouseView.getMeasuredHeight()/2f;
            Path path = new Path();
            //左上角
            path.moveTo(rLeft, rTop);
            // ---
            path.lineTo(rRight - cornerRadius, rTop);
            // 添加圆弧路径,右上角的圆角
            path.arcTo(new RectF(rRight-2*cornerRadius, rTop, rRight, rTop+2*cornerRadius), 270, 90, false);
            // ----
            //    |
            //    |
            path.lineTo(rRight, rBottom - cornerRadius);
            // 添加圆弧路径,右下角的圆角
            path.arcTo(new RectF(rRight - 2 * cornerRadius, rBottom - 2 * cornerRadius, rRight, rBottom), 0, 90, false);
            // ----
            //    |
            //    |
            // ----
            path.lineTo(rLeft, rBottom);
            //移到右中间
            path.moveTo(rRight, centerY);
            // ----
            //    |____
            //    |
            // ----
            path.lineTo(rRight + pathLength, centerY);
            nextPathList.add(path);
 
        }else {
            expandIconView.setVisibility(View.GONE);
            viewList.remove(expandIconView);
        }
        if (member.getIdentity() > 1){//非 第一代 才有前置连线
            float rLeft = viewLeft - pathLength;
            float rRight = viewLeft;
            float rTop = memberItemView.getY()+memberItemView.getMeasuredHeight()/2f;
            float rBottom = spouseView.getY()+spouseView.getMeasuredHeight()/2f;
            Path path = new Path();
            //右上角
            path.moveTo(rRight, rTop);
            // ---
            path.lineTo(rLeft + cornerRadius, rTop);
            // 添加圆弧路径,左上角的圆角
            path.arcTo(new RectF(rLeft, rTop, rLeft+2*cornerRadius, rTop+2*cornerRadius), 270, -90, false);
            // ----
            // |
            // |
            path.lineTo(rLeft, rBottom - cornerRadius);
            // 添加圆弧路径,左下角的圆角
            path.arcTo(new RectF(rLeft, rBottom - 2 * cornerRadius, rLeft + 2 * cornerRadius, rBottom), 180, -90, false);
            // ----
            // |
            // |
            // ----
            path.lineTo(rRight, rBottom);
            //移到左中间
            path.moveTo(rLeft, centerY);
            //    ----
            // ___|
            //    |
            //    ----
            path.lineTo(rLeft - pathLength, centerY);
            prePathList.add(path);
        }
    }
 
    //设置分叉点Y坐标集合
    public void setForkYList(List<Integer> yList){
        if (yList == null || yList.size() < 2){
            return;
        }
        forkPathList.clear();
 
        //取出最大和最小
        int rBottom = 0;
        int rTop = 0;
        for (int i = 0; i < yList.size(); i++) {
            if (i == 0){
                rTop = yList.get(i);
                rBottom = yList.get(i);
            }else {
                if (yList.get(i) > rBottom){
                    rBottom = yList.get(i);
                }
                if (yList.get(i) < rTop){
                    rTop = yList.get(i);
                }
            }
        }
        yList.remove((Integer) rTop);
        yList.remove((Integer) rBottom);
 
        float rLeft = right + pathLength;
        float rRight = rLeft + pathLength;
        Path path = new Path();
        //右上角
        path.moveTo(rRight, rTop);
        // ---
        path.lineTo(rLeft + cornerRadius, rTop);
        // 添加圆弧路径,左上角的圆角
        path.arcTo(new RectF(rLeft, rTop, rLeft+2*cornerRadius, rTop+2*cornerRadius), 270, -90, false);
        // ----
        // |
        // |
        path.lineTo(rLeft, rBottom - cornerRadius);
        // 添加圆弧路径,左下角的圆角
        path.arcTo(new RectF(rLeft, rBottom - 2 * cornerRadius, rLeft + 2 * cornerRadius, rBottom), 180, -90, false);
        // ----
        // |
        // |
        // ----
        path.lineTo(rRight, rBottom);
        //移到左中间
        path.moveTo(rLeft, centerY);
        //    ----
        // ___|
        //    |
        //    ----
        path.lineTo(rLeft - pathLength, centerY);
        for (Integer y : yList) {
            path.moveTo(rLeft, y);
            path.lineTo(rRight, y);
        }
        forkPathList.add(path);
    }
 
}