Clearing canvas with Canvas.drawColor()

Posted by strangeInAStrangerLand on Stack Overflow See other posts from Stack Overflow or by strangeInAStrangerLand
Published on 2011-01-10T19:40:02Z Indexed on 2011/01/10 19:53 UTC
Read the original article Hit count: 432

Filed under:
|
|

I'm attempting to change the background image of a custom View with some success. The image will change but the problem is that I still see traces of the old image. When I attempt to clear the canvas before drawing the new image, it doesn't appear to work. I create a bitmap to store the image. When changing the image, I call Canvas.drawColor() before drawing the new image but the old image persists. I've tried drawColor(0), drawColor(Color.BLACK), c.drawColor(0, PorterDuff.Mode.CLEAR), and none of the above works. As such, I had to post this for review from more experienced minds than mine.

The actual code is as follows:

private int bgnd;
private boolean switching;

public void setBgnd(int incoming){
    switching = true;
    switch (incoming){

    case R.drawable.image1:
        bgnd = incoming;
        this.invalidate();
        break;

    case R.drawable.image2:
        bgnd = incoming;
        this.invalidate();
        break;

    }
}

protected void onDraw(Canvas c){
    if(switching == true){
        Bitmap b = BitmapFactory.decodeResource(getResources(), bgnd);
        c.drawColor(0, PorterDuff.Mode.CLEAR);
        c.drawBitmap(b, 0, 0, null);
        switching = false;

    }else{
        Bitmap b = BitmapFactory.decodeResource(getResources(), bgnd);
        c.drawBitmap(b, 0, 0, null);
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about canvas