白日梦与透明的背景



我正在尝试实现一个透明背景的白日梦服务。

我写了以下代码:

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    setContentView(R.layout.mydream);
    getWindow().setBackgroundDrawable(new ColorDrawable(0));
    .
    .
    .
}

但是当我启动白日梦时,背景只有1秒是透明的。然后变成黑色背景。

有谁能帮我一下吗?

这个问题可能有你想要的答案:如何在Android上创建透明活动?

你可以创建一个Transparent主题,并将其应用到你的activity中。

经过很长时间的调查发现了一个解决方案

您可以访问window.decorView布局参数并将调光设置为0f它会使你的屏保窗口透明,

这是我的代码

 (window.attributes).apply {
     dimAmount = 0f
     format = PixelFormat.RGBA_8888 // I also suggest to set color format PixelFormat.RGBA_8888 and bellow all this code set window.decorView.setBackgroundColor(Color.TRANSPARENT)
     windowManager.updateViewLayout(window.decorView, this)
  }
window.decorView.setBackgroundColor(Color.TRANSPARENT)

幸运的是,您可以访问DreamService的窗口。所以你可以在你的DreamService类中做的是:

 @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        setInteractive(true);
        setContentView(R.layout.dream_service);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00FFFFFF")));
    }

确保你的DreamService布局有一个透明的背景;-)