java Android:如何在任何事物(任何活动)之上自由绘制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16559642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to draw free on top of anything (any activity)
提问by Allahjane
How do you draw a view on top of all other activities regardless of what they are showing and without using transparent activities or consuming its touch events.
您如何在所有其他活动之上绘制视图,而不管它们显示什么并且不使用透明活动或消耗其触摸事件。
Its like to display a custom icon on the screen on top of all other apps that reacts when you touch it but you can still touch the other views on the screen.
就像在所有其他应用程序之上的屏幕上显示一个自定义图标,当您触摸它时会做出反应,但您仍然可以触摸屏幕上的其他视图。
Example: facebook chat heads that displays a dragable and clickable icon on screen regardless of what you are doing whether you are on home screen or app menus or any app. Still you can click the chat head icon and background app elements seperately
示例:无论您在主屏幕、应用程序菜单或任何应用程序上做什么,Facebook 聊天头都会在屏幕上显示可拖动和可点击的图标。仍然可以分别点击聊天头图标和后台应用元素
How to do anything like that?
如何做这样的事情?
回答by Fredszaq
Take a look at this cool article, I think that's exactly what you want :
看看这篇很酷的文章,我认为这正是你想要的:
http://www.piwai.info/chatheads-basics
http://www.piwai.info/chatheads-basics
In short : you want to add this permission to your manifest :
简而言之:您想将此权限添加到清单中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
For api >= 23, you'll need to request the runtime permission Settings.ACTION_MANAGE_OVERLAY_PERMISSION
对于api >= 23,您需要请求运行时权限Settings.ACTION_MANAGE_OVERLAY_PERMISSION
Then in a service, get the window manager
然后在一个服务中,获取窗口管理器
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
and add your views with the addView
method
并使用该addView
方法添加您的视图
回答by bogdan
Take a look at WindowManager
. You can do something like (WindowManager)mContext.getSystemService("window");
if you want to get the root view and use the method addView(view, params)
if you want to add a view.
看看WindowManager
。(WindowManager)mContext.getSystemService("window");
如果要获取根视图,可以执行一些操作,如果addView(view, params)
要添加视图,则可以使用该方法。