AnimationDrawable OutOfMemoryError

643 views Asked by At

Current Version: Android Studio 2.2.3

I will start by asking, how do you create an animation-list? When you click on the res/drawable folder new -> Drawable resource file you don't have the option to change the Root Element from selector to animation-list. So I change it manually.

Nevermind, I followed the instructions here from developers.android.

Here is the code:

res/drawable/anim.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/frame13" android:duration="200" />
<item android:drawable="@drawable/frame14" android:duration="200" />
<item android:drawable="@drawable/frame15" android:duration="200" />
<item android:drawable="@drawable/frame16" android:duration="200" />
<item android:drawable="@drawable/frame17" android:duration="200" />
<item android:drawable="@drawable/frame18" android:duration="200" />
<item android:drawable="@drawable/frame19" android:duration="200" />
<item android:drawable="@drawable/frame20" android:duration="200" />
<item android:drawable="@drawable/frame21" android:duration="200" />
<item android:drawable="@drawable/frame22" android:duration="200" />
<item android:drawable="@drawable/frame23" android:duration="200" />
</animation-list>

activity_main.xml

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/imageView"
    android:src="@drawable/anim"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {

ImageView imageView;
AnimationDrawable a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setBackgroundResource(R.drawable.anim);
    a = (AnimationDrawable) imageView.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        a.start();
        return true;
    }
    return super.onTouchEvent(event);
}
}

You see anything wrong?

I don't at least and when I run it, this error pops up:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: animation.aggelonias.gr.animation, PID: 1146
              java.lang.OutOfMemoryError
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
                  at android.content.res.Resources.loadDrawable(Resources.java:3077)
                  at android.content.res.Resources.getDrawable(Resources.java:1624)
                  at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
                  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
                  at android.content.res.Resources.loadDrawable(Resources.java:3059)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                  at android.widget.ImageView.<init>(ImageView.java:133)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1017)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1076)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                  at animation.aggelonias.gr.animation.MainActivity.onCreate(MainActivity.java:17)
                  at android.app.Activity.performCreate(Activity.java:5451)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
                  at android.app.ActivityThread.access$900(ActivityThread.java:169)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5476)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)

Somewhere in that mess says, MainActivity.java:17: which is setContentView(R.layout.activity_main);

Thanks!

2

There are 2 answers

0
Yua On

AnimationDrawable will load all pictures into memory,if your Animation contains many frames, it may cause Out Of Memory. so you should try to compress your bitmap. Another method, you can use SurfaceView instead of AnimationDrawable.

View here

4
Star_Man On

Perhaps your image files frame13, 14 ,... would be very large or your image type would not be a standard image type such as png, jpg. Check your image files.

Try adding android:largeHeap="true", in the manifest to see if this solves it.