I used following code to draw. I want to clear the previously drawn lines if the clear button is clicked.
public class MainActivity extends Activity {
private ArrayList<Path> _graphics = new ArrayList<Path>();
private Paint mPaint;
Activity activity;
View mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
mView = new DrawingView(this);
activity.addContentView(mView, new LayoutParams(500,
LinearLayout.LayoutParams.WRAP_CONTENT));
init();
}
public void clear(View v) {
new DrawingView(activity).clearView();
}
private void init() {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
class DrawingView extends View {
private Path path;
public DrawingView(Context context) {
super(context);
path = new Path();
this.setBackgroundColor(Color.BLACK);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}
invalidate();
return true;
}
@Override
public void onDraw(Canvas canvas) {
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}
public void clearView() {
path = new Path();
invalidate();
}
}
}
Thank you.
or
See this Doc for more info.
Your
clear
method is wrong.This code works for me
this method work for me,
Add the below in your Drawing view
Also add the below
In your onDraw add the below
To clear on button click