Android custom overlay control floating window control

In the process of mobile application development, we may occasionally receive such requirements:

1. Create a window on the mobile desktop, which is similar to the suspension window of 360. Click this window to respond (as for window dragging, we can expand it later).

2. The self-developed application starts a non-native application B, and a guide window is added to an interface of application B.

3. Trigger the start of this window on the application page. The window is suspended on this page, but it will not affect other operations of the interface. That is, unlike popupwindow, either the window disappears or the page is unresponsive

The above requirements have several common features. 1. The hosting page of the window is not necessarily the activity page, that is, it is not a page like dialog and popupwindow. 2. The display of the window will not affect the user's operation of other interfaces.

According to the above characteristics, we find that this kind of window does not affect other interface operations. It is a bit like toast, but not completely, because toast disappears by itself. The interface can be constantly displayed, and it is a bit like popupwindow. It will disappear only when the disappearance method is called. So we can refer to the implementation of toast and popupwindow when making such controls. The most important time is toast. Well, with so many general ideas, we have understood.

Through the source code of toast and popupwindow, we find that toast and popup are implemented through the addview and removeview of WindowManager and by setting layoutparams. Therefore, the later design should start from here, no nonsense - to realize.

The first step is to design a class floatwindow similar to toast

You may notice

These settings are different, which is the key to realize that we can not only listen to eye-catching events outside the window, but also affect their own operations. At the same time, | windowmanager.layoutparams. Flag_ NOT_ FOCUSABLE ; And | windowmanager.layoutparams.flag_ NOT_ TOUCH_ MODAL; The key setting of whether the form listens to the return key needs to be pointed out that once the form listens to the return key event, the current activity will no longer listen to the return button event, so you can make a choice according to your actual situation.

In order to facilitate the management of the display and disappearance of these floating windows, a class floatwindowmanager for managing the display of windows is also written. This is a singleton mode, and only one display window is displayed. You can change it according to your needs. There are no details here.

There is also a window similar to a suspended ball. You can run it by yourself, which is more useful than watching it a thousand times here.

Attachment: Android floating window

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>