我正在研究一個應該有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>
問題是當我試圖展示它時,動畫不會開始。我知道,有很多關於這個問題的話題,但這裏的事情,我曾嘗試:
- 把對話框顯示()在我的活動的不同部分:的onResume()和的onCreate() 。 onWindowFocusChanged()不是一個選項,因爲我想顯示一個對話框。顯示的對話框 - >焦點更改,對話框關閉 - >焦點更改 - >顯示無限量的對話框。
- 我已經嘗試過animation.setCallback(image),animation.setVisible(true,true),animation.invalidateSelf(),它們都不起作用。
- 我試過image.post()並把runnable作爲參數放在那裏,並且在運行方法啓動動畫時沒有運氣。
在這一點上,我開始用盡選擇。我只是試圖展示3個圖像,直到對話被解散。請如果你知道,我做錯了什麼,或任何其他方式,讓我知道!
在此先感謝!
遺憾的標題拼寫錯誤! – 2012-04-18 19:19:40