title: How to Draw straignt Line between two Points?
body: <p>I am trying to achieve that one point called <code>first</code> in my code to join second point <code>second</code> using finger movement from <code>first</code> point to <code>second</code> , <em>Line should be straight no matter what</em> , and if <code>first</code> point didn't reach <code>second</code> point , line should disappear ON ACTION_UP.</p>
  
  <p>I've got my inspiration from :
  <a href="https://play.google.com/store/apps/details?id=nf.co.sumwu.dropdot&amp;hl=en" rel="nofollow">https://play.google.com/store/apps/details?id=nf.co.sumwu.dropdot&amp;hl=en</a></p>
  
  <p>I am trying to achieve it through onDraw method with CustomView</p>
  
  <p>Here is my attempt:</p>
  
  <pre><code>public class MyGFX extends View{
  
  // setup initial color
    private final int paintColor = Color.BLACK;
    // defines paint and canvas
    private Paint drawPaint;
  
    Point first , second;
  
    private Path path = new Path();
  
  public MyGFX(Context context,AttributeSet attrs) {
      super(context, attrs);
      setupPaint(); // same as before
      first = new Point(100,100);
      second = new Point(200,100);
  }
  
  // Get x and y and append them to the path
  public boolean onTouchEvent(MotionEvent event) {
      float pointX = event.getX();
      float pointY = event.getY();
      // Checks for the event that occurs
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          boolean insideCircle = 
                  (2*((int)pointX - (first.x + 2))) + (2*((int)pointY - (first.y + 2))) &lt;= (2 * 2);
  
          if(insideCircle){
              // Starts a new line in the path
              path.moveTo(first.x,first.y);
              Toast.makeText(getContext(), "path.moveto is called \n first.x = "
              +first.x+" first.y = "+first.y+" px ="+pointX+"  py ="+pointY, 
                      Toast.LENGTH_SHORT).show();
          }
  
          break;
      case MotionEvent.ACTION_MOVE:
          boolean insideCircle2 = 
          (2*(pointX - (second.x + 2))) + (2*(pointY - (second.y + 2))) &lt;= (2 * 2);
  
          if(insideCircle2){
              // Starts a new line in the path
              path.moveTo(pointX, pointY);
          }
          // Draws line between last point and this point
          path.lineTo(pointX, pointY);
          postInvalidate(); // Indicate view should be redrawn
  
          break;
      case MotionEvent.ACTION_UP:
          path.close();
          break;
      default:
          return false;
     }
  
     return true; // Indicate we've consumed the touch
  }
  
   // Setup paint with color and stroke styles
    private void setupPaint() {
      drawPaint = new Paint();
      drawPaint.setColor(paintColor);
      drawPaint.setAntiAlias(true);
      drawPaint.setStrokeWidth(5);
      drawPaint.setStyle(Paint.Style.STROKE);
      drawPaint.setStrokeJoin(Paint.Join.ROUND);
      drawPaint.setStrokeCap(Paint.Cap.ROUND);
    }
  
  // Draws the path created during the touch events
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(first.x, first.y, 2, drawPaint);
        canvas.drawCircle(second.x, second.y, 2, drawPaint);
  
        if(!path.isEmpty())
            canvas.drawPath(path, drawPaint);
  
  
    }
  
  
  }
  </code></pre>