如何在Android布局中动态绘制线条

时间:2016-01-28 21:10:22

标签: android graphics line mindmapping

我做了什么:

Figure 1

我在寻找:

Figure 2

我不关心设计,但我不知道如何使用与第二张图像类似的线将按钮连接到主按钮。 注意:我正在动态创建按钮。因此,我不使用XML文件,因为我不知道我将有多少行/按钮。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);
    //
    RelativeLayout FirstLayout = (RelativeLayout)findViewById(R.id.second_layou);
    RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton = new Button(this);
    yourButton.setText("1");
    yourButton.setWidth(2);
    parms1.setMargins(w, h+250, 0, 0);
    FirstLayout.addView(yourButton, parms1);
    //
    RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton2 = new Button(this);
    yourButton2.setText("2");
    yourButton2.setWidth(2);
    parms2.setMargins(w-300, h+300, 0, 0);
    FirstLayout.addView(yourButton2, parms2);
    // and so on with other buttons

修改

首先:当我在onCreate()末尾添加此代码时尝试this回答:

drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

显示了一个线条视图,但按钮消失了!我希望从MAIN按钮的中心绘制线到其他按钮的中心,而不是从固定点。

第二次:当我在定义按钮之前添加相同的代码时,我遇到了运行时错误,这是logcat:

01-29 15:25:25.956 26170-26170/com.example.user.raywenderlich E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.example.user.raywenderlich, PID: 26170                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.raywenderlich/com.example.user.raywenderlich.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:155)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                                    at com.example.user.raywenderlich.SecondActivity.onCreate(SecondActivity.java:46)
                                                                                    at android.app.Activity.performCreate(Activity.java:5958)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:155) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5696) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

1 个答案:

答案 0 :(得分:1)

我实际上需要类似的东西,并且为此感到无能为力。

在搜索和合并信息后,我进入了这种状态:

Some textviews connected by curved lines.


我所做的是拿一个FrameLayout,所以TextViews可以堆放在图形View的顶部。我按照本教程的建议制作了一个自定义图形{​​{1}}:Basic Painting with Views

View

然后,我通过代码将<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root_layout"> <com.dev.example.CustomDrawingView android:id="@+id/custom_drawing_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </FrameLayout> 动态地添加到TextViews,并使用FrameLayout对象在Path中绘制从{{ 1}}:

CustomDrawingView

现在,曲线是根据以下问题得出的答案:How to draw a curved line between 2 points on canvas?

只需将答案中的TextView调整为具有不同外观的曲线即可。

希望这对以后的人有帮助。


编辑:哦,真正重要的是,在所有布局和视图均已完成之后,仅调用Path path = new Path(); int x1 = (int) firstView.getX() + firstView.getWidth() / 2; int y1 = (int) firstView.getY() + firstView.getHeight() / 2; int x2 = (int) secondView.getX() + secondView.getWidth() / 2; int y2 = (int) secondView.getY() + secondView.getHeight() / 2; path.moveTo(x1, y1); path.lineTo(x2, y2); 的{​​{1}}方法。显示。因为否则curveRadius的{​​{1}}方法仍然返回0,并且您将看不到任何行。
开始绘制它们的建议位置是在活动的onDraw方法中。