Draw Line in View using Vector (in Android Studio)












0















I'm trying to draw a line, I managed to do it before but the first point somehow was always 0, 0. Now my logic is, there are two Vectors, one to store each point that is clicked, and another to store the Line, which is made by two points clicked by the user. I do the verifications to see if the Vector is not empty and only then I draw the line. I don't really know what's going on, I've tried everything, hope some of you can help, I'm in real need. Thank you.
Here it goes the code from MyView.java:



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Vector;

public class MyView extends View {
Paint paint = null;
int figure;
int lados_poly;
int cor;
int deletar;
int CursorX, CursorY;
int nrCliques;

Vector<Ponto2D> ptsReta = new Vector<Ponto2D>();
Vector<Reta> guardaRetas = new Vector<Reta>();

public MyView(Context context) {
super(context);
paint = new Paint();
figure = 0;
cor = 0;
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setStrokeWidth(10);
figure = 0;
cor = 0;
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
figure = 0;
cor = 0;
}


public void clickEcra() {
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:

case MotionEvent.ACTION_UP:

CursorX = (int)event.getX();
CursorY = (int)event.getY();

if (figure == 2){
Ponto2D ptReta = new Ponto2D();
ptReta.x = CursorX;
ptReta.y = CursorY;
ptsReta.add(ptReta);
if (ptsReta.size()> 0) {
for (int c = 0; c < ptsReta.size(); c++)
System.out.println("ptRetaX: " + ptsReta.get(c).x + " ptRetaY: " + ptsReta.get(c).y + " size " + ptsReta.size());
}
invalidate();
}

default:
return false;
}
}
});
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
clickEcra();

paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#F5F1E0"));
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#3F5866"));

//cores
if (cor == 1) {
paint.setColor(Color.parseColor("#393E46"));
} else if (cor == 2){
paint.setColor(Color.parseColor("#00ADB5"));
} else if (cor == 3) {
paint.setColor(Color.parseColor("#F8B500"));
} else if (cor == 4) {
paint.setColor(Color.parseColor("#FC3C3C"));
}


if (figure == 2) {
if (ptsReta.size() >= 2) {
for (int b = 0; b < ptsReta.size(); b = b + 2) {
Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
guardaRetas.add(retinha);
System.out.println("pts: " + ptsReta.get(b) + ptsReta.get(b + 1));
}
}

if (guardaRetas.size() > 0) {
for (int r = 0; r < guardaRetas.size(); r++) {
canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
}
}
}

//clear canvas
if (deletar == 2){
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#F5F1E0"));
canvas.drawPaint(paint);
nrCliques = 0;
ptsCirc.removeAllElements();
ptsReta.removeAllElements();
}
}


public void setfigure(int a) {
this.figure = a;
}

public void Cor1_mudar(int text_cor) {
this.cor = text_cor;
}

public void Resetar(int delete){
this.deletar = delete;
}
}


And now the code from Line.java:



public class Reta {
Ponto2D pinicial;
Ponto2D pfinal;

public Reta(){
pinicial = new Ponto2D();
pfinal = new Ponto2D();
}

public Reta(Ponto2D a, Ponto2D b) {
pinicial = a;
pfinal = b;
}
}


Update: It draws one line, and when I try to do another one, on the third click, it closes and I need to draw multiple lines in my canvas.










share|improve this question





























    0















    I'm trying to draw a line, I managed to do it before but the first point somehow was always 0, 0. Now my logic is, there are two Vectors, one to store each point that is clicked, and another to store the Line, which is made by two points clicked by the user. I do the verifications to see if the Vector is not empty and only then I draw the line. I don't really know what's going on, I've tried everything, hope some of you can help, I'm in real need. Thank you.
    Here it goes the code from MyView.java:



    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import java.util.Vector;

    public class MyView extends View {
    Paint paint = null;
    int figure;
    int lados_poly;
    int cor;
    int deletar;
    int CursorX, CursorY;
    int nrCliques;

    Vector<Ponto2D> ptsReta = new Vector<Ponto2D>();
    Vector<Reta> guardaRetas = new Vector<Reta>();

    public MyView(Context context) {
    super(context);
    paint = new Paint();
    figure = 0;
    cor = 0;
    }

    public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
    paint.setStrokeWidth(10);
    figure = 0;
    cor = 0;
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    paint = new Paint();
    figure = 0;
    cor = 0;
    }


    public void clickEcra() {
    setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:

    case MotionEvent.ACTION_UP:

    CursorX = (int)event.getX();
    CursorY = (int)event.getY();

    if (figure == 2){
    Ponto2D ptReta = new Ponto2D();
    ptReta.x = CursorX;
    ptReta.y = CursorY;
    ptsReta.add(ptReta);
    if (ptsReta.size()> 0) {
    for (int c = 0; c < ptsReta.size(); c++)
    System.out.println("ptRetaX: " + ptsReta.get(c).x + " ptRetaY: " + ptsReta.get(c).y + " size " + ptsReta.size());
    }
    invalidate();
    }

    default:
    return false;
    }
    }
    });
    }


    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    clickEcra();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.parseColor("#F5F1E0"));
    canvas.drawPaint(paint);
    paint.setColor(Color.parseColor("#3F5866"));

    //cores
    if (cor == 1) {
    paint.setColor(Color.parseColor("#393E46"));
    } else if (cor == 2){
    paint.setColor(Color.parseColor("#00ADB5"));
    } else if (cor == 3) {
    paint.setColor(Color.parseColor("#F8B500"));
    } else if (cor == 4) {
    paint.setColor(Color.parseColor("#FC3C3C"));
    }


    if (figure == 2) {
    if (ptsReta.size() >= 2) {
    for (int b = 0; b < ptsReta.size(); b = b + 2) {
    Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
    guardaRetas.add(retinha);
    System.out.println("pts: " + ptsReta.get(b) + ptsReta.get(b + 1));
    }
    }

    if (guardaRetas.size() > 0) {
    for (int r = 0; r < guardaRetas.size(); r++) {
    canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
    }
    }
    }

    //clear canvas
    if (deletar == 2){
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.parseColor("#F5F1E0"));
    canvas.drawPaint(paint);
    nrCliques = 0;
    ptsCirc.removeAllElements();
    ptsReta.removeAllElements();
    }
    }


    public void setfigure(int a) {
    this.figure = a;
    }

    public void Cor1_mudar(int text_cor) {
    this.cor = text_cor;
    }

    public void Resetar(int delete){
    this.deletar = delete;
    }
    }


    And now the code from Line.java:



    public class Reta {
    Ponto2D pinicial;
    Ponto2D pfinal;

    public Reta(){
    pinicial = new Ponto2D();
    pfinal = new Ponto2D();
    }

    public Reta(Ponto2D a, Ponto2D b) {
    pinicial = a;
    pfinal = b;
    }
    }


    Update: It draws one line, and when I try to do another one, on the third click, it closes and I need to draw multiple lines in my canvas.










    share|improve this question



























      0












      0








      0








      I'm trying to draw a line, I managed to do it before but the first point somehow was always 0, 0. Now my logic is, there are two Vectors, one to store each point that is clicked, and another to store the Line, which is made by two points clicked by the user. I do the verifications to see if the Vector is not empty and only then I draw the line. I don't really know what's going on, I've tried everything, hope some of you can help, I'm in real need. Thank you.
      Here it goes the code from MyView.java:



      import android.content.Context;
      import android.graphics.Canvas;
      import android.graphics.Color;
      import android.graphics.Paint;
      import android.util.AttributeSet;
      import android.view.MotionEvent;
      import android.view.View;
      import java.util.Vector;

      public class MyView extends View {
      Paint paint = null;
      int figure;
      int lados_poly;
      int cor;
      int deletar;
      int CursorX, CursorY;
      int nrCliques;

      Vector<Ponto2D> ptsReta = new Vector<Ponto2D>();
      Vector<Reta> guardaRetas = new Vector<Reta>();

      public MyView(Context context) {
      super(context);
      paint = new Paint();
      figure = 0;
      cor = 0;
      }

      public MyView(Context context, AttributeSet attrs) {
      super(context, attrs);
      paint = new Paint();
      paint.setStrokeWidth(10);
      figure = 0;
      cor = 0;
      }

      public MyView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      paint = new Paint();
      figure = 0;
      cor = 0;
      }


      public void clickEcra() {
      setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

      switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:

      case MotionEvent.ACTION_UP:

      CursorX = (int)event.getX();
      CursorY = (int)event.getY();

      if (figure == 2){
      Ponto2D ptReta = new Ponto2D();
      ptReta.x = CursorX;
      ptReta.y = CursorY;
      ptsReta.add(ptReta);
      if (ptsReta.size()> 0) {
      for (int c = 0; c < ptsReta.size(); c++)
      System.out.println("ptRetaX: " + ptsReta.get(c).x + " ptRetaY: " + ptsReta.get(c).y + " size " + ptsReta.size());
      }
      invalidate();
      }

      default:
      return false;
      }
      }
      });
      }


      @Override
      protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      clickEcra();

      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.parseColor("#F5F1E0"));
      canvas.drawPaint(paint);
      paint.setColor(Color.parseColor("#3F5866"));

      //cores
      if (cor == 1) {
      paint.setColor(Color.parseColor("#393E46"));
      } else if (cor == 2){
      paint.setColor(Color.parseColor("#00ADB5"));
      } else if (cor == 3) {
      paint.setColor(Color.parseColor("#F8B500"));
      } else if (cor == 4) {
      paint.setColor(Color.parseColor("#FC3C3C"));
      }


      if (figure == 2) {
      if (ptsReta.size() >= 2) {
      for (int b = 0; b < ptsReta.size(); b = b + 2) {
      Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
      guardaRetas.add(retinha);
      System.out.println("pts: " + ptsReta.get(b) + ptsReta.get(b + 1));
      }
      }

      if (guardaRetas.size() > 0) {
      for (int r = 0; r < guardaRetas.size(); r++) {
      canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
      }
      }
      }

      //clear canvas
      if (deletar == 2){
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.parseColor("#F5F1E0"));
      canvas.drawPaint(paint);
      nrCliques = 0;
      ptsCirc.removeAllElements();
      ptsReta.removeAllElements();
      }
      }


      public void setfigure(int a) {
      this.figure = a;
      }

      public void Cor1_mudar(int text_cor) {
      this.cor = text_cor;
      }

      public void Resetar(int delete){
      this.deletar = delete;
      }
      }


      And now the code from Line.java:



      public class Reta {
      Ponto2D pinicial;
      Ponto2D pfinal;

      public Reta(){
      pinicial = new Ponto2D();
      pfinal = new Ponto2D();
      }

      public Reta(Ponto2D a, Ponto2D b) {
      pinicial = a;
      pfinal = b;
      }
      }


      Update: It draws one line, and when I try to do another one, on the third click, it closes and I need to draw multiple lines in my canvas.










      share|improve this question
















      I'm trying to draw a line, I managed to do it before but the first point somehow was always 0, 0. Now my logic is, there are two Vectors, one to store each point that is clicked, and another to store the Line, which is made by two points clicked by the user. I do the verifications to see if the Vector is not empty and only then I draw the line. I don't really know what's going on, I've tried everything, hope some of you can help, I'm in real need. Thank you.
      Here it goes the code from MyView.java:



      import android.content.Context;
      import android.graphics.Canvas;
      import android.graphics.Color;
      import android.graphics.Paint;
      import android.util.AttributeSet;
      import android.view.MotionEvent;
      import android.view.View;
      import java.util.Vector;

      public class MyView extends View {
      Paint paint = null;
      int figure;
      int lados_poly;
      int cor;
      int deletar;
      int CursorX, CursorY;
      int nrCliques;

      Vector<Ponto2D> ptsReta = new Vector<Ponto2D>();
      Vector<Reta> guardaRetas = new Vector<Reta>();

      public MyView(Context context) {
      super(context);
      paint = new Paint();
      figure = 0;
      cor = 0;
      }

      public MyView(Context context, AttributeSet attrs) {
      super(context, attrs);
      paint = new Paint();
      paint.setStrokeWidth(10);
      figure = 0;
      cor = 0;
      }

      public MyView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      paint = new Paint();
      figure = 0;
      cor = 0;
      }


      public void clickEcra() {
      setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

      switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:

      case MotionEvent.ACTION_UP:

      CursorX = (int)event.getX();
      CursorY = (int)event.getY();

      if (figure == 2){
      Ponto2D ptReta = new Ponto2D();
      ptReta.x = CursorX;
      ptReta.y = CursorY;
      ptsReta.add(ptReta);
      if (ptsReta.size()> 0) {
      for (int c = 0; c < ptsReta.size(); c++)
      System.out.println("ptRetaX: " + ptsReta.get(c).x + " ptRetaY: " + ptsReta.get(c).y + " size " + ptsReta.size());
      }
      invalidate();
      }

      default:
      return false;
      }
      }
      });
      }


      @Override
      protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      clickEcra();

      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.parseColor("#F5F1E0"));
      canvas.drawPaint(paint);
      paint.setColor(Color.parseColor("#3F5866"));

      //cores
      if (cor == 1) {
      paint.setColor(Color.parseColor("#393E46"));
      } else if (cor == 2){
      paint.setColor(Color.parseColor("#00ADB5"));
      } else if (cor == 3) {
      paint.setColor(Color.parseColor("#F8B500"));
      } else if (cor == 4) {
      paint.setColor(Color.parseColor("#FC3C3C"));
      }


      if (figure == 2) {
      if (ptsReta.size() >= 2) {
      for (int b = 0; b < ptsReta.size(); b = b + 2) {
      Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
      guardaRetas.add(retinha);
      System.out.println("pts: " + ptsReta.get(b) + ptsReta.get(b + 1));
      }
      }

      if (guardaRetas.size() > 0) {
      for (int r = 0; r < guardaRetas.size(); r++) {
      canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
      }
      }
      }

      //clear canvas
      if (deletar == 2){
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.parseColor("#F5F1E0"));
      canvas.drawPaint(paint);
      nrCliques = 0;
      ptsCirc.removeAllElements();
      ptsReta.removeAllElements();
      }
      }


      public void setfigure(int a) {
      this.figure = a;
      }

      public void Cor1_mudar(int text_cor) {
      this.cor = text_cor;
      }

      public void Resetar(int delete){
      this.deletar = delete;
      }
      }


      And now the code from Line.java:



      public class Reta {
      Ponto2D pinicial;
      Ponto2D pfinal;

      public Reta(){
      pinicial = new Ponto2D();
      pfinal = new Ponto2D();
      }

      public Reta(Ponto2D a, Ponto2D b) {
      pinicial = a;
      pfinal = b;
      }
      }


      Update: It draws one line, and when I try to do another one, on the third click, it closes and I need to draw multiple lines in my canvas.







      java android android-studio vector






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 22:01







      soalribeiro

















      asked Nov 20 '18 at 19:19









      soalribeirosoalribeiro

      19312




      19312
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This were my changes, the following inside onDraw() method:



              if (figure == 2) {
          if (ptsReta.size() %2 == 0) {
          for (int b = 0; b < ptsReta.size(); b = b + 2) {
          Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
          guardaRetas.add(retinha);
          }

          if (guardaRetas.size() > 0) {
          for (int r = 0; r < guardaRetas.size(); r++) {
          canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
          }
          }
          }
          }


          And this ones inside the click method:



          public void clickEcra() {
          setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
          switch (event.getActionMasked()) {
          case MotionEvent.ACTION_DOWN:

          case MotionEvent.ACTION_UP:

          CursorX = (int)event.getX();
          CursorY = (int)event.getY();

          if (figure == 2){
          Ponto2D ptReta = new Ponto2D();
          ptReta.x = CursorX;
          ptReta.y = CursorY;
          ptsReta.add(ptReta);
          invalidate();
          }

          default:
          return false;
          }
          }
          });
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400064%2fdraw-line-in-view-using-vector-in-android-studio%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This were my changes, the following inside onDraw() method:



                if (figure == 2) {
            if (ptsReta.size() %2 == 0) {
            for (int b = 0; b < ptsReta.size(); b = b + 2) {
            Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
            guardaRetas.add(retinha);
            }

            if (guardaRetas.size() > 0) {
            for (int r = 0; r < guardaRetas.size(); r++) {
            canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
            }
            }
            }
            }


            And this ones inside the click method:



            public void clickEcra() {
            setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:

            case MotionEvent.ACTION_UP:

            CursorX = (int)event.getX();
            CursorY = (int)event.getY();

            if (figure == 2){
            Ponto2D ptReta = new Ponto2D();
            ptReta.x = CursorX;
            ptReta.y = CursorY;
            ptsReta.add(ptReta);
            invalidate();
            }

            default:
            return false;
            }
            }
            });
            }





            share|improve this answer




























              0














              This were my changes, the following inside onDraw() method:



                  if (figure == 2) {
              if (ptsReta.size() %2 == 0) {
              for (int b = 0; b < ptsReta.size(); b = b + 2) {
              Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
              guardaRetas.add(retinha);
              }

              if (guardaRetas.size() > 0) {
              for (int r = 0; r < guardaRetas.size(); r++) {
              canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
              }
              }
              }
              }


              And this ones inside the click method:



              public void clickEcra() {
              setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
              switch (event.getActionMasked()) {
              case MotionEvent.ACTION_DOWN:

              case MotionEvent.ACTION_UP:

              CursorX = (int)event.getX();
              CursorY = (int)event.getY();

              if (figure == 2){
              Ponto2D ptReta = new Ponto2D();
              ptReta.x = CursorX;
              ptReta.y = CursorY;
              ptsReta.add(ptReta);
              invalidate();
              }

              default:
              return false;
              }
              }
              });
              }





              share|improve this answer


























                0












                0








                0







                This were my changes, the following inside onDraw() method:



                    if (figure == 2) {
                if (ptsReta.size() %2 == 0) {
                for (int b = 0; b < ptsReta.size(); b = b + 2) {
                Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
                guardaRetas.add(retinha);
                }

                if (guardaRetas.size() > 0) {
                for (int r = 0; r < guardaRetas.size(); r++) {
                canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
                }
                }
                }
                }


                And this ones inside the click method:



                public void clickEcra() {
                setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:

                case MotionEvent.ACTION_UP:

                CursorX = (int)event.getX();
                CursorY = (int)event.getY();

                if (figure == 2){
                Ponto2D ptReta = new Ponto2D();
                ptReta.x = CursorX;
                ptReta.y = CursorY;
                ptsReta.add(ptReta);
                invalidate();
                }

                default:
                return false;
                }
                }
                });
                }





                share|improve this answer













                This were my changes, the following inside onDraw() method:



                    if (figure == 2) {
                if (ptsReta.size() %2 == 0) {
                for (int b = 0; b < ptsReta.size(); b = b + 2) {
                Reta retinha = new Reta(ptsReta.get(b), ptsReta.get(b + 1));
                guardaRetas.add(retinha);
                }

                if (guardaRetas.size() > 0) {
                for (int r = 0; r < guardaRetas.size(); r++) {
                canvas.drawLine(guardaRetas.get(r).pinicial.x, guardaRetas.get(r).pinicial.y, guardaRetas.get(r).pfinal.x, guardaRetas.get(r).pfinal.y, paint);
                }
                }
                }
                }


                And this ones inside the click method:



                public void clickEcra() {
                setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:

                case MotionEvent.ACTION_UP:

                CursorX = (int)event.getX();
                CursorY = (int)event.getY();

                if (figure == 2){
                Ponto2D ptReta = new Ponto2D();
                ptReta.x = CursorX;
                ptReta.y = CursorY;
                ptsReta.add(ptReta);
                invalidate();
                }

                default:
                return false;
                }
                }
                });
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 0:18









                soalribeirosoalribeiro

                19312




                19312
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400064%2fdraw-line-in-view-using-vector-in-android-studio%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini