Android Animation (XML) Giving OutOfMemoryError: b

2019-08-04 08:38发布

I'm getting a [java.lang.OutOfMemoryError: bitmap size exceeds VM budget] when running an animation sequence on a very few Android devices. The app has been tested on 40 other devices without ever faulting (including the full AT&T android lineup), but gets this error every time on the Motorola Droids (X, X2, 3), Motorola Xoom tablet, and sporadically on the HTC Thunderbolt, Evo 4G, HTC Desire, and Xperia.

Offending code:

ImageView img = (ImageView) findViewById(R.id.simple_anim);
img.setBackgroundResource(R.anim.my_anim);
startAnimation = (AnimationDrawable) img.getBackground();
startAnimation.start();

I'm creating the frame animation sequence through XML and here is the sample anim/my_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="true">
<item android:drawable="@drawable/frame00" android:duration="20" />
<item android:drawable="@drawable/frame01" android:duration="20" />
<item android:drawable="@drawable/frame02" android:duration="20" />
<item android:drawable="@drawable/frame03" android:duration="20" />
<item android:drawable="@drawable/frame04" android:duration="20" />
</animation-list>

Each frame is a 240x360 .PNG (8 bit) approx. 20kb in size. I can get this error if the sequence runs at the start of the activity, on a touch event, etc.

The animation is stuck inside a RelativeLayout (snippet):

<ImageView android:id="@+id/simple_anim"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="bottom|left"
android:scaleType="centerInside" /> 

At this point, I'm open to another method to frame animate entirely as no matter what I've tried I can't get this to work on a select number of devices. On all the 40+ other tested devices, I can animate hundreds of frames this way, but on these few devices, anything over 10 frames or so seems to fail.

Any ideas?

0条回答
登录 后发表回答