FloatingX, a powerful perm-free levitating window component, supports both global and local levitating Windows.

background

In the previous months of business development, our app needs to make changes to the suspension window. There are two common ways to implement the suspension window:

  • The former is obtained after the use of permissionsWindowManagerimplementation
  • The latter is based onDecorViewImplementation of the insert suspension window

The choice of scheme mainly depends on the orientation of the business, because our business is not video call class, but recipe tool class, so we do not want to let users to set the permission, this is a costly thing, so we adopted the latter scheme, which is also used by zhihu industry.

In the previous version, we have adopted the latter solution. The former students used to insert the implementation into content, but without too much encapsulation, the code was directly inserted into the base layer. For now, it is basically suitable for use, but the extension cost is too high, and for the future, it obviously does not meet the requirements.

I wanted to have a component that had the following basic requirements:

  • Non-inductive insert
  • Can drag
  • Good layering design

That was the initial feature, but as I went through the encapsulation process, and compared it to other floating Windows like Zhihu, I realized that I might be able to make this something more interesting than just a utility class.

After more than a dozen iterations of optimization and testing, FloatingX is finally ready for its first RC release. The API is relatively fixed at 👏 and has been running online for three months +(beta start), with nearly a hundred models on the market being tested.

features

FloatingX provides the following functions:

  • The singleton holds the float window view
  • Support for callback listening
  • Chain call, no inductive insertion
  • Support to customize whether to save the historical location and restore
  • Support the insertViewGroup , Fragment , Activity
  • Allow custom hover window indicators, custom hidden display animation
  • Support bounding back, touch, small screen adaptation, screen rotation
  • Support custom position direction, with auxiliary positioning display coordinates
  • Improved Kotlin build extensions and Java friendly compatibility
  • Support display position [forced repair], for special models (need to be opened separately)
  • Perfect log system, open to see different levels of Fx running process, more conducive to finding problems
  • .

rendering

Full screen,activity,fragment, single view Small screen display The screen is scaled abnormally
Screen rotation Function demonstration

How to use

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io'}}}Copy the code
dependencies {
	    implementation 'com. Making. Petterpx: FloatingX: 1.0 rc01'
}
Copy the code

Perfect log – viewer

Turn on the log viewer and you will see the entire path of Fx, making it easier to find problems and trace solutions. Custom log tags are also supported

App Activity ViewGroup

Global floating window management

kt

FloatingX.init {
        setContext(this@CustomApplication)
        setLayout(R.layout.item_floating_new)
  			addBlackClass(
                MainActivity::class.java,
                NewActivity::class.java,
                ImmersedActivity::class.java
         )
  			// Only when show is called will app-lifecycle be monitored and will be automatically inserted into the activity
        show()
}
Copy the code

Java

AppHelper helper = AppHelper.builder()
        .setContext(application)
        .setLayout(R.layout.item_floating)
        .build();
FloatingX.init(helper);
Copy the code

Local suspension window management

Generic creation mode

kt

ScopeHelper.builder {
  setLayout(R.layout.item_floating)
}.toControl(activity)
Copy the code

kt && java

ScopeHelper.builder()
            .setLayout(R.layout.item_floating)
            .build()
            .toControl(activity)
            .toControl(fragment)
            .toControl(viewgroup)
Copy the code

Extended support for KT

Activity Creates a floating window
private val activityFx by activityToFx(activity) {
    setLayout(R.layout.item_floating)
}
Copy the code
Fragment Creates a floating window
private val fragment by fragmentToFx(fragment) {
    setLayout(R.layout.item_floating)
}
Copy the code
ViewGroup creates a floating window
private val viewFx by createFx({
        init(viewGroup)
    }) {
        setLayout(R.layout.item_floating)
        setEnableLog(true."main_fx")}Copy the code
Quickly create levitating Windows of any scope
private val customCreateFx by createFx {
    setLayout(R.layout.item_floating)
    build().toControl(activity)
    build().toControl(fragment)
    build().toControl(viewgroup)
}
Copy the code

The technical implementation

App-level hover window is based on DecorView. It holds a separate hover window View globally that listens to the Activity lifecycle through AppLifecycle and is inserted into the DecorView at the appropriate time.

View-level hover window, based on the given ViewGroup;

Fragment level, based on its corresponding rootView;

Acrtivity level, based on THE R.I.D.C tent inside the DecorView;

The details are as follows:

See my blog: source code analysis | Activity – the setContentView

Why should the App level hover window be inserted into the DecorView instead of r.I. d.c. tent -> FrameLayout?

Inserting into the DecorView gives you maximum control over the degree of freedom of the hover window, meaning that the hover window can truly be dragged [full-screen].

When inserted into content, the drag range is actually the range of the application view, that is, the placement is influenced by the status bar, the bottom navigation bar and the default AppBar. For example, when the user hides the status bar or navigation bar, the corresponding view size will change, which will affect the position of the floating window.

Thank you

The basic Floating window View is derived from the FloatingMagnetView implementation of EnFloatingView, with some improvements added.

For the navigation bar measurement part of the code from wenlu@nuggets, and on it to add more adaptions, has covered most models in the market.