android开发: 我的小米手机播放视频时下拉状态栏发现状态栏是半透明的 可以迷糊地看到电影画面吗 看起来像是实时的高斯模糊 这个效果怎么做出来的 我的APP有个悬浮窗 我想给悬浮窗也加上这样的效果 有示例代码吗
要给悬浮窗加上类似的效果,可以通过以下步骤实现:
-
创建一个自定义的悬浮窗布局文件,例如
floating_window.xml。 -
在布局文件中添加一个
FrameLayout作为根布局,用于显示悬浮窗内容。 -
使用
FrameLayout的foreground属性设置半透明的背景,可以使用颜色值或者透明度。<FrameLayout ... android:foreground="@android:color/transparent" android:foregroundAlpha="0.5"> <!-- 悬浮窗内容 --> </FrameLayout> -
将悬浮窗布局文件加载到悬浮窗服务中。
// 创建悬浮窗布局参数 WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; // 加载悬浮窗布局文件 LayoutInflater inflater = LayoutInflater.from(context); View floatingWindow = inflater.inflate(R.layout.floating_window, null); // 添加悬浮窗到窗口 WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); windowManager.addView(floatingWindow, params);
这样就可以给悬浮窗添加类似半透明效果了。根据自己的需求,可以调整foregroundAlpha的值来控制背景的透明度。
希望对你有所帮助
原文地址: https://www.cveoy.top/t/topic/iUvj 著作权归作者所有。请勿转载和采集!