How to erase previous drawing on Canvas?

2019-02-03 00:55发布

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);
}

7条回答
Rolldiameter
2楼-- · 2019-02-03 01:36

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.

查看更多
登录 后发表回答