2013-04-13 16 views
0

我在哪裏我onCreateDialog()啓動一個後臺線程,最終調用這個方法的片段在第一時間:動畫無法啓動,即使在Handler.post()開始

protected void showMessageWithProgressWhenEmpty(final String message) { 
    handler.post(new Runnable() { 
     textView.setText(message); 
     textView.setCompoundDrawablesWithIntrinsicBounds(
      R.drawable.anim_spin_refresh, 0, 0, 0 
     ); 
     AnimationDrawable anim = (AnimationDrawable)textView.getCompoundDrawables()[0]; 
     anim.start(); 
    } 
} 

onCreateDialog(),創建handler 。後續調用showMessageWithProgressWhenEmpty()有意動畫。在對onCreateDialog()的調用約1秒內發生第一次對此方法的調用(沒有正確動畫的調用)。

我已經看到答案,提到動畫在Activity.onCreate()開始時不起作用,這就是爲什麼我要post致電handler

我在這裏錯過了什麼?

+0

收到一些問題,動畫,沒有完全相同的問題,你有,但我不得不無效視圖。 在setCompoundDrawablesWithIntrinsicBounds和啓動方法之後嘗試invalidate()。 – Alex

+0

我試圖在'setCompoundDrawablesWithIntrinsicBounds'以及'anim.start()'之後調用'invalidate()',但不起作用。任何其他想法? –

+0

textView.startAnimation(anim)? ,只是猜測。儘管這看起來像是在視圖實際「創建」之前嘗試啓動動畫。可能在您的DialogFragment中尋找像onStart這樣的函數,而不是您現在使用的onCreateDialog – Alex

回答

1
protected void showMessageWithProgressWhenEmpty(final String message) { 

     textView.setText(message); 
     textView.setCompoundDrawablesWithIntrinsicBounds(
      R.drawable.anim_spin_refresh, 0, 0, 0 
     ); 
     AnimationDrawable anim = (AnimationDrawable)textView.getCompoundDrawables()[0]; 
     // run the start() method later on the UI thread 
     textView.post(new Starter()); 
} 
class Starter implements Runnable { 

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

呼叫啓動後,設置文本視圖

+0

這是一個片段,因此它沒有此方法。有沒有類似的方法可以使用? –

+0

@DanielGabriel嘗試使用上述代碼更改片段。 – kyogs

+0

謝謝,這是它! –