【问题标题】:android custom progress Dialog animation not workingandroid自定义进度对话框动画不起作用
【发布时间】:2015-08-06 21:28:21
【问题描述】:

我写了自定义对话框。对话框正确显示,但在关闭时我想添加动画(slide_out_top)。对话框的重力是顶部。下面是对话框的来源:

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。现在我想添加 slide_out_top 动画 在我关闭对话框动画无法正常工作的那一刻

我该如何解决这个问题?

【问题讨论】:

    标签: android android-animation android-dialog


    【解决方案1】:

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

    1)你可以使用Dismiss监听器

    @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"

    【讨论】:

    • 感谢我用 style.xml 文件尝试过,但我的对话框重力是 TOP,我的 android:windowExitAnimation 起始中心位置。你知道这个解决方案
    • schemas.android.com/apk/res/android" android:toYDelta="100%p" android:duration="@android:integer/config_longAnimTime" /> 添加 android:fromYDelta="10% p"
    • 我想在 2 秒后关闭我的对话框。我如何编写计时器来关闭我的对话框,并且我的动画应该可以工作 @bpA 52
    • @ bpA 我明白了,但两秒钟后我可以关闭我的对话框,但动画现在开始了
    • 我更新了我的代码。请看一下。我的动画不工作
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 2012-12-29
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多