当在外部触摸时被关闭时如何将动画添加到警报对话框

时间:2019-06-13 00:53:04

标签: android android-animation android-alertdialog dismiss

我正在尝试将动画添加到警报对话框,当用户在其外部触摸时将其关闭。默认情况下,在外部触摸时,对话框只是淡出或消失。有没有办法覆盖默认的取消或取消?我可以在按下按钮和调用按钮时添加动画,但无法弄清楚如何在外部触摸时将动画添加到默认的关闭动作中。请帮忙。预先感谢

我尝试过,但是没用

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>

1 个答案:

答案 0 :(得分:0)

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

经过一周的尝试,我找到了一种方法来替代默认的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>

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