3
\$\begingroup\$

I am currently loading my game bitmaps when the user presses 'start' in my animated splash screen activity (the first / launch activity) and the app progresses from my this activity to the main game activity,

This is causing choppy animation in the splashscreen while it loads/creates the bitmaps for the new activity.

I've been told that I should load all my bitmaps in one go at the very beginning.

However, I can't work out how to do this - could anyone please point me in the right direction?

I have 2 activities, a splash screen and the main game. Each consist of a class that extends activity and a class that extends SurfaceView (with an inner class for the rendering / logic updating).

So, for example at the moment I am creating my bitmaps in the constructor of my SurfaceView class like so:

public class OptionsScreen extends SurfaceView implements
  SurfaceHolder.Callback {

      //Create variables here

 public OptionsScreen(Context context) {


Create bitmaps here
}

public void intialise(){                         //This method is called from onCreate() of corresponding application context

// Create scaled bitmaps here (from bitmaps previously created)

}
\$\endgroup\$
5
  • \$\begingroup\$ If there are alot of resources that you need to load for your app then you should probably only load the absolutely necessary ones right away and then load the rest in a background thread. If you absolutely require all of the assets then you should add an additional extended splash screen animation that runs while you load the resources on a background thread. Basically you dont want to tie up your UI thread otherwise it will look choppy or seem unresponsive. \$\endgroup\$
    – RobCurr
    Mar 1, 2013 at 19:06
  • \$\begingroup\$ thanks @RobCurr, regarding loading the resources on the splashscreen - If I load all the resources in my splash screen activity, how do I then access them form the new activity? I've not worked out how to do that yet. Thanks again appreciate it. \$\endgroup\$ Mar 1, 2013 at 19:23
  • \$\begingroup\$ I would think that you could create a content manager class that you could pass in via the constructor to each of your SurfaceView implementations. Inside the content manager you can keep a list of resources that you have loaded along with a reference count so that you have some idea when you can dispose of them. Normally once all references to an object are severed the garbage collection should occur but if you are maintaining a content manager then that will never occur hence keeping track of resource usage. \$\endgroup\$
    – RobCurr
    Mar 1, 2013 at 19:37
  • \$\begingroup\$ @RobbCurr - do you know of any resources that you could point me to on creating such a system - I've looked but I can't see anything - thanks!! \$\endgroup\$ Mar 1, 2013 at 20:48
  • \$\begingroup\$ If this is android then I think you should look at the android.content.res package as I think they have it handled for you. \$\endgroup\$
    – RobCurr
    Mar 1, 2013 at 21:36

2 Answers 2

1
\$\begingroup\$

Never do massive loading of resources all at once on the GUI thread - that's a certain approach to getting choppiness.

Making sure that your app can access your bitmaps from any activity is not difficult; simply set up a Singleton class that handles your bitmaps. You can use the Application class as Jay suggests, but personally I'd build an own singleton class for such activities as I don't feel that the Application class is the right place for such data.

I have open-sourced my own implementation of an ImageHandler for Android bitmaps in https://github.com/micabyte/android_game

See com.micabyte.android.graphics.ImageHandler

I've not implemented background loading in the ImageHandler (never needed it for the small bitmaps that I load via the ImageHandler, but you can check the SurfaceView implementation also in that project for how I handle loading a very large bitmap of 4096x4096 for game maps).

\$\endgroup\$
0
\$\begingroup\$

As RobCurr has mentioned, it's probably best to only load the necessary resources during the splash screen. Then load the absolute critical when you go to run the game. There are a couple different approaches to persisting your graphical resources across different activities. Basically your problem is "How to maintain global variables/state across different activities?"

Based on how you described your App, I think your best bet is to extend the Application class and load all the initial resources there. The Application class can then be accessed "globally" sorta of speak from any Activity. The Application class maintains it's existence during the life of your App. Note, if it's possible to load your resources statically, then creating static references is a better alternative to extending the Application class.

Some helpful links:
Extending Application Class
StackOverFlow post on Extending Application Class
Loading, Processing, Caching, Managing, Displaying Bitmaps Efficiently
Interesting group discussion about different options to maintaing global state

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .