title: Android - Draw a straight line
body: <p>I am trying to draw a straight line. I want it to work so you drag your finger across the screen and it draws it straight. My code now begins drawing the line where the last one ended. How would I do this?? Thanks!
  Here is the code:</p>
  
  <pre><code>package barnquilt.nkc.com.barnquiltmaker.Qulits;
  
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.graphics.Point;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import barnquilt.nkc.com.barnquiltmaker.BitMap.blue;
  import barnquilt.nkc.com.barnquiltmaker.BitMap.red;
  import barnquilt.nkc.com.barnquiltmaker.R;
  import barnquilt.nkc.com.barnquiltmaker.UI.Line;
  import barnquilt.nkc.com.barnquiltmaker.BitMap.bitmapInfo;
  /**
   * Created by pokem on 6/1/2017.
   */
  
      public class MainDrawingView extends View  {
  
    Path path = new Path();
      public float eventX;
      public float eventY;
  
     public List&lt;Point&gt;redPointList = new ArrayList&lt;Point&gt;();
      public List&lt;Point&gt;bluePointList = new ArrayList&lt;Point&gt;();
  
  
  
  
      Context context = getContext();
     //Bitmaps
     public Bitmap redSquare;
     public Bitmap blueSquare;
  
  
  
      public int width = context.getResources().getDisplayMetrics().widthPixels;
      public int height = context.getResources().getDisplayMetrics().heightPixels;
      public int center  = height/2;
      public int widthDiv = width/2;
  
  
      private Paint redPaint  = new Paint();
      private Paint bluePaint = new Paint();
      private Paint black = new Paint();
  
  
  
  
      public MainDrawingView(final Context context, AttributeSet attrs){
          super(context, attrs);
          redPaint.setAntiAlias(true);
          redPaint.setStrokeWidth(5f);
          redPaint.setColor(Color.RED);
          redPaint.setStyle(Paint.Style.STROKE);
          redPaint.setStrokeJoin(Paint.Join.ROUND);
  
  
          bluePaint.setAntiAlias(true);
          bluePaint.setStrokeWidth(5f);
          bluePaint.setColor(Color.BLUE);
          bluePaint.setStyle(Paint.Style.STROKE);
          bluePaint.setStrokeJoin(Paint.Join.ROUND);
  
  
          black.setAntiAlias(true);
          black.setStrokeWidth(5f);
          black.setColor(Color.BLACK);
          black.setStyle(Paint.Style.STROKE);
          black.setStrokeJoin(Paint.Join.ROUND);
  
  
          //define bitmap
          redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.red);
          blueSquare = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
  
  
  
  
  
      }
  
      @Override
      public void onDraw(Canvas canvas){
  
         //Makes a straight line go through the center of the screen.
         canvas.drawLine(0,height/2.5f, width,height/2.5f,black);
         //Makes a straight line go up and down.
          canvas.drawLine(width/2.3f,0, width/2.3f, height,black);
  
  
               canvas.drawPath(path, bluePaint);
  
               for(Point point : redPointList){
  
                  new red(redSquare,canvas,point.x, point.y);
  
               }
  
              for(Point pointt : bluePointList){
                    new blue(blueSquare,canvas,pointt.x, pointt.y);
              }
  
  
  
  
  
  
  
  
  
  
      }
  
  
  
      @Override
      public boolean onTouchEvent( MotionEvent event){
  
  
  
          bitmapInfo red = new bitmapInfo(redSquare);
  
          String height = String.valueOf(red.getBitmapHeight());
          String width = String.valueOf(red.getBitmapWidth());
          Toast.makeText(getContext(), height, Toast.LENGTH_SHORT).show();
  
  
  
  
           eventX = event.getX();
           eventY = event.getY();
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
          switch(event.getAction()){
  
              case MotionEvent.ACTION_DOWN:
  
                  path.lineTo(eventX  ,eventY );
              break;
  
              case MotionEvent.ACTION_MOVE:
  
                  break;
  
              case MotionEvent.ACTION_UP:
                 //  alertMessage();
                  break;
  
  
  
  
              default:
                  return false;
          }
          invalidate();
          return true;
      }
  
  
   public void alertMessage(){
       AlertDialog.Builder message = new AlertDialog.Builder(context);
       message.setTitle("choose a color");
       message.setMessage("Colors:");
       message.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();
  
               invalidate();
           }
       });
       message.setPositiveButton("Change color to red", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
             Point redPointt  =new Point();
             redPointt.x = (int)eventX;
             redPointt.y = (int)eventY;
             redPointList.add(redPointt);
               previewLine.removeAll(previewLine);
               invalidate();
           }
       });
       message.setNeutralButton("Change color to blue", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
  
  
               Point bluePointt  =new Point();
               bluePointt.x = (int)eventX;
               bluePointt.y = (int)eventY;
               bluePointList.add(bluePointt);
               previewLine.removeAll(previewLine);
               invalidate();
           }
       });
       message.create();
       message.show();
   }
  
  
  
  
  
  }
  </code></pre>