| | |
| | | private Paint linePaint; |
| | | private HomeRootBean rootMember;// 根节点成员 |
| | | private Node rootNode;// 根节点 |
| | | private Map<HomeRootBean, View> viewMap = new HashMap<>();// 存放所有节点的view,记录节点的view是否已被addView |
| | | |
| | | private int width, height;// 当前布局的宽高 |
| | | private int minHeight, minWidth;// 当前布局的最小宽高 |
| | | private int padding = 100;//内容矩形 与 FrameLayout矩形 的padding |
| | | private static final int spouseMargin = ScreenSizeUtils.dip2px(15);// 配偶两个view之间的间隔 |
| | | private final int verticalMargin = ScreenSizeUtils.dip2px(20);// //节点竖直方向间距 |
| | | |
| | | private static final int pathLength = ScreenSizeUtils.dip2px(20);// 线条长度 |
| | | |
| | | private OnItemClickListener onItemClickListener;// 点击事件 |
| | | public HomeMindMapLayout(@NonNull Context context) { |
| | | this(context, null); |
| | | } |
| | |
| | | |
| | | private void setRootMemberReal(HomeRootBean root) { |
| | | this.rootMember = root; |
| | | // 清空viewMap |
| | | viewMap.clear(); |
| | | if (rootMember != null) { // 绘制根节点,在竖直方向上居中 |
| | | if (rootMember.getSpouse() == null){ |
| | | rootNode = new SimpleNode(rootMember); |
| | | rootNode = new SimpleNode(getContext(),rootMember); |
| | | }else { |
| | | rootNode = new DoubleNode(rootMember); |
| | | rootNode = new DoubleNode(getContext(),rootMember); |
| | | } |
| | | rootNode.setPosition(padding,padding); |
| | | //把所有节点的view添加到viewGroup中 |
| | | addNodeView(rootNode); |
| | | requestLayout(); |
| | | if (onItemClickListener != null){ |
| | | rootNode.setOnItemClickListener(onItemClickListener); |
| | | } |
| | | } |
| | | } |
| | | public void setOnItemClickListener(OnItemClickListener listener){ |
| | | this.onItemClickListener = listener; |
| | | if (rootNode != null){ |
| | | rootNode.setOnItemClickListener(onItemClickListener); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | setMeasuredDimension(Math.max(minWidth,width),Math.max(minHeight,height)); |
| | | |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | } |
| | | |
| | | private ImageView createExpandIconView() { |
| | | ImageView expandView = new ImageView(getContext()); |
| | | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(pathLength,pathLength); |
| | | expandView.setLayoutParams(params); |
| | | int widthMeasureSpec = MeasureSpec.makeMeasureSpec(pathLength, MeasureSpec.EXACTLY); |
| | | int heightMeasureSpec = MeasureSpec.makeMeasureSpec(pathLength, MeasureSpec.EXACTLY); |
| | | expandView.measure(widthMeasureSpec,heightMeasureSpec); |
| | | return expandView; |
| | | } |
| | | |
| | | private View createItemView(HomeRootBean member) { |
| | | // 使用布局填充器加载节点布局 |
| | | LayoutInflater inflater = LayoutInflater.from(getContext()); |
| | | View familyMemberView = inflater.inflate(R.layout.item_family_member, null, false); |
| | | |
| | | familyMemberView.measure(MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST)); |
| | | // 获取测量后的宽高 |
| | | int width = familyMemberView.getMeasuredWidth(); |
| | | int height = familyMemberView.getMeasuredHeight(); |
| | | |
| | | // 添加到父布局前再次测量 |
| | | familyMemberView.measure( |
| | | View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), |
| | | View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY) |
| | | ); |
| | | |
| | | if (member == null) { |
| | | return familyMemberView; |
| | | } |
| | | |
| | | // 获取布局中的组件 |
| | | TextView tv_generation = familyMemberView.findViewById(R.id.tv_generation); |
| | | ImageFilterView iv_avatar = familyMemberView.findViewById(R.id.iv_avatar); |
| | | TextView tv_name = familyMemberView.findViewById(R.id.tv_name); |
| | | |
| | | // 设置节点内容 |
| | | tv_generation.setText(String.valueOf(member.getIdentity())); |
| | | if (member.getImg() != null) { |
| | | GlideUtil.loadImage(member.getImg(), iv_avatar); |
| | | } |
| | | tv_name.setText(member.getNickName()); |
| | | |
| | | return familyMemberView; |
| | | } |
| | | |
| | | public void setMinSize(int minWidth, int minHeight) { |
| | | this.minWidth = minWidth; |
| | | this.minHeight = minHeight; |
| | | } |
| | | |
| | | |
| | | |
| | | public interface OnItemClickListener{ |
| | | void onItemClick(View view,HomeRootBean member); |
| | | } |
| | | |
| | | } |