Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:29:40+00:00 2026-05-29T06:29:40+00:00

I am currently developing a game and I’m trying to use one AnimationDrawable to

  • 0

I am currently developing a game and I’m trying to use one AnimationDrawable to show one animation in my Main Menu. When I run the game once, it works perfectly. But if i tap the back button, when I open the game again it gives me OutOfMemorryError.
If I hit home and go back to the game, it loads but doesn’t show the animation, it’s empty where it should be.

I think when it opens the game again it tries to load the animation, but it’s already loaded from the previous run, can i free that memory somehow? How can I treat that exception?

Searching around I can find that its a common problem in Android, but I couldn’t find anything useful.

If it’s relevant, my images are 310×316 and I have 114 frames to load, the animation is loaded in a xml.

My MainMenu class:

public class MainMenuActivity extends Activity{

 private String TAG = MainMenuActivity.class.getSimpleName();
 ImageView rabbitMenu ;
 AnimationDrawable ad;   

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);               
        setContentView(R.layout.mainmenu);          
        rabbitMenu = (ImageView) findViewById(R.id.rabbitMenu);
        rabbitMenu.setImageResource(R.drawable.rabbit_menu_animation);
        ad = (AnimationDrawable) rabbitMenu.getDrawable();

    }

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);       
        ad.start();     

}
}

The error log:

01-31 16:13:11.320: E/AndroidRuntime(19550): FATAL EXCEPTION: main
01-31 16:13:11.320: E/AndroidRuntime(19550): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-31 16:13:11.320: E/AndroidRuntime(19550):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-31 16:13:11.320: E/AndroidRuntime(19550):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
01-31 16:13:11.320: E/AndroidRuntime(19550):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)

Edit:

After @OleGG suggestion and now the second run goes fine, but on the third run I’m apparently trying to use a recycled bitmap.

So I gave up of using AnimationDrawable, and I used AsncTask to solve my problem. Now i keep changing the background of the ImageView on my onProgressUpdate(). May not be the best approach, but now I’m not having any problems! Here is my AsyncTask code in case someone has the same issue!

private class DrawRabbit extends AsyncTask<Void, Integer, Void> {
         private boolean running = true;
         private int fps = 24;
         private int frame = 10014;
         private String drawableName; 
         @Override
         protected Void doInBackground(Void... params) {


             while (running){
                 try {  
                    if (frame == 10014)
                        Thread.sleep(1000);
                    else 
                        Thread.sleep(fps);
                    if (frame < 10160) { 
                        frame++;
                        publishProgress(frame);
                    }
                    else 
                        running = false;

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }               
             return null;
         }

         protected void onProgressUpdate(Integer... frame) {
             drawableName = "rm" + frame[0];
             int res_id = getResources().getIdentifier(drawableName, "drawable", getPackageName());          
             rabbitFrame.setBackgroundResource(res_id);
         }   
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:29:40+00:00Added an answer on May 29, 2026 at 6:29 am

    You need to free memory used by bitmaps, when you’re exiting this screen. Unfortunately, for earlier versions of Android (afaik this was “fixed” since 3.0, but I can be wrong) memory for bitmaps is allocated through native code and, what is sad, memory should be freed explicitly.

    So, I suppose you to try this approach:

    1. Move code for AnimationDrawable to onResume() method.
    2. Add following code for releasing memory to onPause().

      ad.stop();
      for (int i = 0; i < ad.getNumberOfFrames(); ++i){
          Drawable frame = ad.getFrame(i);
          if (frame instanceof BitmapDrawable) {
              ((BitmapDrawable)frame).getBitmap().recycle();
          }
          frame.setCallback(null);
      }
      ad.setCallback(null);
      

    I hope it’ll help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing a C# .net xna game engine. I have been trying
I'm currently developing a small OpenGL game for the Android platform and I wonder
I'm currently developing an OpenGL ES game for the iPhone and iPod touch. I
Hi i am developing game level editor.Currently I am using win32 with directX.But with
I'm currently developing a webtool for a game called 'Eve Online'. This game has
I'm currently developing a simple 2D game for Android. I have a stationary object
I am currently developing a simple Pong game for the iPhone. Currently using CGRectIntersectsRect
I'm currently developing my first android app, and my first game. I've been developing
I'm currently developing an iPhone App that uses Phonegap and HTML5. I use localstorage
I am currently interested in game developing. I know a little bit of C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.