如何动态更改节点标签?

时间:2019-01-26 20:44:32

标签: android graph

我正在使用https://github.com/LordVulkan/Graphs库来创建自定义地图。每个节点将显示一个位置,顶点将显示从一个节点到另一节点的距离,如下图所示, enter image description here

现在,我想动态更改每个节点的名称和顶点。

为了测试,我写了一个函数

public void renameNode(Node n, String name) {
    if(n != null) {

        Node node = g.getVertex().get(0);
        canvas.drawText(name+" | "+node.getEnlacesExistentes(), node.getCenterX(), node.getCenterY()
                - node.radius - 20, fontPaint);
    }

}

并从MainActivity类view.renameNode(node, "zohaib");执行它,但应用程序崩溃了。

onDraw()负责标签名称

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    this.canvas = canvas;

    if(checked_iso)
    {
        for (int i = 0; i < gIso.getArrows().size(); i++) {
            Arrow a = gIso.getArrows().get(i);
            paint.setColor(Color.RED);
            canvas.drawLine(a.start[0], a.start[1], a.stop[0], a.stop[1], paint);

        }

        for (int ns : gIso.getNombres()) {
            Node n = gIso.getVertex().get(ns);
            n.setColor(Color.RED);
            n.draw(canvas);
            canvas.drawText(label[n.getId()]+" | "+n.getEnlacesExistentes(), n.getCenterX(), n.getCenterY()
                    - n.radius - 20, fontPaint);
        }

    }

    for (int i = 0; i < g.getArrows().size(); i++) {
        Arrow a = g.getArrows().get(i);
        paint.setColor(g.getArrows().get(i).color);
        canvas.drawLine(a.start[0], a.start[1], a.stop[0], a.stop[1], paint);
        if (!checked_iso) {
            path = new Path();
            path.moveTo(a.start[0], a.start[1]);
            path.lineTo(a.stop[0], a.stop[1]);
            canvas.drawTextOnPath(a.getWeightS(), path, 0, 30, fontPaint);

            path = new Path();
            path.moveTo(a.stop[0], a.stop[1]);
            path.lineTo(a.start[0], a.start[1]);
            canvas.drawTextOnPath(a.getWeightS(), path, 0, 30, fontPaint);

        }

    }

    if (aux != null) {
        canvas.drawLine(aux.start[0], aux.start[1], aux.stop[0],
                aux.stop[1], auxP);
    }

    for (int ns : g.getNombres()) {
        Node n = g.getVertex().get(ns);
        n.draw(canvas);
        canvas.drawText(label[n.getId()]+" | "+n.getEnlacesExistentes(), n.getCenterX(), n.getCenterY()
                - n.radius - 20, fontPaint);
    }

}

在上述代码中,该代码可用于分配标签canvas.drawText(label[n.getId()]+" | "+n.getEnlacesExistentes(), n.getCenterX(), n.getCenterY() - n.radius - 20, fontPaint); }

其中的标签为private static final String[] label = { "1","2","3","4","5","6","7","8","9","10"};

请帮助我该怎么做?

0 个答案:

没有答案