How to draw a line dynamically in Android layout

China☆狼群 提交于 2021-02-19 03:42:08

问题


What I have done:

What I am looking for:

I do not care about design, but I do not know how to connect buttons to Main button with lines that are similar to the second image. Note: I am creating buttons dynamically. Thus, I do not use XML file as I do not know how many lines/buttons I will have.

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

Edit:

First: When I tried this answer by adding this code at the end of onCreate():

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

a line view is shown but buttons are gone! and I want the line to be drawn from the center of MAIN button to center of other buttons not from fixed points.

Second: when I add the same code before defining the buttons I got runtime error, and this is 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:


I actually needed something similar and was bummed that there was no answer to this.

After some searching and combining of information I got to this state:

Some textviews connected by curved lines.


What I did was take a FrameLayout, so the TextViews could be stacked on top of the drawing View. I made a custom drawing View as suggested in this tutorial: Basic Painting with Views.

<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>

Then I add the TextViews dynamically via code to the FrameLayout and use Path objects to draw the lines in the CustomDrawingView, from center to center of each 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);

Now the curve is made with the answer from this question: How to draw a curved line between 2 points on canvas?

Simply adjust the curveRadius in that answer to have different looking curves.

Hope this helps someone in the future.


EDIT: Oh and what's really important is to only call the onDraw method of the CustomDrawingView after all the layouts and views have been displayed. Because else the TextView's getX() & getY() methods still return 0 and you won't see any lines.
The suggested position to start drawing them was in the onWindowFocusChanged method of an activity.



来源:https://stackoverflow.com/questions/35072106/how-to-draw-a-line-dynamically-in-android-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!