Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid LinkedList lag when removing element
    text
    copied!<p>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.</p> <p>This is an example of the code I am using for my lists:</p> <p>Initialize:</p> <pre><code>private LinkedList&lt;Background&gt; bg = new LinkedList&lt;Background&gt;(); </code></pre> <p>Render:</p> <pre><code>for (Iterator&lt;Background&gt; iterator = bg.iterator(); iterator.hasNext();) { Background back = iterator.next(); if (back.getX()-(-width-1) &gt; 0) { if ((back.getX() &lt; width) || (back.getX()+width &gt; 0)) { back.draw(canvas); } else { iterator.remove(); } } } </code></pre> <p>This is the draw function inside the object class in which back.draw(canvas); is calling from:</p> <pre><code> 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); } </code></pre> <p>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.</p> <p>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 800x480. I have tried to reduce the quality of the image, but it doesn't make much of a difference.</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload