简体   繁体   中英

Android Drawing Line with 2 points

Currently I'm developing an app, where line will be drawn from one point to another with button click on a bitmap. Here's my code in MainActivity.java:

    public class MainActivity extends Activity {
    LineView lineview;
    Button button;

    @Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   //lineview = (LineView)findViewById (R.id.lineView1);
   button = (Button)findViewById(R.id.btnCapture); 
   //lineview.setVisibility(View.INVISIBLE);
   button.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {

          // lineview.setVisibility(View.VISIBLE);
       }
   });


 }
}

LineView.java - class that draws line

public class LineView extends View {
Paint paint = new Paint();

public LineView(Context context, AttributeSet attrs) {
  super(context, attrs);
  }

public LineView(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle );
  }


public LineView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
    canvas.drawLine(0, 0, 20, 20, paint);
}

}

Right now it draws the line from the start using defined coordinates. I want line to be drawn using coordinates from 2 points that get declared in main activity. And that function should work after onClick. Thanks in advance.

You will have to create a custom view class that extends View . Within that class you will override the onDraw method which is passed a canvas. create a Paint object and use it with the drawLine function above.

I would suggest looking up Finger Paint app tutorials. There are at least a few online, and they are a good introduction to custom views and overriding the onDraw method.

Use customview by extending the view class to achieve this: Let's call your custom class say LineView. So this is what Line should look like.

LineView.java

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.View;

    public class LineView extends View {
    Paint paint = new Paint();

  public LineView(Context context, AttributeSet attrs) {
      super(context, attrs);
      }

    public LineView(Context context, AttributeSet attrs, int defstyle) {
    super(context, attrs, defstyle );
      }


    public LineView(Context context) {
    super(context);
    paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 20, 20, paint);
}

}

Now, you'll have to instantiate this in your mainactivity. You can do that using java code or xml. Using java code it will look like this:

   import android.app.Activity;
   import android.graphics.Color;
   import android.os.Bundle;

 public class MainActivity extends Activity {
     LineView lineview;
     Button button;

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lineview = (LineView)findViewById (R.id.lineView1);
    button = (Button)findViewById(R.id.button1); 
    lineview.setVisibility(View.INVISIBLE);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            lineview.setVisibility(View.VISIBLE);
        }
    });


  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM