Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need to do is store the lines inside an arraylist and then read the arraylist onDraw(). try this code for your View Class:</p> <pre><code>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.ArrayList; class Line { float startX, startY, stopX, stopY, float joinX, joinY = 0; public Line(float startX, float startY, float stopX, float stopY) { this.startX = startX; this.startY = startY; this.stopX = stopX; this.stopY = stopY; } public Line(float startX, float startY) { // for convenience this(startX, startY, startX, startY); } } public class DrawView extends View { Paint paint = new Paint(); ArrayList&lt;Line&gt; lines = new ArrayList&lt;Line&gt;(); public DrawView(Context context, AttributeSet attrs) { super(context, attrs); paint.setAntiAlias(true); paint.setStrokeWidth(6f); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); } @Override protected void onDraw(Canvas canvas) { for (Line l : lines) { canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint); } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (joinX &lt;= 0 || joinY &lt;= 0) lines.add(new Line(event.getX(), event.getY())); else lines.add(new Line(joinX, joinY); return true; } else if ((event.getAction() == MotionEvent.ACTION_MOVE) &amp;&amp; lines.size() &gt; 0) { Line current = lines.get(lines.size() - 1); current.stopX = event.getX(); current.stopY = event.getY(); Invalidate(); return true; } else if ((event.getAction() == MotionEvent.ACTION_UP) &amp;&amp; lines.size() &gt; 0 { Line current = lines.get(lines.size() - 1); current.stopX = event.getX(); current.stopY = event.getY(); joinX = event.getX(); joinY = event.getY(); Invalidate(); return true; } else { return false; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload