【问题标题】:How to add animation to an alert dialog when it is dismissed when touched outside如何在外部触摸时将动画添加到警报对话框
【发布时间】:2019-10-27 13:11:08
【问题描述】:

我正在尝试向警报对话框添加动画,当用户触摸它外部时它被关闭。默认情况下,当它在外面被触摸时,对话框只是淡出或消失。有没有办法覆盖默认的解雇或取消?我能够在按下按钮和调用按钮时添加动画,但无法弄清楚如何在外部触摸按钮时将动画添加到默认关闭操作中。请帮忙。提前致谢

我试过了,还是不行

alert.setOnCancelListener()
alert.setOnDismissListener()

这是我的代码:

View rl = getActivity().findViewById(R.id.map) ;

                AlertDialog.Builder alert = new 
AlertDialog.Builder(rl.getContext(), R.style.CFDialog);
                LayoutInflater inflater = 
getActivity().getLayoutInflater();
                View v = 
inflater.inflate(R.layout.dialog_footer_layout, null);

                Animation transition_in_view = 
AnimationUtils.loadAnimation(rl.getContext(), 
R.anim.alert_present);
                Animation transition_out_view = 
AnimationUtils.loadAnimation(rl.getContext(), 
R.anim.alert_dismiss);

                //customer animation appearance

                v.setAnimation( transition_in_view );
                v.startAnimation( transition_in_view );

                alert.setView(v);
                alert.setOnCancelListener(new 
DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface 
dialogInterface) {

                        v.startAnimation(transition_out_view);

                    }
                });

                alert.setOnDismissListener()
                Button button = 
v.findViewById(R.id.configuration_toggle_button);

                AlertDialog helpDialog = alert.create();
                button.setOnClickListener(new 
View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        v.startAnimation(transition_out_view);

transition_out_view.setAnimationListener(new 
Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation 
animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation 
animation) {

                                helpDialog.dismiss();

                            }

                            @Override
                            public void onAnimationRepeat(Animation 
animation) {

                            }
                        });

                    }
                });

                helpDialog.setOnCancelListener(new 
DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface 
dialogInterface) {
                        v.startAnimation(transition_out_view);
                        return;
                    }
                });

                helpDialog.setOnDismissListener(new 
DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface 
dialogInterface) {
                        v.startAnimation(transition_out_view);
                        return;
                    }
                });

                // Hide after some seconds
                final Handler handler  = new Handler();
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (helpDialog.isShowing()) {

                            v.startAnimation(transition_out_view);

transition_out_view.setAnimationListener(new 
Animation.AnimationListener() {
                                @Override
                                public void 
onAnimationStart(Animation animation) {

                                }

                                @Override
                                public void 
onAnimationEnd(Animation animation) {

                                    helpDialog.dismiss();

                                }

                                @Override
                                public void 
onAnimationRepeat(Animation animation) {

                                }
                            });
                        }
                    }
                };

                alert.setOnDismissListener(new 
DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        handler.removeCallbacks(runnable);
                    }
                });


                handler.postDelayed(runnable, 5000);



helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams wmlp = 
helpDialog.getWindow().getAttributes();


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    wmlp.gravity = Gravity.TOP;

                    wmlp.y = 200;   //y position
                    helpDialog.show();
                } else {

//                        if (Build.VERSION.SDK_INT <= 
Build.VERSION_CODES.O)

                    wmlp.gravity = Gravity.TOP;

                    wmlp.y = 180;   //y position
                    helpDialog.show();

                }

这是对话框的样式:

<style name="CFDialog" parent="Theme.AppCompat.Translucent">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimAmount">0.5</item>
    <item name="colorPrimaryDark">@color/transparent</item>
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>

【问题讨论】:

    标签: android android-animation android-alertdialog dismiss


    【解决方案1】:

    我想做的是在对话框被外部触摸时为其添加动画。触摸外部代码很容易,但制作动画的方式却很难。我希望它向上滑动。

    经过一周的尝试寻找解决方案,我找到了一种方法来覆盖默认的dialog.setCanceledOnTouchOutside(true) 并为其设置动画。

    这就是我所做的。我研究了http://iserveandroid.blogspot.com/2011/04/how-to-dismiss-your-non-modal-dialog.html 并从这里我能够搜索如何将动画添加到FLAG_WATCH_OUTSIDE_TOUCH 并遇到了这个 WindowManager with Animation (is it possible?)

    从这里我能够实现这个wmlp.windowAnimations = R.style.CFDialog_Animation;

    这是我的代码:

    AlertDialog helpDialog = alert.create();
    
    helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    WindowManager.LayoutParams wmlp = 
    helpDialog.getWindow().getAttributes();
    
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                    {
                        wmlp.gravity = Gravity.TOP;
    
                        wmlp.y = 200;   //y position
                        helpDialog.show();
                    } else {
    
    //                        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O)
    
                        wmlp.gravity = Gravity.TOP;
                        wmlp.y = 180;   //y position
                        helpDialog.show();
    
                    }
    
                    //this code below is what overrides it and add the animation
    
                    wmlp.windowAnimations = R.style.CFDialog_Animation; 
    
    
    
    
            ///optional, your preference
    
     Window window = helpDialog.getWindow();
    
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    
    window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    

    我还利用可选代码使其成为非模型,因此当您触摸对话框外的对象时可以取消/关闭它(可选)

    现在,当您在对话框外部单击时,它会向上滑动。当然,您可以使用自己的动画。 wmlp.windowAnimations = android.R.style.Animation_Translucent; 让它横着走。我不知道为什么。

    这是我的风格:

    <style name="CFDialog.Animation">
    
        <item name="android:windowExitAnimation">@anim/alert_dismiss</item>
    </style>
    

    和 alert_dimiss 动画

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <alpha
        android:interpolator="@android:anim/anticipate_interpolator"
        android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
    
    <translate android:fromYDelta="00%" android:toYDelta="-100%" 
    android:duration="500"/>
    </set>
    

    希望这可以帮助其他人寻找解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2012-08-19
      相关资源
      最近更新 更多