實現activity變暗的效果

有什麼便捷的方式實現activity變暗的效果

  • 不要新開啓Activity的方式
  • 也不要使用Dialog
  • 讓背景跟Dialog出現一樣,變暗,帶動畫。
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(1.0f,0.5f);

    /** 窗口背景變亮*/
    dimBackground(0.5f,1.0f);

更多參考資料

發佈了124 篇原創文章 · 獲贊 114 · 訪問量 226萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章