23

I have a background image (a map) on which I need to regularly draw the you-are-here icon. I use Canvas to draw the icon on top of the map. Assuming that the drawing process is triggered on button click (see code below), how can I erase the previous drawing?

private void displayUserPos(Point userPos) {
    Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.ic_yah);
    canvas.drawBitmap(marker, (float)userPos.getX(), (float)userPos.getY(), null);
    imgView.setImageBitmap(fmOverlay);
}
springrolls
  • 1,331
  • 1
  • 14
  • 40

7 Answers7

49
canvas.drawColor(0, Mode.CLEAR);

More info http://developer.android.com/guide/topics/graphics/index.html

Caner
  • 57,267
  • 35
  • 174
  • 180
  • 1
    What if there are other drawings from another button click which I want to keep? Is there a way to somehow get hold of the id? – springrolls Aug 05 '11 at 13:29
  • That is discueessed here: http://stackoverflow.com/questions/5729377/android-canvas-how-do-i-clear-delete-contents-of-a-canvas-bitmaps-living – Caner Aug 05 '11 at 13:44
19
canvas.drawColor(0, Mode.CLEAR);
Maxim
  • 2,996
  • 17
  • 19
  • 1
    This one is very helpful when your canvas needs to be transparent and you can not fill it with white or black or whatever bg color you have. – Dhruv Oct 17 '11 at 10:01
7
overlayBitmap.eraseColor(Color.TRANSPARENT);

This simply sets an existing Bitmap to all transparent.

I use this to "clear" a Bitmap object that I use to overlay on top of another to show a cropping window.
renrip
  • 71
  • 1
  • 1
2

Just fill in the canvas with a color or image:

canvas.drawColor(Color.BLACK);

If you want to keep certain elements and take certain elements away you can store these in an ArrayList. Then you can add and remove elements from this ArrayList when you want, and then iterate through them in onDraw().

for (Iterator<GraphicObject> it = _graphics.iterator(); it.hasNext();) {
    GraphicObject graphic = (GraphicObject)it.next();
    coords = graphic.getCoordinates();
    canvas.drawCircle(coords.getX(), coords.getY(), (float)coords.getRadius(), paint);
}
Matt K
  • 6,620
  • 3
  • 38
  • 60
1

Try as below,it can be used to clear the canvas totally.

Declaration should be like this,

ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();

and while clearing use

    undonePaths.clear();
    paths.clear();
    invalidate();
user3009917
  • 113
  • 9
0
canvas.drawColor(0, PorterDuff.Mode.CLEAR);

For more info Click here

Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
0

I wrote a function to help with this. Usually I set up a boolean to check if the page has changed, if it has, I call this function, using the return boolean to set my newpage boolean.

    private boolean clearCanvas(Canvas canvas){
      canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
      return false;
    }

Color.TRANSPARENT isn't really needed in this, you could do with out.

jrende
  • 51
  • 1
  • 4