Android- Draw line between two views

后端 未结 2 1362
礼貌的吻别
礼貌的吻别 2021-01-17 05:00

Below is my program where I have created three new views in a frame. On click of two different views I want to draw a line between the views. I am trying to figure out how t

相关标签:
2条回答
  • 2021-01-17 05:18

    For a draw line between your two views.

    Create class for view which draw a line.

    public class DrawView extends View {
        Paint paint = new Paint();
    
        public DrawView(Context context) {
            super(context);
            paint.setColor(Color.BLACK);
        }
    
        @Override
        public void onDraw(Canvas canvas) {
                canvas.drawLine(0, 50, 350, 50, paint);
        }
    
    }
    

    now from your activtiy where you want to add this line in your layout. Create object of this class and add this view in your layout.

    According to your requirment try like this.

    DrawView drawView; drawView = new DrawView(this);

    frame1.addView(ball1);
                                // add that view here
    frame1.addView(drawView);
    frame1.addView(ball2);
                                // same way here
    frame1.addView(ball3);
    

    For more Detail See Example

    0 讨论(0)
  • 2021-01-17 05:19

    Just draw line in onDraw() on some condition and set this condition in your activity in onTouch() method. Then call invalidate on Views that you changed their condition.

    0 讨论(0)
提交回复
热议问题