有什么便捷的方式实现activity变暗的效果

文章目录

  1. 1. 需求说明
  2. 2. 使用方法
  3. 3. 更多参考资料

需求说明

  • 不要新开启Activity的方式
  • 也不要使用Dialog
  • 让背景跟Dialog出现一样,变暗,带动画。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void dimBackground(final float from, final float to) {
final Window window = getWindow();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = (Float) animation.getAnimatedValue();
window.setAttributes(params);
}
});
valueAnimator.start();
}

使用方法

1
2
3
4
5
/** 窗口背景变暗*/
dimBackground(1.0f,0.5f);
/** 窗口背景变亮*/
dimBackground(0.5f,1.0f);

更多参考资料