i need some help on my android project.
i have a canvas and i fill it with some bitmap.
and i have a pointer that drawn a line on the canvas.
my problem is how to clean the line that i’ve drawn before?
what method should i call on canvas?
i’ve tried Canvas.drawColor(), invalidate() and that’s not working.
and what is the function of Canvas.drawColor() and
Please help me solve my problem.
thanks in advance
UPDATE!
if i made the code like this:
@Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
where i place invalidate() in my code?
and what code should i use if i want to clear the canvas using a button?
UPDATE!
i wrote my onDraw like this:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(letsdraw){
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
and the method in reset button is like this:
public void rst(){
letsdraw = false;
invalidate();
Log.v("tag", "this method called");
}
but no change in the canvas when i called the method.
did i wrote something wrong on the code above?
Make your logic like this. Draw the line on the
Canvaswith some condition. Check if you want to draw the line, then draw the line.Skeleton code –
Now just update your
needToDrawLinevariable and callinvalidate(). You’ll get your result. Let me know if it works.Update:
onDraw()method will call everytime you call theinvalidate(). So everyting inside theonDraw()will execute. The way is, you have to prevent it from drawing some of the part. You’ll callinvalidate()when you want to redraw the whole view, for example – button for clear the canvas.canvas.drawColor(Color.BLACK);this line clear your whole view to BLACK color.canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);this line draw the bitmap at (0,0).canvas.drawPath(mPath, p);this line draw the pathmPath.obaby.draw(canvas);some other object draw itself.Now – for example you want to clear the screen, when button pressed. Just initialize a variable if it draw everything. And update the variable in button click.