是否可以在对话框中设置动画列表?

时间:2013-08-11 22:37:58

标签: android animation dialog android-animation

我创建了一个对话框:

    Dialog dialog = new Dialog(myContext, R.style.dialog_loading);
    dialog.setCancelable(false);
    RelativeLayout contentView = (RelativeLayout) ((Activity) myContext).getLayoutInflater().inflate(R.layout.dialog_loading, null);
    dialog.setContentView(contentView);
    dialog.show

我也试过这个:

static public Dialog PleaseWait() {
    Dialog dialog = new Dialog(myContext, R.style.dialog_loading);
    dialog.setCancelable(false);
    RelativeLayout contentView = (RelativeLayout) ((Activity) myContext).getLayoutInflater().inflate(R.layout.dialog_loading, null);
    dialog.setContentView(contentView);
    ImageView loading=(ImageView) contentView.findViewById(R.id.loading);
  loading.setBackgroundResource(R.drawable.anim_loading);
  ((AnimationDrawable) loading.getBackground()).start();
    return dialog;
}

我也试过这个:

final Dialog dialog = new Dialog(My_Activity.this);
    //Set Background of Dialog - Custom 
    //dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbg);
    //Remove the Title
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    //Set the View of the Dialog - Custom
    dialog.setContentView(R.layout.dialog_loading);
    //Set the title of the Dialog
    //dialog.setTitle("Title...");
    ImageView progressSpinner = (ImageView) dialog.findViewById(R.id.loading);

    //Set the background of the image - In this case an animation (/res/anim folder)
    progressSpinner.setBackgroundResource(R.drawable.anim_loading);
    //Get the image background and attach the AnimationDrawable to it.
    final AnimationDrawable progressAnimation = (AnimationDrawable) progressSpinner.getBackground();
    //Start the animation after the dialog is displayed.
    dialog.setOnShowListener(new OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
    progressAnimation.start();
    }
    });
    dialog.show();

布局xml如下:

           <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_black"
            android:gravity="center" >

<ImageView
    android:id="@+id/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="24dp"
    android:src="@drawable/anim_loading"  />

        </RelativeLayout>

anim_loading如下:

<?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/loading1" android:duration="150" />
    <item android:drawable="@drawable/loading2" android:duration="150" />
    <item android:drawable="@drawable/loading3" android:duration="150" />
    <item android:drawable="@drawable/loading4" android:duration="150" />
</animation-list>

图像显示,但不是动画。如何在循环中启动此动画?

2 个答案:

答案 0 :(得分:3)

    Dialog dialog = new Dialog(this);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            RelativeLayout contentView = (RelativeLayout) ((Activity) this)
                    .getLayoutInflater().inflate(R.layout.dialog_loading, null);
            dialog.setContentView(contentView);

            ImageView image = (ImageView) contentView.findViewById(R.id.loading);
            final AnimationDrawable animation = (AnimationDrawable) image.getDrawable();
            dialog.setCancelable(false);



            dialog.setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                animation.start();
                }
                });
            dialog.show();

答案 1 :(得分:0)

尝试手动启动动画

ImageView image = (ImageView) contentView.findViewById(R.id.loading);
AnimationDrawable animation = (AnimationDrawable) image.getDrawable();
animation.start();