2012-04-03 70 views
0

這裏是我的ProgressBar類的核心:如何創建一個進度條類,從Android的/ res和/ assets文件夾加載不同類型的文件?

package nttu.edu.activities; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Stack; 

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.Bundle; 
import android.os.Handler; 
import android.widget.ProgressBar; 

public class NewLoadingActivity extends Activity 
{ 
    private ProgressBar bar; 
    private AssetManager assetManager; 
    private Load loading; 

    private class Load 
    { 
     public Stack<byte[]> stack; 
     public Stack<Bitmap> results; 
     public Handler handler; 
     public int totalByteSize; 
     public int currentByteSize; 

     private final String[] list = 
     { "art/sprites.png" }; 

     public Load() 
     { 
      stack = new Stack<byte[]>(); 
      results = new Stack<Bitmap>(); 
      handler = new Handler(); 
      totalByteSize = 0; 
      currentByteSize = 0; 
     } 

     public void loadBar() 
     { 
      try 
      { 
       for (int i = 0; i < list.length; i++) 
       { 
        byte[] bytes = readFromStream(list[i]); 
        stack.push((byte[]) bytes); 
        totalByteSize += bytes.length; 
       } 
      } 
      catch (IOException e) 
      { 
       throw new RuntimeException(e); 
      } 
      bar.setMax(totalByteSize); 
     } 

     public void startHandler() 
     { 
      handler.post(new Runnable() 
      { 
       public void run() 
       { 
        while (currentByteSize < totalByteSize) 
        { 
         try 
         { 
          Thread.sleep(1000); 
          bar.setProgress(currentByteSize); 
         } 
         catch (InterruptedException e) 
         { 
          e.printStackTrace(); 
         } 
        } 
       } 
      }); 
     } 

     public void startLoad(){ 
      while (stack.size() > 0){ 
       byte[] bytes = (byte[]) stack.pop(); 
       Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
       if (bitmap != null) 
        currentByteSize += bytes.length; 
       results.push((Bitmap) bitmap); 
      } 
      sort(); 
      finish(); 
     } 

     //This is the place to load specific assets into a class. 
     private void sort(){ 
      Art.sprites = (Bitmap) results.pop(); 
     } 

     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(); 
     loading = new Load(); 
     //bar = new ProgressBar(this); 
     bar = (ProgressBar) this.findViewById(R.id.loadingBar); 
     loading.loadBar(); 
     loading.startHandler(); 
     loading.startLoad(); 
    } 

    public void finish() 
    { 
     Intent intent = new Intent(this, BaseActivity.class); 
     intent.putExtra("Success Flag", Art.sprites != null); 
     this.setResult(RESULT_OK, intent); 
     super.finish(); 
    } 
} 

到目前爲止,我只能通過增加它們的路徑進入sort()函數加載位圖。

我只能加載位圖的原因是我不知道如何區分位圖加載,聲音加載和資源加載,但我想把所有需要加載的東西放到1個大類中。我只是不知道如何分割它們。

我試圖通過目錄名分裂所需的文件後,或將它們整理到自己的子目錄,像這樣:

Snippet of the /assets folder's subdirectories

但隨後我會發現自己被困在尋找一個新的解決方案,以遞歸目錄列表和東西,仍然無法修復它。過去兩天我一直在解決這個問題,但沒有什麼結果。

這裏是我的這一結果,所以要證明我真的一直在做我的家庭作業:

public void loadStack(AssetManager manager, String path, int level) { 
    try { 
     String[] list = manager.list(path); 
     if (list != null) { 
      for (int i = 0; i < list.length; i++) { 
       if (level >= 1) loadStack(manager, path + "/" + list[i], level + 1); 
       else if (level == 0) loadStack(manager, list[i], level + 1); 
       else { 
        byte[] byteBuffer = readFromStream(path); 
        assetStack.push(byteBuffer); 
        totalByteSize += byteBuffer.length; 
       } 
      } 
     } 
    } 
    catch (IOException e) { 
     Log.e("Loading", "Occurs in AssetLoad.loadStack(AssetManager, String, int), file can't be loaded: " + path); 
     throw new RuntimeException("Couldn't load the files correctly."); 
    } 
} 

我一直在繼續做更多的研究,關於如何分割在不同的文件類型加載屏幕,但是Stack Overflow沒有任何關於如何加載不同文件的問題。因此,我決定想出一個簡單,粗糙和不好的答案,這就是在位圖中創建所有資源,並犧牲遊戲應用程序的聲音文件。說實話,我不想這樣做。

請幫助我,例如給我提示,提示或任何你的袖子。我應該怎麼做才能在我的進度條中加載各種文件類型?我需要尋找什麼?

在此先感謝。

回答

0

以下是如何爲任意文件創建ProgressBar。

package nttu.edu.activities; 

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(); 
    } 
} 
相關問題