{"id":2246,"date":"2022-08-30T15:23:16","date_gmt":"2022-08-30T15:23:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/setbackgroundresource-from-xml-crashes-vm-out-of-memory-how-can-i-optimise-this-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:16","modified_gmt":"2022-08-30T15:23:16","slug":"setbackgroundresource-from-xml-crashes-vm-out-of-memory-how-can-i-optimise-this-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/setbackgroundresource-from-xml-crashes-vm-out-of-memory-how-can-i-optimise-this-collection-of-common-programming-errors\/","title":{"rendered":"SetbackgroundResource from xml crashes VM, Out of memory. How can i optimise this?-Collection of common programming errors"},"content":{"rendered":"<p>I am currently making a small application in Android. My wish is to have two ImageViews that change their animation when i click different buttons on the screen.<\/p>\n<p>First a sample xml: (the drawables are 250x350y .png files)<\/p>\n<pre><code>\n\n    \n    \n    \n    \n\n<\/code><\/pre>\n<p>Example of my code<\/p>\n<pre><code>ImageView _imgView1; \/\/declared in oncreate = ((ImageView) findViewById... etc)\nAnimationDrawable _aniDrawable1;\n<\/code><\/pre>\n<p>OnResume:<\/p>\n<pre><code>_imgView1.setBackgroundResource(R.drawable.anim_main_idle);\n_aniDrawable1 = (AnimationDrawable) _imgView1.getBackground();\n_aniDrawable1.start();\n<\/code><\/pre>\n<p>So far this works, and there are no issues. The little sprite does it&#8217;s jig. I am however doing this to two imageViews at a time, and I&#8217;ve been trying to add more\/different &#8220;frames&#8221; in the XML for each. However, i do not have to add many frames, before the VM crashes because it&#8217;s out of memory.<\/p>\n<p>It seems to me as if it&#8217;s making a bmp out of each drawable mentioned in the XML, and then binding this in the VM memmory when i call &#8220;setBackgroundResource&#8221;.<\/p>\n<p>My question: Is there any way to make this less memmory intensive? Can i change how .setBackgroundResource saves the data from the XML? 7-8 different pictures like the one in my xml is enough to break a 16MB VM. I dont want much, i just want a few frames of animation running side by side that change when i click buttons, but that seems prettymuch impossible if AnimationDrawable eats so much out of the VM-memmory.<\/p>\n<p>Hints? ideas? alternatives?<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:32:10. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am currently making a small application in Android. My wish is to have two ImageViews that change their animation when i click different buttons on the screen. First a sample xml: (the drawables are 250x350y .png files) Example of my code ImageView _imgView1; \/\/declared in oncreate = ((ImageView) findViewById&#8230; etc) AnimationDrawable _aniDrawable1; OnResume: _imgView1.setBackgroundResource(R.drawable.anim_main_idle); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2246","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2246"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2246\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}