我正在开发一个应该有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>
问题在于,当我尝试显示时,动画无法启动。我知道,关于这个问题有很多话题,但是我尝试过这些事情:
此时我开始没有选择了。我只是试图显示3个图像改变,直到对话框被解除。如果您知道,我做错了什么,或者任何其他选择,请告诉我!
提前致谢!
答案 0 :(得分: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();
答案 1 :(得分:0)
今天面临同样的问题。将animation.start()
放在OnShowListener的onShow()方法中对我来说很有用。
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);
}
}