2013-05-20 10 views
0

請參閱下面的代碼。無法從UI中的URL製作精靈Andengine Android中的線程

Activity ac= (Activity)cxt; 

    ac.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      load(); 
     } 
    }); 

這是構造在那裏我調用一個函數來從URL加載一個精靈。功能如下

private void load() 
{ 

    if(isInternetOn()){ 
     try { 
      ITexture mTexture = new BitmapTexture(mEngine.getTextureManager(), new IInputStreamOpener() { 
       @Override 
       public InputStream open() throws IOException { 

        URL url = new URL("http://tenlogix.com/cupcakemania/Ads/burgermaker.png"); 

        HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoInput(true); 
        connection.connect(); 
        InputStream input = connection.getInputStream(); 
        BufferedInputStream in = new BufferedInputStream(input);  
        return in; 
       } 
      }); 
      mTexture.load(); 
      MyImageFromWeb = TextureRegionFactory.extractFromTexture(mTexture); 



     } catch (IOException e) { 
      Log.d("TenlogixAds"," "+e); 
     } 

     AdSprite = new Sprite(0, 0, MyImageFromWeb, mEngine.getVertexBufferObjectManager()); 
    } 
    else{ 
     Log.d("TenlogixAds"," No Internet Connection Detected.. "); 
     AdSprite = null; 
    } 

} 

的問題是,如果我現在調用加載功能在UI線程,但是當我把它在UI線程其未加載圖像加載。

在UI線程中調用它非常重要,因爲我想要執行此任務是背景導致遊戲運行問題。

我也試過AsyncTask,但是我用UI線程描述的問題與異步任務是一樣的。

任何人都可以幫助我解決這個問題嗎?

我想從網絡加載圖像,我希望它在後臺加載,導致遊戲正在播放任何延遲或問題。

+0

使用異步任務的兄弟,因爲當它被稱爲UI線程沒有恢復call.But異步答覆你,如果它得到來自以前呼叫的迴應。 –

回答

1

可以使用異步任務還調試值mTexture不爲null。

public void loadImageFromServer() { 

    final IAsyncCallback callback = new IAsyncCallback() { 

     @Override 
     public void workToDo() { 
      Log.e("BaseActivity", "OnWhat to do working"); 
      if (isInternetOn()) { 
       try { 
        ITexture mTexture = new BitmapTexture(
          mEngine.getTextureManager(), 
          new IInputStreamOpener() { 
           @Override 
           public InputStream open() 
             throws IOException { 

            URL url = new URL(
              "http://tenlogix.com/cupcakemania/Ads/burgermaker.png"); 

            HttpURLConnection connection = (HttpURLConnection) url 
              .openConnection(); 
            connection.setDoInput(true); 
            connection.connect(); 
            InputStream input = connection 
              .getInputStream(); 
            BufferedInputStream in = new BufferedInputStream(
              input); 
            return in; 
           } 
          }); 
        if (mTexture != null) { 
         mTexture.load(); 

         MyImageFromWeb = TextureRegionFactory 
           .extractFromTexture(mTexture); 
        } 

       } catch (IOException e) { 
        Log.d("TenlogixAds", " " + e); 
       } 

      } else { 
       Log.d("TenlogixAds", " No Internet Connection Detected.. "); 
       AdSprite = null; 
      } 

     } 

     @Override 
     public void onComplete() { 
      // can create after Initializing MyImageFromWeb 

      if (MyImageFromWeb != null) { 

       AdSprite = new Sprite(0, 0, MyImageFromWeb, 
         mEngine.getVertexBufferObjectManager()); 
      } 

     } 

    }; 

    aBaseActivity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      new AsyncTaskLoader().execute(callback); 
     } 
    }); 

} 

IAsyncCallback是一個接口。樣子:

public interface IAsyncCallback { 
    // =========================================================== 
    // Methods 
    // =========================================================== 
    public abstract void workToDo(); 

    public abstract void onComplete(); 

} 

而且我AsyncTaskLoader類是像:

import android.os.AsyncTask; 

public class AsyncTaskLoader extends 
     AsyncTask<IAsyncCallback, Integer, Boolean> { 
    // =========================================================== 
    // Fields 
    // =========================================================== 
    IAsyncCallback[] _params; 

    // =========================================================== 
    // Inherited Methods 
    // =========================================================== 
    @Override 
    protected Boolean doInBackground(IAsyncCallback... params) { 
     this._params = params; 
     int count = params.length; 
     for (int i = 0; i < count; i++) { 
      params[i].workToDo(); 
     } 
     return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     int count = this._params.length; 
     for (int i = 0; i < count; i++) { 
      this._params[i].onComplete(); 
     } 
    } 
} 
+0

謝謝,像一個魅力工程 –

3

當前您正在調用load();方法在UI線程上凍結UI線程直到網絡操作未完成。爲了在後臺線程中獲得圖像,你需要調用load(); runOnUiThread之外爲:

new Thread(new Runnable() { 

     @Override 
     public void run() { 
     // call load() method here 
      load(); 
      ac.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // update image here without interrupting user 
        ..... 
      } 
     }); 
     } 
}).start(); 
+0

但他想在他的比賽中延遲和問題:) –

0

我不是Andengine的專業人員。希望我可以幫助你:D

我在使用在線資源之前先使用一些應用程序先使用android中的緩存存儲方法緩存文件。

您可以將文件保存到Conetext.getCacheDir() ,系統將在應用程序卸載後自動釋放內存。

public abstract File getCacheDir()

更多信息。 使用HttpURLConnection類下載圖像 您可以使用FileOutputStream中保存圖像

FileOutputStream fos = new FileOutputStream(new File(Context.getCacheDir() + "/my_image.png")); 
    fos.write(.....); 
    fos.flush(); 

然後你就可以正常使用了從Context.getCacheDir(圖像)+「/my_image.png」

+0

這不是我的問題。從URL加載圖片時,我的問題是延遲。 –

+0

抱歉誤會您的問題:D 您可以使用Android asynctask || runOnUiThread, 我總是使用asynctask來加載資源。 http://developer.android.com/reference/android/os/AsyncTask.html –