📜  alertdialog 颤动的圆角 (1)

📅  最后修改于: 2023-12-03 15:13:17.946000             🧑  作者: Mango

介绍AlertDialog颤动的圆角

AlertDialog 是一个经常用到的对话框,在很多应用中用于显示一些提示信息或者提醒用户做出一些选择。如果需要增加一些动态效果来吸引用户的注意力,可以考虑使用 AlertDialog 颤动的圆角。

什么是AlertDialog颤动的圆角?

AlertDialog 颤动的圆角是一种让AlertDialog在显示时变得更加动态的效果。在 AlertDialog 显示时,圆角会发生颤动的效果并且有一个缓慢的停止过程。这个效果能够在用户注意力被分散时吸引他们的注意力。

如何实现?

这个效果可以通过使用 CircularRevealViewPropertyAnimator 这两个类和方法来实现。

在AlertDialog显示时,我们需要创建一个 Drawable 对象,它要设置边框、内部颜色和圆角半径。然后我们可以使用 CircularReveal 来对AlertDialog进行圆形揭示,从而展示AlertDialog。当揭示完成之后,我们使用 ViewPropertyAnimator 来使 AlertDialog 的圆角颤动。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("颤动的圆角");
builder.setMessage("这是一个颤动圆角的AlertDialog");

// 创建Drawable对象,设置颜色和圆角半径
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
drawable.setCornerRadius(getResources().getDimensionPixelOffset(R.dimen.dialog_corner_radius));

// 将Drawable设置给AlertDialog
AlertDialog alertDialog = builder.create();
alertDialog.show();

Window window = alertDialog.getWindow();
window.setBackgroundDrawable(drawable);

// 使用CircularReveal展示AlertDialog
View view = window.getDecorView().findViewById(android.R.id.content);
int centerX = (view.getLeft() + view.getRight()) / 2;
int centerY = (view.getTop() + view.getBottom()) / 2;
float radius = (float) Math.hypot(view.getWidth(), view.getHeight());
Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0, radius);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(1000);
animator.start();

// 使用ViewPropertyAnimator使AlertDialog的圆角颤动
int shakeRadius = getResources().getDimensionPixelOffset(R.dimen.dialog_shake_radius);
int shakeDuration = getResources().getInteger(R.integer.dialog_shake_duration);
int shakeRepeatCount = getResources().getInteger(R.integer.dialog_shake_repeat_count);

ObjectAnimator shakeAnimatorX = ObjectAnimator.ofFloat(view, "translationX", 0, shakeRadius, 0, -shakeRadius, 0);
shakeAnimatorX.setDuration(shakeDuration);
shakeAnimatorX.setRepeatCount(shakeRepeatCount);
ObjectAnimator shakeAnimatorY = ObjectAnimator.ofFloat(view, "translationY", 0, shakeRadius, 0, -shakeRadius, 0);
shakeAnimatorY.setDuration(shakeDuration);
shakeAnimatorY.setRepeatCount(shakeRepeatCount);

AnimatorSet shakeAnimatorSet = new AnimatorSet();
shakeAnimatorSet.playTogether(shakeAnimatorX, shakeAnimatorY);
shakeAnimatorSet.start();
总结

AlertDialog 颤动的圆角是一种可以增加AlertDialog动态效果的方法。使用 CircularReveal 和 ViewPropertyAnimator 可以轻松地实现这个效果,从而提高应用的用户体验。