Android Open Source - Marble-Run New Loading Activity






From Project

Back to project page Marble-Run.

License

The source code is released under:

Apache License

If you think the Android project Marble-Run listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package nttu.edu.activities;
/*from  ww w  . ja v a  2s .c o  m*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;

import nttu.edu.R;
import nttu.edu.graphics.Art;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class NewLoadingActivity extends Activity {
  public ProgressBar bar;
  private AssetManager assetManager;
  public Handler handler;
  public ProgressTask task;
  
  private final String[] list = {
  // Art.sprites
  "art/sprites.png" };
  
  private class ProgressTask extends AsyncTask<Void, Void, Void> {
    public int totalByteSize;
    public int currentByteSize;
    public Queue<Bitmap> bitmapQueue;
    public Queue<byte[]> byteQueue;
    
    public ProgressTask() {
      totalByteSize = 0;
      currentByteSize = 0;
      bitmapQueue = new LinkedList<Bitmap>();
      byteQueue = new LinkedList<byte[]>();
    }
    
    public void onPostExecute(Void params) {
      Art.sprites = bitmapQueue.remove();
      finish();
    }
    
    public void onPreExecute() {
      try {
        for (int i = 0; i < list.length; i++) {
          byte[] bytes = readFromStream(list[i]);
          totalByteSize += bytes.length;
          byteQueue.add(bytes);
        }
        bar.setMax(totalByteSize);
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    
    public void onProgressUpdate(Void... params) {
      bar.setProgress(currentByteSize);
    }
    
    @Override
    protected Void doInBackground(Void... params) {
      while (currentByteSize < totalByteSize) {
        try {
          Thread.sleep(1000);
          if (byteQueue.size() > 0) {
            byte[] bytes = byteQueue.remove();
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            bitmapQueue.add(bitmap);
            currentByteSize += bytes.length;
            this.publishProgress();
          }
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      return null;
    }
    
    private byte[] readFromStream(String path) throws IOException {
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int length = 0;
      InputStream input = assetManager.open(path);
      while (input.available() > 0 && (length = input.read(buffer)) != -1)
        output.write(buffer, 0, length);
      return output.toByteArray();
    }
    
  }
  
  public void onCreate(Bundle b) {
    super.onCreate(b);
    this.setContentView(R.layout.progressbar);
    assetManager = this.getAssets();
    handler = new Handler();
    task = new ProgressTask();
    bar = (ProgressBar) this.findViewById(R.id.loadingBar);
    if (bar == null) throw new RuntimeException("Failed to load the progress bar.");
    task.execute();
  }
  
  public void finish() {
    Intent intent = new Intent(this, MenuActivity.class);
    intent.putExtra("Success Flag", Art.sprites != null);
    this.setResult(RESULT_OK, intent);
    super.finish();
  }
}




Java Source Code List

nttu.edu.activities.LevelSelectionActivity.java
nttu.edu.activities.MenuActivity.java
nttu.edu.activities.NewLoadingActivity.java
nttu.edu.activities.PlayActivity.java
nttu.edu.activities.ScoreActivity.java
nttu.edu.activities.SettingsActivity.java
nttu.edu.alt.NewBall.java
nttu.edu.alt.NewCue.java
nttu.edu.alt.NewHole.java
nttu.edu.alt.Obstacle.java
nttu.edu.ball.Ball.java
nttu.edu.ball.Cue.java
nttu.edu.ball.Marble.java
nttu.edu.entity.Border.java
nttu.edu.entity.Bumper.java
nttu.edu.entity.Coin.java
nttu.edu.entity.Connector.java
nttu.edu.entity.CurvePipe.java
nttu.edu.entity.Entity.java
nttu.edu.entity.Funnel.java
nttu.edu.entity.Hole.java
nttu.edu.entity.Path.java
nttu.edu.entity.Pipe.java
nttu.edu.entity.Ramp.java
nttu.edu.entity.ShortFunnel.java
nttu.edu.entity.Tee.java
nttu.edu.entity.Terrain.java
nttu.edu.entity.Void.java
nttu.edu.graphics.Art.java
nttu.edu.graphics.RenderView.java
nttu.edu.handler.Accelero.java
nttu.edu.handler.ImageInfo.java
nttu.edu.handler.Info.java
nttu.edu.handler.Loading.java
nttu.edu.handler.ModPlayer.java
nttu.edu.handler.MusicHandler.java
nttu.edu.hud.BestScore.java
nttu.edu.hud.Compass.java
nttu.edu.hud.GoalCompass.java
nttu.edu.hud.HUDMenu.java
nttu.edu.hud.HUDScore.java
nttu.edu.hud.MarbleCompass.java
nttu.edu.hud.TimeBasedScore.java
nttu.edu.level.HUD.java
nttu.edu.level.Stage.java
nttu.edu.score.Format.java
nttu.edu.score.Score.java
nttu.edu.sound.Sound.java