Android Drawing Line with 2 dots
I am currently developing an application where a string will be output from one point to another with a 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 - the class that draws a 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, he is drawing a line from the beginning using specific coordinates. I want the line to be drawn using coordinates from 2 points that are declared in the main action. And this function should work after onClick. Thanks in advance.
source to share
Use a customview extending the view class to achieve this: Call your own 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 will need to instantiate this function in your main activity. You can do it with 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);
}
});
}
}
source to share
You will need to create a custom view class that extends View
. Inside this class, you will override the method onDraw
that the canvas is passed to. create an object Paint
and use it with the function drawLine
above.
I would suggest looking for tutorials for Finger Paint apps. There are at least a few online and they are a good introduction to custom views and method overriding onDraw
.
source to share