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

效果图:

Android 简单实现Activity界面变暗效果_第1张图片

代码实现如下:

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();
    }

 变亮

dimBackground(0.5f,1.0f);

 变暗

  dimBackground(1.0f,0.5f);

你可能感兴趣的:(【Android开发进阶】)