1

I'm trying to program a little game for Android, and when I draw the bitmap and animate it using Sprites, I can see a trail in the image! (it's a PNG image). I know it is not a problem of the sprites, because I used a static image moving along the X axis and I could still see the trail

trail of the animation

This is the code I used to load the image:

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);

How can I eliminate it so I can only see one of the sprites every time?

Thank you very much in advance!

noloman
  • 11,411
  • 20
  • 82
  • 129

3 Answers3

3

It looks like you need to clear the background before each time you draw your sprite. Calling drawColor on your Canvas should do it, something like

    Canvas c = null;
    try {
        c = surfaceHolder.lockCanvas(null);
        synchronized(surfaceHolder) {
            c.drawColor(Color.BLACK);
            c.draw(bmp, posX, posY, null);
        }
    } finally {
        if(c != null)
            surfaceHolder.unlockCanvasAndPost(c);
    }
Steve Blackwell
  • 5,904
  • 32
  • 49
1

Well, it was a very stupid thing: instead of

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(***Color.BLACK***);
    sprite.onDraw(canvas);
}

I did a

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(***color.black***);
    sprite.onDraw(canvas);
}

Thanks a lot both for the answers, because I really was not sure why all that had to be done, so both of you actually helped a lot!

noloman
  • 11,411
  • 20
  • 82
  • 129
0

Are you doing this in a SurfaceView or a Canvas?

With SurfaceView I believe you have to manually clear parts of the drawing that you want to disappear.

Matthew
  • 44,826
  • 10
  • 98
  • 87