Would 15 be the maximum limit of images per Drawable Resource File in Android Studio?

Asked

Viewed 255 times

0

I’d like to make a Animation Drawable with 18 images in Android Studio, but when trying to run it the application stopped out of nowhere and gave this in Android Monitor:

03-16 16:58:26.531 12973-12973/genesysgeneration.animation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.OutOfMemoryError
                                                                                 at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
                                                                                 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
                                                                                 at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
                                                                                 at android.content.res.Resources.loadDrawable(Resources.java:1940)
                                                                                 at android.content.res.Resources.getDrawable(Resources.java:664)
                                                                                 at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:280)
                                                                                 at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
                                                                                 at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
                                                                                 at android.content.res.Resources.loadDrawable(Resources.java:1925)
                                                                                 at android.content.res.Resources.getDrawable(Resources.java:664)
                                                                                 at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)
                                                                                 at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)
                                                                                 at android.view.View.setBackgroundResource(View.java:11695)
                                                                                 at android.support.v7.widget.AppCompatImageView.setBackgroundResource(AppCompatImageView.java:86)
                                                                                 at genesysgeneration.animation.MainActivity.onCreate(MainActivity.java:20)
                                                                                 at android.app.Activity.performCreate(Activity.java:4466)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                                                 at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                                 at dalvik.system.NativeStart.main(Native Method)

I was blinded by this, because I did not know where the error was and I was not accusing anything, so I decided to run only an image of my animation... RESULTED!!!

I tried 2, 3, 4... up to 15 it was all right:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/an01" android:duration="100"></item>
    <item android:drawable="@drawable/an02" android:duration="100"></item>
    <item android:drawable="@drawable/an03" android:duration="100"></item>
    <item android:drawable="@drawable/an04" android:duration="100"></item>
    <item android:drawable="@drawable/an05" android:duration="100"></item>
    <item android:drawable="@drawable/an06" android:duration="100"></item>
    <item android:drawable="@drawable/an07" android:duration="100"></item>
    <item android:drawable="@drawable/an08" android:duration="100"></item>
    <item android:drawable="@drawable/an09" android:duration="100"></item>
    <item android:drawable="@drawable/an10" android:duration="100"></item>
    <item android:drawable="@drawable/an11" android:duration="100"></item>
    <item android:drawable="@drawable/an12" android:duration="100"></item>
    <item android:drawable="@drawable/an13" android:duration="100"></item>
    <item android:drawable="@drawable/an14" android:duration="100"></item>
    <item android:drawable="@drawable/an15" android:duration="100"></item>


</animation-list>

When I put 16 images, gave error again...

I thought it could be error in the pictures properly speaking, so I jumped to 16 and went to 17... Mistake again!!! Same thing with 18.

Result, I suspected that it was not an error in the images, but that it was some kind of limit of the total number of frames. I modified the Animation-list to the following:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/an01" android:duration="100"></item>
    <item android:drawable="@drawable/an02" android:duration="100"></item>
    <item android:drawable="@drawable/an03" android:duration="100"></item>
    <item android:drawable="@drawable/an04" android:duration="100"></item>
    <item android:drawable="@drawable/an05" android:duration="100"></item>
    <item android:drawable="@drawable/an06" android:duration="100"></item>
    <item android:drawable="@drawable/an07" android:duration="100"></item>
    <item android:drawable="@drawable/an08" android:duration="100"></item>
    <item android:drawable="@drawable/an09" android:duration="100"></item>
    <item android:drawable="@drawable/an10" android:duration="100"></item>
    <item android:drawable="@drawable/an11" android:duration="100"></item>
    <item android:drawable="@drawable/an12" android:duration="100"></item>
    <item android:drawable="@drawable/an16" android:duration="100"></item>
    <item android:drawable="@drawable/an17" android:duration="100"></item>
    <item android:drawable="@drawable/an18" android:duration="100"></item>


</animation-list>

The 16, 17 and 18 images worked, so I suspected it was even a 15 frame limit.

I wonder if it is a limit even and if there is how to increase it or even make an armengue to display one animation soon after the other. Thanks in advance!!!

Code of the Activity:

package genesysgeneration.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animation;
    private ImageView ivInvisible;

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

        ivInvisible=(ImageView)findViewById(R.id.ivInvisible);
        ivInvisible.setBackgroundResource(R.drawable.an00);
        animation=(AnimationDrawable)ivInvisible.getBackground();

    }

    public boolean onTouchEvent(MotionEvent event){

        if (event.getAction()==MotionEvent.ACTION_DOWN){
            animation.start();
            return true;
        }

        return super.onTouchEvent(event);

    }

}
  • 1

    What is the resolution of these images? The most likely there is that are very large images and that is why is giving the Outofmemory, because you do not have enough memory to load them, remembering that Android limits the maximum that the application can use of RAM

  • the average image size is 350KB with dimensions 720x1280

1 answer

2


Your problem is certainly the use of memory of these images and not the amount of them in themselves, given that an Animationdrawable carries all the images in memory at once.

First of all it is important to understand that the file size has no relevance when using images, it is only the format of the encoded image, when loaded in memory the image to be used is decoded and, in general, you will have a use of 4 bytes (32 bits) for each pixel of the image, which is usually stored in memory as ARGB.

With this your image that has only 350kb and 720x1280 resolution, after decoding occupies about 3.5mb of RAM, with 18 of them you should need almost 64mb of RAM just for this animation, and I’m still not even counting on the case of using the image on a device with a higher pixel density, because the moment the image is loaded Android will resize it according to the screen, so imagine using it on a mobile with 4K screen, it would be easy for each image of yours to occupy more than 10mb of RAM.

And the maximum amount of RAM that one app can use varies a lot from one device to another, and there’s nothing to do about it, there’s no way to change that value.

You can test just to confirm this using images with a lower resolution and so you’ll probably be able to use more of them in your animation, but I would say that Animationdrawable is not designed to be used this way, with animations of screen size and you will probably have to consider some alternative, such as a smaller animation only with the part of the image that is actually animated if it has a large static part, or make yourself by code the animation by drawing it directly on the screen, only that preferably you should not use whole frames but rather make the animation itself by code.

  • Indeed. I halved both width and height and resulted. Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.