如何使用材质设计的显示效果在Android上显示对话框?

如何使用材质设计的显示效果在Android上显示对话框?,android,animation,android-alertdialog,material-design,Android,Animation,Android Alertdialog,Material Design,我有一个带有浮动操作按钮的活动。当我按下FAB时,会显示一个警报对话框。我想通过使用Android的材质设计中的“显示效果”或“弯曲运动”之类的东西来为其外观设置动画。只有一个用于更改现有视图可见性的示例 对于警报对话框,如何实现这一点?如果您有自定义视图(用XML定义),可以尝试以下方法: AlertDialog a = new AlertDialog.Builder(this)...blablabla; View v = a.findViewById(R.layout.example);

我有一个带有浮动操作按钮的
活动
。当我按下FAB时,会显示一个
警报对话框
。我想通过使用
Android
材质设计
中的“显示效果”或“弯曲运动”之类的东西来为其外观设置动画。只有一个用于更改现有视图可见性的示例

对于
警报对话框
,如何实现这一点?

如果您有自定义视图(用XML定义),可以尝试以下方法:

AlertDialog a = new AlertDialog.Builder(this)...blablabla;
View v = a.findViewById(R.layout.example);

// get the center for the clipping circle
int cx = (v.getLeft() + v.getRight()) / 2;
int cy = (v.getTop() + v.getBottom()) / 2;

// get the final radius for the clipping circle
int finalRadius = Math.max(v.getWidth(), v.getHeight());

// create the animator for this view (the start radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, 0, finalRadius);

// make the view visible and start the animation
v.setVisibility(View.VISIBLE);
anim.start();
要使用反向动画将其隐藏,请执行以下操作:

View v = <yourAlertDialog>.findViewById(R.layout.example);

// get the center for the clipping circle
int cx = (v.getLeft() + v.getRight()) / 2;
int cy = (v.getTop() + v.getBottom()) / 2;

// get the initial radius for the clipping circle
int initialRadius = v.getWidth();

// create the animation (the final radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, initialRadius, 0);

// make the view invisible when the animation is done
anim.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        v.setVisibility(View.INVISIBLE);
    }
});

// start the animation
anim.start();
View v=.findviewbyd(R.layout.example);
//获取剪切圆的中心
intcx=(v.getLeft()+v.getRight())/2;
int cy=(v.getTop()+v.getBottom())/2;
//获取剪裁圆的初始半径
int initialRadius=v.getWidth();
//创建动画(最终半径为零)
Animator anim=ViewAnimationUtils.createCircularVeal(v、cx、cy、initialRadius,0);
//动画完成后,使视图不可见
anim.addListener(新的AnimatorListenerAdapter(){
@凌驾
AnimationEnd上的公共无效(Animator动画){
super.onAnimationEnd(动画);
v、 设置可见性(视图不可见);
}
});
//启动动画
anim.start();

在显示对话框之前,我通过调用对话框的
setOnShowListener
做了类似的事情。我仍然需要优化动画,但这是一个开始:

dialogToAnimate.setOnShowListener(new OnShowListener() {
    @Override public void onShow(DialogInterface dialog) {
        // Remember that ViewAnimationUtils will not work until API 21.
        final View view = dialogToAnimate.getWindow().getDecorView();
        final int centerX = view.getWidth() / 2;
        final int centerY = view.getHeight() / 2;
        // TODO Get startRadius from FAB
        // TODO Also translate animate FAB to center of screen?
        float startRadius = 20;
        float endRadius = view.getHeight();
        Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, startRadius, endRadius);
        animator.setDuration(1000);
        animator.start();
    }
});

具有上述API 21显示效果的警报对话框=>

public final void customAlertDialog(final Activity mActivity)
 {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
        final View view = LayoutInflater.from(mActivity).inflate(R.layout.alert_dialog_simple_msg,null);
        alertDialogBuilder.setView(view);
        final AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        TextView tvOk= (TextView) view.findViewById(R.id.tvOk);
        final Animator[] animHide = {null};
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            alertDialog.show();
            view.post(new Runnable() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void run() {
                    int cx = (int)view.getWidth() / 2;
                    int cy = (int) view.getHeight() / 2;
                    float finalRadius = (float) Math.hypot(cx, cy);
                    Animator animVisible = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
                    animHide[0] = ViewAnimationUtils.createCircularReveal(view, cx, cy, finalRadius, 0);
                    animVisible.start();
                }
            });
        }
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    if ( animHide[0]!=null)
                    {
                        animHide[0].addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                alertDialog.dismiss();
                            }
                        });
                        animHide[0].start();
                    }
                    else
                    {
                        alertDialog.dismiss();
                    }
                }
                else
                {
                    alertDialog.dismiss();
                }
            }
        });
        alertDialog.setCancelable(false);
    }

对于API级别的类似动画,您有什么建议?我更新了我的答案,以在对话框中使用现有的API。