android自定义进度对话框动画无效

时间:2015-08-06 11:23:32

标签: android android-animation android-dialog

我写了自定义dialog.Dialog正确显示,但在解雇时我想添加动画(slide_out_top).Dialog的重力是Top.Below是对话源:

public class MessageDialog extends Dialog {

private LinearLayout mLinearLayout;
private TextView mText;
private ImageView mAttentionImage;


private int mBackgroundColor;
private String mMessage;
private int mImage;
private Animation bottondown;
private RelativeLayout body;


public MessageDialog(Context context) {
    super(context);
}

public MessageDialog(Context context, int custom_message_dialog) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.view_message);


    WindowManager.LayoutParams wmlp = getWindow().getAttributes();
    getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    wmlp.gravity = Gravity.TOP;
    wmlp.width = WindowManager.LayoutParams.MATCH_PARENT;
    wmlp.height = 300;
    wmlp.horizontalMargin = 0;
    wmlp.verticalMargin = 0;
    getWindow().setBackgroundDrawableResource(android.R.color.transparent);



    Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
            R.anim.slide_in_top);

    bottondown=AnimationUtils.loadAnimation(getContext(),
            R.anim.slide_out_top);


    body  = (RelativeLayout) findViewById(R.id.u_message_body);
    //body.startAnimation(bottomUp);

    mLinearLayout = (LinearLayout) findViewById(R.id.u_message_content);
    mText = (TextView) findViewById(R.id.u_message_text);
    mAttentionImage = (ImageView) findViewById(R.id.u_message_dialog_image);

    mLinearLayout.setBackgroundColor(getContext().getResources().getColor(mBackgroundColor));
    mText.setText(mMessage);
    mAttentionImage.setBackgroundResource(mImage);


    final Timer t = new Timer();
    t.schedule(new TimerTask() {
        public void run() {
            try {

               dismiss();

                t.cancel();
            } catch (Exception ex) {
            }
        }
    }, 2000);






    findViewById(R.id.u_message_dialog_close).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            dismiss();
        }
    });
}

}

这是我的动画xml代码

slide_in_top

<?xml version="1.0" encoding="utf-8"?>

<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromYDelta="-100%p"
    android:toYDelta="0%p" />

slide_out_top.xml

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:toYDelta="10%p"
    android:duration="@android:integer/config_longAnimTime" />

正如我所说,我的对话重力是Top.Now我想添加slide_out_top动画 在我解除对话框动画不能正常工作的那一刻

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点

1)您可以使用Dismiss listener

@Override
    public void setOnDismissListener(OnDismissListener listener) {
        super.setOnDismissListener(listener);

  body.setAnimation(bottondown);
    }

2)您可以在onCreate方法

中定义样式
getDialog().getWindow() .getAttributes().windowAnimations = R.style.MyAnimation_Window;

and in styles 
<style name="MyAnimation.Window" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">"place Enter anim"</item>
        <item name="android:windowExitAnimation">"place exit anim"</item>
    </style>

编辑:

在animation.xml文件中添加 android:toYDelta="-15%p"