开发者

Image trail when moving bitmap in Android

开发者 https://www.devze.com 2023-02-21 13:36 出处:网络
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'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

Image trail when moving bitmap in Android

This is the code I used to load the image:

bmp = Bi开发者_如何学CtmapFactory.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!


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


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消