Search code examples
javaandroiddrawing

Drawing Between Two Points on an Android Emulator in Java


Given two points on an emulator (x and y values for both coordinates), how do I draw a line connecting them?

I have already retrieved the two coordinates like this...

//imports not included
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(iv.getLayoutParams());
float fx = event.getX();
float fy = event.getY();
int x = (int)fx;
int y = (int)fy;

Solution

  • Use Canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint) method to draw a straight line between two points using X, Y coordinates like the following Code Snippet.

    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();

    paint.setStrokeWidth(5);

    paint.setColor(Color.Black);

    paint.setStyle(Paint.Style.STROKE);

    paint.setAntiAlias(true);

    Canvas.drawLine(startX, startY, stopX, stopY, paint);