将圆形显示动画添加到BottomSheet

时间:2017-04-27 07:33:02

标签: android animation bottom-sheet circularreveal

我在Android应用中使用了Bottomsheet。 如何添加圆形显示动画以加载底片? createCircularReveal是一个android sdk函数,支持api级别高于21的所有设备。

1 个答案:

答案 0 :(得分:0)

将常规的上滑动画转换为在21以上的API上滑动动画的简单逻辑:

1)制作视图的动画代码。

2)获取对话框的父视图,并在 .show()方法之前将其背景更改为透明

((View) bsdCreateNewBinding.getRoot().getParent()).setBackgroundColor(Color.TRANSPARENT);

3)在此方法中应用揭示动画:

bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                revealShow(bsdCreateNewBinding.getRoot(), true, null);
            }
        });

这是揭示动画代码:

private void revealShow(View dialogView, boolean b, final BottomSheetDialog dialog) {

        final View view = dialogView.findViewById(R.id.dialog);

        int w = view.getWidth();
        int h = view.getHeight();

        int endRadius = (int) Math.hypot(w, h);

        int cx = (int) (binding.btnAdd.getX() + (binding.btnAdd.getWidth() / 2));
        int cy = (int) (binding.btnAdd.getY()) + binding.btnAdd.getHeight() + 56;


        if (b) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Animator revealAnimator = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, endRadius);
                view.setVisibility(View.VISIBLE);
                revealAnimator.setDuration(700);
                revealAnimator.start();
            } else {
                view.setVisibility(View.VISIBLE);
            }
        } else {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Animator anim =
                        ViewAnimationUtils.createCircularReveal(view, cx, cy, endRadius, 0);

                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        dialog.dismiss();
                        view.setVisibility(View.INVISIBLE);

                    }
                });
                anim.setDuration(700);
                anim.start();
            } else {
                dialog.dismiss();
                view.setVisibility(View.INVISIBLE);
            }

        }

    }

P.S:处理Lolipop案件的上方和下方。

以下是带有显示动画的Dialog或BottomSheetDialog的完美示例:https://android.jlelse.eu/custom-dialog-with-circular-reveal-animation-ef7dc77ba1e