guaishouN / android-thinkmap-treeview

Tree View; Mind map; Think map; tree map; custom view; 自定义;关系图;树状图;思维导图;组织机构图;层次图
MIT License
475 stars 66 forks source link

增加完整导图截图功能和导出文本功能 #10

Open onexuan opened 3 years ago

onexuan commented 3 years ago

可以增加完整导图截图功能和导出文本功能

文本功能如下:

地球
    海洋
         北冰洋
         大西洋
         太平洋
    陆地
         亚洲
         欧洲
guaishouN commented 3 years ago

可以增加完整导图截图功能和导出文本功能

文本功能如下:

地球
    海洋
         北冰洋
         大西洋
         太平洋
    陆地
         亚洲
         欧洲

如果作为一个通用的结构图控件,这方面你有具体的方案没有?比如json、xml...,一般用法有哪些? 因为我之前项目没有做复杂功能,编辑功能都是应别人请求做的

onexuan commented 3 years ago

我是试了序列化对象再转成hex string,这样app容易序列化与反序列化,但如果离开app,就只能导致出文本格式了,方便用户看文本。试过NodeModel导成Gson,会FC 编辑功能是有必要的

onexuan commented 3 years ago

我试过截view图,但文字比较小还有只有某个区域,得做区域截图并做全域滚动,再把图拼接。导出图片也是一种格式导出

hardlove commented 1 year ago

生成截图的功能我这边已经实现,可以参考:

 /**
     * 加载静态xml布局资源生成Bitmap
     *
     * @param context
     * @param layoutRes xml 布局资源ID
     * @param onInitCallBack 数据初始化回调
     */
    fun convertViewToBitmap(
        context: Context,
        @LayoutRes layoutRes: Int,
        onInitCallBack: (View)-> Unit
    ): Bitmap {
        val root = LayoutInflater.from(context).inflate(layoutRes, null)
        onInitCallBack(root)
        //测量
        val width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        val height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        root.measure(width, height)
        val measuredWidth = root.measuredWidth
        val measuredHeight = root.measuredHeight

        //再次测量(避免子View无法正常显示)
        root.measure(View.MeasureSpec.makeMeasureSpec(measuredWidth, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(measuredHeight, View.MeasureSpec.EXACTLY))

        //布局
        root.layout(0, 0, measuredWidth, measuredHeight)
        val bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        root.draw(canvas)
        return bitmap
    }
val bitmap =
                                                BitmapUtils.convertViewToBitmap(activity,
                                                        R.layout.save_xmind_layout) { rootView ->
                                                    val gysoTreeView =
                                                            rootView.findViewById<GysoTreeView>(
                                                                    R.id.tree_view)

                                                    val saveAdapter =
                                                            XMindTreeViewAdapter()
                                                    gysoTreeView.adapter = saveAdapter

                                                    gysoTreeView.setTreeLayoutManager(
                                                            getTreeLayoutManager())

                                                    saveAdapter.treeModel =
                                                            adapter.treeModel
                                                    saveAdapter.bindBaseTreeView(
                                                            gysoTreeView)
                                                    saveAdapter.getTreeViewEditor()
                                                            .focusMidLocation(false)
                                                }

目前还有给棘手的问题就是,如何把数据存储起来,序列化会直接报错,这比较麻烦,不知道大家有什么好办法没?

onexuan commented 1 year ago

@hardlove

public static String writeTreeObject(Object object) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(object); oos.close(); ByteString byteString = ByteString.of(out.toByteArray(),0,out.size()); return byteString.hex(); }

public static Object readTreeObject(String hex) throws Exception {
    ByteArrayInputStream in=new ByteArrayInputStream(ByteString.decodeHex(hex).toByteArray());
    ObjectInputStream oin = new ObjectInputStream(in);
    Object obj = oin.readObject();
    oin.close();
    return obj;
}