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

0

I'd like to make a Animation Drawable with 18 images in Android Studio, but when I tried to run it, the application stopped working and I gave it to Android Monitor : < p>

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 confused by this, because I did not know where the error was and was not accusing anything, so I decided to execute only one image of my animation ... RESULTS !!!

I tried 2, 3, 4 ... until 15 everything worked out:

<?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, it gave me an error again ...

I thought it could be a mistake in the images properly speaking, so I jumped to 16 and went to 17 ... Error 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 for 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 images 16, 17 and 18 worked, so I suspected that it was really a limit of 15 frames.

I would like to know if it is a limit and if there is a way to increase it or even to do an armengue to display one animation right after the other. Thank you!

Activity code:

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);

    }

}
    
asked by anonymous 16.03.2017 / 21:07

1 answer

2

Your problem is certainly the memory usage of these images and not the amount of them in themselves, since an AnimationDrawable loads all the images in memory at once.

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

With this your image that has only 350kb and resolution of 720x1280, after decoding takes up about 3.5mb of RAM, with 18 of them you should need almost 64mb of RAM just for this animation, and I'm not even relying 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 cell phone with 4K screen, it would be easy each image its occupying more than 10mb of RAM.

And the maximum amount of RAM an application can use varies greatly from device to device, and there is nothing to do about it, you can not change that value.

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

    
16.03.2017 / 21:54