So here’s the deal, I’ve searched every single question and link online but none are helpful.
I have 120 frames of an animation in .jpg format for my splash screen. I understand that jpegs are converted to bitmaps on memory so that’s why I get an OutOfMemoryError. The maximum frames I get to animate are 10. Is there any way to do this frame by frame, or should I try something else. Here’s my code:
final AnimationDrawable anim = new AnimationDrawable();
anim.setOneShot(true);
for (int i = 1; i <= 120; i++)
{
Drawable logo = getResources().getDrawable(getResources()
.getIdentifier("l"+i, "drawable", getPackageName()));
anim.addFrame(logo, 50);
if (i % 3 == 0)
{
System.gc();
}
}
ImageView myImageView = (ImageView) findViewById(R.id.SplashImageView);
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable()
{
public void run()
{
anim.start();
}
});
I’ve placed the 120 jpegs under the drawable folder with an “l” prefix (eg l1, l2 etc).
I do garbage collection every 3 jpegs but that won’t do a thing.
You can try to do it without
AnimationDrawableusingHandler.postDelayed. Something like this:This solution requires less memory because it keeps only one drawable at the time.
You can cancel the animation using
handler.removeCallbacks(animation);.If you want make a one-shot animation you can call
handler.postDelayedconditionally: