Out of Memory error when using AnimationDrawable

2019-02-28 17:51发布

问题:

Hi I'm trying to create a logo animation from a few png images i have created an animation and it crashes with an out of memory error. Does anyone know a solution to this?

heres what i have so far

heres my animation list

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:drawable="@drawable/intro_animation_00" android:duration="50" /> 
  <item android:drawable="@drawable/intro_animation_01" android:duration="50" />  
  <item android:drawable="@drawable/intro_animation_02" android:duration="50" /> 
  <item android:drawable="@drawable/intro_animation_03" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_04" android:duration="50" />  
  <item android:drawable="@drawable/intro_animation_05" android:duration="50" />  
  <item android:drawable="@drawable/intro_animation_06" android:duration="50" /> 
  <item android:drawable="@drawable/intro_animation_07" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_08" android:duration="50" /> 
  <item android:drawable="@drawable/intro_animation_09" android:duration="50" />  
  <item android:drawable="@drawable/intro_animation_10" android:duration="50" /> 
  <item android:drawable="@drawable/intro_animation_11" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_12" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_13" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_14" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_15" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_16" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_17" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_18" android:duration="50" />  
  <item android:drawable="@drawable/intro_animation_19" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_20" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_21" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_22" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_23" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_24" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_25" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_26" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_27" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_28" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_29" android:duration="50" />
  <item android:drawable="@drawable/intro_animation_30" android:duration="50" />
</animation-list>

heres my Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/bg"
    tools:context=".MenuActivity" >

        <ImageView
            android:id="@+id/myanimation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@anim/intro_animation"
        />

</RelativeLayout>

Heres my Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_menu);
        ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
        final AnimationDrawable myAnimationDrawable
        = (AnimationDrawable)myAnimation.getDrawable();

        myAnimation.post(
        new Runnable(){

          @Override
          public void run() {
           myAnimationDrawable.start();
          }
        });

    }

回答1:

try to add largeHeap=true in your application tag of your
manifest.

I hope this help.

or try using small size image.