Android setBackgroundResource release memory?

2020-07-18 04:57发布

问题:

I have 2 quiet big Animations each 50pics a 20kb Both defined as Animations .xml

One I let start from the beginning and the second after a button click.

//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, till i hit the button and assign the second anim 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 looks definitly that each animation for itself is fine (i could even set anim2 at the autostart). but BOTH will exceed the memory.

Btw. on my Nexus One it works perfect. Where it fails is on G1 1.6 (even in Simulator).

So HOW would I release anim1 first before I assign anim2 ??

thx chris

回答1:

Try to run

rocketAnimation.setCallback(null);

before you show anim2.

btw: N1 has more heap per app allowed (24mb) than the G1 (16mb). Samsung Galaxy S has even more (48MB). But via parameter you can also set a higher heap limit for the emulator; (unfortunately not for an unrooted phone though).



回答2:

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