Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes the "decodeResource" method internally maintain bitmaps references?
    primarykey
    data
    text
    <p>I'm new to android so maybe this question is obvious but I'll ask it as I didn't find it.</p> <p>I'm working with a lot of objects generated randomly (enemies on a game), and I'm concerned whether I should keep relying on decodeResource:</p> <pre><code>public class Bird extends Actor { int x=0, y=0; public Bird (Context context) { setFace(BitmapFactory.decodeResource(context.getResources(),R.drawable.bird1)); ... </code></pre> <p>or create a <strong>'bitmap manager'</strong> of my own to allocate bitmaps as needed and reuse them if already decoded. </p> <p>i.e:</p> <pre><code>public class Bird extends Actor { int x=0, y=0; public Bird (Context context) { setFace(Director.getFace(Director.BIRD)); ... </code></pre> <p>which would need a static class and a lot of other stuff, but maybe decodeResource already does this for me internally? </p> <p>In other words, is it really necessary to implement this bitmap tracker <strong>'manually'</strong>?</p> <p>For the time being, I'm using this:</p> <pre><code>public class Artist { private static Context mContext; private static Hashtable&lt;Integer, Bitmap&gt; assets = new Hashtable&lt;Integer, Bitmap&gt;(); public static void setContext(Context c) { mContext = c; } public static Bitmap getFace(int id){ if (assets.containsKey(id)) return assets.get(id); Bitmap d = BitmapFactory.decodeResource(mContext.getResources(), id); assets.put(id, d); return d; } } </code></pre> <p>Though, I'm worried of this static class having references to unused bitmaps (as the game moves on some enemies don't come anymore), then this unused bitmaps wouldn't be garbage collected.</p> <p>I guess I'll have to address this problem by limiting the size of the assets hashtable removing the least accessed bitmap from it.</p> <p>Any thoughts with regarding my former question are welcome, I'll leave two more questions:</p> <p><strong>-Would the bitmaps be successfully garbage collected after their 'Actor's reference is gone and the reference in the hashtable is also gone?</strong> </p> <p><strong>-Do you spot some obvious flaw on my approach?</strong> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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