1
votes

I draw line with canvas and touch But my problem is this that when we toch again the screen , the perivious line deleted I want with touch again draw new line but the last line dont deleted Tnx protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw the mPath with the mPaint on the canvas when onDraw canvas.drawLine(sX, sY, eX, eY, mPaint); }

// when ACTION_DOWN start touch according to the x,y values
private void startTouch(float x, float y) {
    sX = x;
    sY = y;
}

// when ACTION_MOVE move touch according to the x,y values
private void moveTouch(float x, float y) {
        eX = x;
        eY = y;
}
private void upTouch(float x, float y) {
    eX = x;
    eY = y;
}

public void clearCanvas() {
    invalidate();
}



@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        startTouch(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        moveTouch(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        upTouch(x,y);
        invalidate();
        break;
    }
    return true;
}
2
please paste some code.Sascha Kolberg

2 Answers

1
votes

Here is a pretty good example for canvas and drawing

1
votes

Here's some example code to get you started. Each time the user touches, a new Line is created and added to a list of Lines.

MainActivity

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DrawLineView drawLineView = new DrawLineView(this);
    setContentView(drawLineView);
  }
}

DrawLineView

public class DrawLineView extends View {

  List<Line> lines;
  Paint black;

  public DrawLineView(Context context) {
    super(context);

    // sets up a new list of Lines
    lines = new ArrayList<>(); 

    black = new Paint();
    black.setColor(Color.BLACK);
    black.setStrokeWidth(5);
  }

  @Override
  public void onDraw(Canvas canvas) {
    // for each of your Lines, draw them on the canvas
    for (Line line : lines) { 
      canvas.drawLine(line.xStart, line.yStart, line.xEnd, line.yEnd, black);
    }

    invalidate();
  }


  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {

      case MotionEvent.ACTION_DOWN:
        Line line = new Line(event.getX(), event.getY(), event.getX(), event.getY());
        lines.add(line);
        break;

      case MotionEvent.ACTION_MOVE:
        lines.get(lines.size() - 1).xEnd = event.getX();
        lines.get(lines.size() - 1).yEnd = event.getY();
        break;
    }
    return true;
  }

  class Line {
    public float xStart;
    public float yStart;
    public float xEnd;
    public float yEnd;

    public Line(float xStart, float yStart, float xEnd, float yEnd) {
      this.xStart = xStart;
      this.yStart = yStart;
      this.xEnd = xEnd;
      this.yEnd = yEnd;
    }
  }
}