2012-04-18 25 views
1

我正在研究一個應該有progressdialog的項目。但是因爲我沒有找到一種簡單的方式來設計progressdialog,我在想,最簡單的方法是用它自己的樣式創建一個自定義對話框類,然後逐幀動畫,然後顯示它。這裏是我的代碼:動畫列表根本沒有開始

的XML佈局對話框:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/dialog_background" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <ImageView 
     android:id="@+id/loaddialog_animation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/loadanimation" 
     android:layout_marginRight="10dp" /> 

    <TextView 
     android:id="@+id/loaddialog_text" 
     style="@style/SmallText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

這裏是我的對話框類:

public class LoadDialog extends Dialog 
{ 
    private TextView message; 
    ImageView image; 
    AnimationDrawable animation; 

    public LoadDialog(Context context) 
    { 
     super(context, R.style.Dialog); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.load_dialog); 

     message = (TextView) findViewById(R.id.loaddialog_text); 

     image = (ImageView) findViewById(R.id.loaddialog_animation); 
     image.setBackgroundResource(R.drawable.loadanimation); 
     animation = (AnimationDrawable) image.getBackground(); 
    } 

    public void setText(String msg) 
    { 
     message.setText(msg); 
    } 

    @Override 
    public void show() 
    { 
     super.show(); 
     animation.start(); 
    } 

    @Override 
    public void dismiss() 
    { 
     animation.stop(); 
     super.dismiss(); 
    } 
} 

和動畫資源:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > 
    <item android:drawable="@drawable/loadbar_01" android:duration="50" /> 
    <item android:drawable="@drawable/loadbar_02" android:duration="50" /> 
    <item android:drawable="@drawable/loadbar_03" android:duration="50"/> 
</animation-list> 

問題是當我試圖展示它時,動畫不會開始。我知道,有很多關於這個問題的話題,但這裏的事情,我曾嘗試:

  1. 把對話框顯示()在我的活動的不同部分:的onResume()和的onCreate() 。 onWindowFocusChanged()不是一個選項,因爲我想顯示一個對話框。顯示的對話框 - >焦點更改,對話框關閉 - >焦點更改 - >顯示無限量的對話框。
  2. 我已經嘗試過animation.setCallback(image),animation.setVisible(true,true),animation.invalidateSelf(),它們都不起作用。
  3. 我試過image.post()並把runnable作爲參數放在那裏,並且在運行方法啓動動畫時沒有運氣。

在這一點上,我開始用盡選擇。我只是試圖展示3個圖像,直到對話被解散。請如果你知道,我做錯了什麼,或任何其他方式,讓我知道!

在此先感謝!

+0

遺憾的標題拼寫錯誤! – 2012-04-18 19:19:40

回答

0

將android:oneshot =「true」更改爲android:oneshot =「false」,以便重複。它應該目前只做一個循環,因爲你的演出時間是50,那真的很快。我也將它改成也許200左右..

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > 
<item android:drawable="@drawable/loadbar_01" android:duration="200" /> 
<item android:drawable="@drawable/loadbar_02" android:duration="200" /> 
<item android:drawable="@drawable/loadbar_03" android:duration="200"/> 
</animation-list> 

此外,要設置的圖像在你的XML佈局,但動畫的背景,所以你需要更改的XML佈局到這一點:

<ImageView   android:id="@+id/loaddialog_animation"   android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/loadanimation" 
android:layout_marginRight="10dp" /> 

或改變你的代碼,以獲得SRC文件,而不是背景,就像這樣:

image = (ImageView) findViewById(R.id.loaddialog_animation); 
image.set(R.drawable.loadanimation); 
animation = (AnimationDrawable) image.getDrawable(); 
+0

謝謝,但沒有運氣,仍然沒有動畫... :( – 2012-04-18 20:56:47

+0

你都看到圖像,如果是這三幅圖像,你看到了嗎? – Nick 2012-04-19 00:43:26

+0

好吧,我看別的東西,但你也需要改變持續時間我先前的答案,我會更新我的答案... – Nick 2012-04-19 00:53:26

0

今天面臨着同樣的問題。在OnShowListener的onShow()方法中放置animation.start()對我來說是個訣竅。

public class CustomProgressDialog extends Dialog 
{ 
ImageView progressImage; 
AnimationDrawable frameAnimation; 

public CustomProgressDialog(Context context) 
{ 
    super(context); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.custom_progress); 
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

    progressImage = (ImageView) findViewById(R.id.custom_progress_image); 
    progressImage.setBackgroundResource(R.anim.progress_anim); 

    frameAnimation = (AnimationDrawable) progressImage.getBackground(); 

    OnShowListener osl = new OnShowListener() 
    { 
     @Override 
     public void onShow(DialogInterface dialog) 
     { 
      frameAnimation.start(); 
     } 
    }; 
    setOnShowListener(osl); 

    OnDismissListener odl = new OnDismissListener() 
    { 

     @Override 
     public void onDismiss(DialogInterface dialog) 
     { 
      frameAnimation.stop(); 
     } 
    }; 
    setOnDismissListener(odl); 
} 

}