Android setBackgroundResource releases memory?

I have 2 quiet large animations every 50pics 20kb Both are defined as Animations.xml

One I start from the beginning, and the second after pressing the button.

//Start immediatly imgView.setBackgroundResource(R.layout.anim1); rocketAnimation = (AnimationDrawable) imgView.getBackground(); //Start after button click imgView.setBackgroundResource(R.layout.anim2); rocketAnimation = (AnimationDrawable) imgView.getBackground(); 

It works fine until I press a button and assign a second animator to my view

 08-22 14:56:03.886: DEBUG/AndroidRuntime(1541): Shutting down VM 08-22 14:56:03.886: WARN/dalvikvm(1541): threadid=3: thread exiting with uncaught exception (group=0x4001da28) 08-22 14:56:03.886: ERROR/AndroidRuntime(1541): Uncaught handler: thread main exiting due to uncaught exception 08-22 14:56:04.096: ERROR/AndroidRuntime(1541): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 08-22 14:56:04.096: ERROR/AndroidRuntime(1541): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 

It is clear that each animation is beautiful for itself (I could even set anim2 to autorun). but BOTH will exceed memory.

Btw. On my Nexus One, it works fine. Where it fails, on G1 1.6 (even in the simulator).

So HOW should I let go of the animation first before assigning anim2 ??

THX Chris

+4
source share
2 answers

Try to run

 rocketAnimation.setCallback(null); 

before displaying the animation.

btw: N1 has more heaps per application (24 MB) than G1 (16 MB). Samsung Galaxy S has even more (48 MB). But through the parameter, you can also set a higher heap limit for the emulator; (unfortunately not for an uncorrected phone, though).

0
source

Try adding imgView.setBackgroundResource (0); before the line imgView.setBackgroundResource (R.layout.anim2);

0
source

All Articles