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);
}
}
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
– Leandro Godoy Rosa
the average image size is 350KB with dimensions 720x1280
– Boneco Sinforoso