Image trail when moving bitmap in Android
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
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.
精彩评论