转:Android 简单实现Activity界面变暗效果

转自:https://blog.csdn.net/millerkevin/article/details/76686962

代码实现如下:

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
6
7
8
9
10
11
12
13
14
15
 变亮

dimBackground(0.5f,1.0f);
1
 变暗

  dimBackground(1.0f,0.5f);

猜你喜欢

转载自blog.csdn.net/Rodulf/article/details/86533036