I have a couple of frame-by-frame animations (animation-list
). Loading any one of them to an ImageView is not a problem. The problem occurs when I try to load a different animation in the same ImageView.
private void startAnimation(int anim) {
mImageView.setImageResource(anim);
((AnimationDrawable) mImageView.getDrawable()).start();
}
This was the code that I was using. After getting java.lang.OutOfMemoryError
after calling the function more than one time, I added the following code to try and clear the AnimationDrawable
and the ImageView
.
private void startAnimation(int anim) {
if (mImageView.getDrawable() != null) { //trying to clear if it's not empty
if (((AnimationDrawable) mImageView.getDrawable()).isRunning()) {
((AnimationDrawable) mImageView.getDrawable()).stop();
((AnimationDrawable) mImageView.getDrawable()).selectDrawable(0);
}
mImageView.setImageResource(null);
}
//starting animation here
mImageView.setImageResource(anim);
((AnimationDrawable) mImageView.getDrawable()).start();
}
Well, this didn't work. I've tried the solutions posted here and here and they also didn't work. I still keep getting OutOfMemoryError
.
This is how I call the startAnimation
function.
startAnimation(R.drawable.anim1);//works fine
startAnimation(R.drawable.anim2);//OutOfMemoryError
...
So, how do I free the memory, and load the animation? Note that I want to do this over and over again.
I finally got it to work by adding mImageView.getDrawable().setVisible(false, true)
before starting a new animation.