I really need help with this one. I am working with a game and have encountered a problem where it lags everytime an element is getting removed from my lists with objects. I have searched for solutions and found out about using iterators to reduce lag, but still, it lags. I have used both ArrayList and LinkedList, but they both end up with the same result.
This is an example of the code I am using for my lists:
Initialize:
private LinkedList<Background> bg = new LinkedList<Background>();
Render:
for (Iterator<Background> iterator = bg.iterator(); iterator.hasNext();) {
Background back = iterator.next();
if (back.getX()-(-width-1) > 0) {
if ((back.getX() < width) || (back.getX()+width > 0)) {
back.draw(canvas);
} else {
iterator.remove();
}
}
}
This is the draw function inside the object class in which back.draw(canvas); is calling from:
public void draw(Canvas canvas) {
// where to draw the sprite
Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
//canvas.drawBitmap(bitmap, 20, 150, null);
//Paint paint = new Paint();
//paint.setARGB(50, 0, 255, 0);
//canvas.drawRect(20 + (currentFrame * destRect.width()), 150, 20 + (currentFrame * destRect.width()) + destRect.width(), 150 + destRect.height(), paint);
}
Maybe there is something that has to be changed in the MainThread that handles the FPS? Or is there any other solution? I have heard about sets, but can’t seem to find any good example try it out.
Something to point out is that I am using Canvas, not OpenGL.
Something else to point out is that each element in the above array has an image size of 800×480. I have tried to reduce the quality of the image, but it doesn’t make much of a difference.
Thank you.
Its probably with the amount of computation you are doing in draw. I am sure its nothing to do with the collection you use.