2011-09-02 71 views
0

在我的應用程序得到的圖像從服務器和這些圖像構建animation.this所有的東西都沒有了是正確的方式和我的it.this創建方法是方法::混亂傳遞論點的AsyncTask

package com.animation; 

import java.io.InputStream; 
import java.net.URL; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.AnimationDrawable; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.ImageView; 
public class animation extends Activity { 
    Button Buttona; 
    AnimationDrawable animation; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView img = (ImageView) findViewById(R.id.simple_anim); 
     animation = new AnimationDrawable(); 

      try { 
       for(int i=0;i<54;i++) 
       {  
       xyz("girl000",i); 
       }   
      animation.setOneShot(false); 
      } catch (Exception e) { 
      } 
      img.setBackgroundDrawable(animation); 
      img.post(new Starter()); 

    } 


    public void xyz(String str,int x) 
    { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
     "http://201.109.115.111/MRESC/images/test/girl/"+"girl000"+x+".png") 
      .getContent()); 
      Drawable frame =new BitmapDrawable(bitmap); 
      animation.addFrame(frame, 50); 

     } catch (Exception e) { 

     } 

    } 
    class Starter implements Runnable { 

     public void run() { 
      animation.start();   
     } 


    } 
} 

現在我的問題是從服務器加載圖像需要很多時間,所以只是我打算使用asyncTask。但問題是我不能得到判斷,我該如何做到這一點? 你可以給我的例子(注:我知道的AsyncTask和已經使用,但問題是傳遞參數按我xyz()方法申報)

感謝 NIK

回答

1

這裏是代碼:

  • 請注意,循環現在位於後臺線程中
  • 每循環後,您將發佈設置動畫幀的進度
  • 在最後,你跑onPostExecute運行其餘代碼

請注意,這只是一個骨架和草圖,你需要了解和調試它,如果有任何問題。我還沒有運行代碼


    public class Animation extends Activity { 
     Button Buttona; 
     AnimationDrawable animation; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 
      animation = new AnimationDrawable(); 

      AsyncTask asyncTask = 
       new AsyncTask() { 

       @Override 
       protected Void doInBackground(Void... params) { 
        try { 
         // Execute this whole loop in background, so it doesn't 
         // block your UI Thread 
         for(int i=0;i<54;i++) {  
          xyz("girl000",i); 
         }   

        } catch (Exception e) { 
         return null; 
        } 
       } 

       public void xyz(String str,int x) { 
        try { 
         Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
           "http://201.109.115.111/MRESC/images/test/girl/"+"girl000"+x+".png") 
         .getContent()); 

         // publish progress so that the bitmap is set on the UI Thread 
         publishProgress(bitmap); 
        } catch (Exception e) { 
         // handle error 
        } 
       } 

       @Override 
       protected void onProgressUpdate(Bitmap... result) { 
        // handle the progress update to add the animation frame 
        Bitmap bitmap = result[0]; 
        Drawable frame =new BitmapDrawable(bitmap); 
        animation.addFrame(frame, 50); 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        if(result != null) { 
         // execute the rest of your instruction after the loop is over here 
         animation.setOneShot(false); 
         ImageView img = (ImageView) findViewById(R.id.simple_anim); 
         img.setBackgroundDrawable(animation); 
         img.post(new Starter()); 
        } else { 
         // handle error 
        } 
       } 
      }; 

      asyncTask.execute(); 
     } 

     class Starter implements Runnable { 
      public void run() { 
       animation.start();   
      } 
     } 
    } 

+0

你能舉一個例子嗎? –

+0

我正在考慮http://developer.android.com/reference/android/os/AsyncTask.html 而不是類asynctask的名稱。 –

+0

是的我明白,你將xyz中的參數封裝到一個新的Object wrapper中,並將Object Wrapper作爲參數傳遞給AsyncTask。讓我把代碼說明一下。查看更新的答案 – momo