2011-05-08 64 views
0

我想動畫圖像(imageView)與他們的相關文本(textView)一起。 我遵循了我在這裏得到的建議,並使用AsyncTask和處理程序來UI線程動態更改圖像和文本。這應該是一個簡單的代碼,但問題是我得到/只看到最後的圖像和文本。在下面的代碼中,只會顯示im2(第二個圖像)和t2(第二個文本)。動畫的文字和圖像

有人可以告訴問題是什麼嗎?

我的代碼是這樣的:

public class picshow extends Activity 
{ 
    private static Integer[] Imgid = {R.drawable.im1, R.drawable.im2}; 
    private static Integer[] Strid = {R.string.t1, R.string.t2}; 

    private Handler handToUI = new Handler(); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView image = (ImageView) findViewById(R.id.picView); 
     TextView text = (TextView) findViewById(R.id.usertext); 

     myTask task = new myTask(); 
     task.execute("nothing"); 
    } 



    private class myTask extends AsyncTask<String, Void, String > 
    { 

     @Override 
     protected String doInBackground(String... params) 
     { 
      //Do noting and return a String. a preparation for future code 
      return params[0]; 
     } 

     @Override 
     protected void onPostExecute(String result) 
     { 

      //send message to UI 
      for(int i = 0;i<Strid.length;i++) 
      { 
       final int si = Strid[i]; 
       final int ii = Imgid[i]; 
       try 
       { 
        Thread.sleep(5000); 
        handToUI.post(new Runnable() 
        { 
         ImageView image = (ImageView) findViewById(R.id.picView); 
         TextView text = (TextView) findViewById(R.id.usertext); 
         public void run() 
         { 
          text.setText(si); 
          image.setImageResource(ii); 
         } 
        }); 
       } 
       catch (InterruptedException e) 
       { 
        e.printStackTrace(); 
       } 



      } 
     } 
    } 

} 

感謝 梨皮

回答

0

試試這個。我已更正您的代碼

handToUI.post(new Runnable() 
{ 
    ImageView image = (ImageView) findViewById(R.id.picView); 
    TextView text = (TextView) findViewById(R.id.usertext); 
    public void run() 
    { 
      try 
      { 
       Thread.sleep(5000); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      text.setText(si); 
      image.setImageResource(ii); 
    } 
}); 

我希望它對您有所幫助。