I'm developing an Android game. Basically I want to pre-load all graphics/sounds when the app is first loaded. But I also would like to show a progress bar as this is happening. Here is a snippet of Java/Android code to display a progress bar while doing some work:
public class LoadingScreen extends Activity {
private static int progress = 0;
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadingscreen);
progressBar = (ProgressBar) findViewById(R.id.progress);
//---do some work in background thread---
new Thread(new Runnable()
{
public void run()
{
while (progressStatus < 100)
{
progressStatus = doSomeWork();
//---Update the progress bar---
handler.post(new Runnable()
{
public void run() {
progressBar.setProgress(progressStatus);
}
});
}
//---hides the progress bar---
handler.post(new Runnable()
{
public void run() {
progressBar.setVisibility(8);
}
});
}
private int doSomeWork()
{
try
{
//---simulate doing some work---
Thread.sleep(50);
} catch (InterruptedException e)
{
}
return ++progress;
}
}).start();
}
My question is: how do I design my Asset Manager to load "chunks" of data (bitmaps and sounds/music) in the doSomeWork()
function? Would it be some sort of stack that I keep popping off the next asset to load? How do I determine how much of the progress bar I should increment after loading a "chunk" of data?
Is there a better way to approach this or is this the correct way?