Skip to content

Commit 0c3f9bd

Browse files
committed
fix:修复计算逻辑,更新在小窗时的显示规则
1 parent eb4c9fd commit 0c3f9bd

8 files changed

Lines changed: 187 additions & 127 deletions

File tree

core/src/main/java/com/petterp/floatingx/FloatingX.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ import com.petterp.floatingx.listener.IFxControl
1818
*/
1919
@SuppressLint("StaticFieldLeak")
2020
object FloatingX {
21-
private var iFxAppLifecycle: FxLifecycleCallback? = null
2221
private var fxControl: IFxControl? = null
23-
private var helper: FxHelper? = null
24-
internal val topActivity: Activity
25-
get() = iFxAppLifecycle?.topActivity
26-
?: throw NullPointerException("topActivity == null !,Have you ever called FloatingX.init()?")
22+
internal var helper: FxHelper? = null
23+
internal var iFxAppLifecycle: FxLifecycleCallback? = null
2724

2825
/** dsl初始化 */
2926
fun init(obj: FxHelper.Builder.() -> Unit): FloatingX =

core/src/main/java/com/petterp/floatingx/config/FxHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FxHelper(
6363
private var rBorderMargin: Float = 0f
6464
private var bBorderMargin: Float = 0f
6565
private var context: Context? = null
66-
private var gravity: Direction = Direction.RIGHT_OR_BOTTOM
66+
private var gravity: Direction = Direction.LEFT_OR_TOP
6767
private var iFxScrollListener: IFxScrollListener? = null
6868
private var iFxViewLifecycle: IFxViewLifecycle? = null
6969
private var layoutParams: FrameLayout.LayoutParams? = null
@@ -186,7 +186,7 @@ class FxHelper(
186186
defaultX = marginEdgeTox + lBorderMargin
187187
}
188188
Direction.RIGHT_OR_BOTTOM -> {
189-
defaultY = -(marginEdgeToy + tBorderMargin)
189+
defaultY = -(marginEdgeToy + bBorderMargin)
190190
defaultX = -(marginEdgeTox + rBorderMargin)
191191
}
192192
Direction.RIGHT_OR_TOP, Direction.RIGHT_OR_CENTER -> {

core/src/main/java/com/petterp/floatingx/ext/BarExt.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.petterp.floatingx.ext
22

33
import android.app.Activity
4-
import android.graphics.Rect
5-
import android.view.View
6-
import android.view.ViewGroup
4+
import android.content.Context
75
import android.widget.FrameLayout
8-
import androidx.core.view.isVisible
6+
import com.petterp.floatingx.FloatingX
97

108
/**
119
* @Author petterp
@@ -22,70 +20,18 @@ internal inline fun <reified T : Any> lazyLoad(
2220
obj()
2321
}
2422

25-
internal fun ViewGroup.updateParams(left: Int, top: Int, end: Int, bottom: Int) {
26-
val parent = (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
27-
leftMargin = left
28-
topMargin = top
29-
marginEnd = end
30-
bottomMargin = bottom
31-
}
32-
layoutParams = parent
33-
}
34-
35-
internal fun View.show() {
36-
if (isVisible) return
37-
else isVisible = true
38-
}
23+
internal val topActivity: Activity?
24+
get() = FloatingX.iFxAppLifecycle?.topActivity
3925

40-
internal fun View.hide() {
41-
if (!isVisible) return
42-
else isVisible = false
43-
}
26+
internal val appContext: Context
27+
get() = FloatingX.helper?.context
28+
?: throw NullPointerException("appContext == null !,Did you initialize the context?")
4429

45-
internal val Activity.rootView: FrameLayout?
30+
internal val Activity.fxParentView: FrameLayout?
4631
get() = try {
4732
window.decorView as FrameLayout
4833
} catch (e: Exception) {
4934
e.printStackTrace()
5035
FxDebug.e("rootView -> Null")
5136
null
5237
}
53-
54-
fun isViewCovered(view: View): Boolean {
55-
var currentView = view
56-
val currentViewRect = Rect()
57-
val partVisible = currentView.getGlobalVisibleRect(currentViewRect)
58-
val totalHeightVisible: Boolean =
59-
currentViewRect.bottom - currentViewRect.top >= view.measuredHeight
60-
val totalWidthVisible: Boolean =
61-
currentViewRect.right - currentViewRect.left >= view.measuredWidth
62-
val totalViewVisible = partVisible && totalHeightVisible && totalWidthVisible
63-
// if any part of the view is clipped by any of its parents,return true
64-
if (!totalViewVisible) return true
65-
while (currentView.parent is ViewGroup) {
66-
val currentParent = currentView.parent as ViewGroup
67-
// if the parent of view is not visible,return true
68-
if (currentParent.visibility != View.VISIBLE) return true
69-
val start = indexOfViewInParent(currentView, currentParent)
70-
for (i in start + 1 until currentParent.childCount) {
71-
val viewRect = Rect()
72-
view.getGlobalVisibleRect(viewRect)
73-
val otherView = currentParent.getChildAt(i)
74-
val otherViewRect = Rect()
75-
otherView.getGlobalVisibleRect(otherViewRect)
76-
// if view intersects its older brother(covered),return true
77-
if (Rect.intersects(viewRect, otherViewRect)) return true
78-
}
79-
currentView = currentParent
80-
}
81-
return false
82-
}
83-
84-
private fun indexOfViewInParent(view: View, parent: ViewGroup): Int {
85-
var index = 0
86-
while (index < parent.childCount) {
87-
if (parent.getChildAt(index) === view) break
88-
index++
89-
}
90-
return index
91-
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.petterp.floatingx.ext
2+
3+
import android.app.Activity
4+
import android.content.res.Resources
5+
import android.graphics.Point
6+
import android.graphics.Rect
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import androidx.core.view.isVisible
10+
11+
/**
12+
* @Author petterp
13+
* @Date 2021/6/2-7:12 下午
14+
* @Email ShiyihuiCloud@163.com
15+
* @Function
16+
*/
17+
18+
val Activity.navigationBarHeight: Int
19+
get() {
20+
val display = windowManager.defaultDisplay
21+
val size = Point()
22+
val realSize = Point()
23+
display.getSize(size)
24+
display.getRealSize(realSize)
25+
val resourceId =
26+
resources.getIdentifier("navigation_bar_height", "dimen", "android")
27+
val height = resources.getDimensionPixelSize(resourceId)
28+
// 超出系统默认的导航栏高度以上,则认为存在虚拟导航
29+
return if (realSize.y - size.y > height - 10) {
30+
height
31+
} else 0
32+
}
33+
34+
internal fun ViewGroup.updateParams(left: Int, top: Int, end: Int, bottom: Int) {
35+
val parent = (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
36+
leftMargin = left
37+
topMargin = top
38+
marginEnd = end
39+
bottomMargin = bottom
40+
}
41+
layoutParams = parent
42+
}
43+
44+
internal fun View.show() {
45+
if (isVisible) return
46+
else isVisible = true
47+
}
48+
49+
internal fun View.hide() {
50+
if (!isVisible) return
51+
else isVisible = false
52+
}
53+
54+
class UiExt private constructor() {
55+
companion object {
56+
var statsBarHeightConfig: Int = getBarHeight()
57+
58+
var navigationBarHeightConfig: Int = 0
59+
60+
private fun getBarHeight(): Int {
61+
var height = 0
62+
val resourceId: Int =
63+
Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android")
64+
if (resourceId > 0) {
65+
height = Resources.getSystem()
66+
.getDimensionPixelSize(resourceId)
67+
}
68+
return height
69+
}
70+
71+
fun isViewCovered(view: View): Boolean {
72+
var currentView = view
73+
val currentViewRect = Rect()
74+
val partVisible = currentView.getGlobalVisibleRect(currentViewRect)
75+
val totalHeightVisible: Boolean =
76+
currentViewRect.bottom - currentViewRect.top >= view.measuredHeight
77+
val totalWidthVisible: Boolean =
78+
currentViewRect.right - currentViewRect.left >= view.measuredWidth
79+
val totalViewVisible = partVisible && totalHeightVisible && totalWidthVisible
80+
// if any part of the view is clipped by any of its parents,return true
81+
if (!totalViewVisible) return true
82+
while (currentView.parent is ViewGroup) {
83+
val currentParent = currentView.parent as ViewGroup
84+
// if the parent of view is not visible,return true
85+
if (currentParent.visibility != View.VISIBLE) return true
86+
val start = indexOfViewInParent(currentView, currentParent)
87+
for (i in start + 1 until currentParent.childCount) {
88+
val viewRect = Rect()
89+
view.getGlobalVisibleRect(viewRect)
90+
val otherView = currentParent.getChildAt(i)
91+
val otherViewRect = Rect()
92+
otherView.getGlobalVisibleRect(otherViewRect)
93+
// if view intersects its older brother(covered),return true
94+
if (Rect.intersects(viewRect, otherViewRect)) return true
95+
}
96+
currentView = currentParent
97+
}
98+
return false
99+
}
100+
101+
private fun indexOfViewInParent(view: View, parent: ViewGroup): Int {
102+
var index = 0
103+
while (index < parent.childCount) {
104+
if (parent.getChildAt(index) === view) break
105+
index++
106+
}
107+
return index
108+
}
109+
}
110+
}

core/src/main/java/com/petterp/floatingx/impl/FxControlImpl.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import androidx.annotation.DrawableRes
99
import androidx.core.view.OnApplyWindowInsetsListener
1010
import androidx.core.view.ViewCompat
1111
import androidx.core.view.isVisible
12-
import com.petterp.floatingx.FloatingX
1312
import com.petterp.floatingx.config.FxHelper
14-
import com.petterp.floatingx.ext.BarExt
1513
import com.petterp.floatingx.ext.FxDebug
14+
import com.petterp.floatingx.ext.UiExt
15+
import com.petterp.floatingx.ext.fxParentView
1616
import com.petterp.floatingx.ext.hide
17-
import com.petterp.floatingx.ext.rootView
1817
import com.petterp.floatingx.ext.show
19-
import com.petterp.floatingx.listener.FxLifecycleCallback
18+
import com.petterp.floatingx.ext.topActivity
2019
import com.petterp.floatingx.listener.IFxControl
2120
import com.petterp.floatingx.view.FxMagnetView
2221
import com.petterp.floatingx.view.FxViewHolder
@@ -33,7 +32,6 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
3332
private var managerView: FxMagnetView? = null
3433
private var viewHolder: FxViewHolder? = null
3534
private var mContainer: WeakReference<FrameLayout>? = null
36-
internal var fxLifecycleCallback: FxLifecycleCallback? = null
3735
private val managerViewOrContainerIsNull: Boolean
3836
get() = mContainer == null && managerView == null
3937

@@ -74,7 +72,7 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
7472
}
7573

7674
override fun attach(activity: Activity) {
77-
activity.rootView?.let {
75+
activity.fxParentView?.let {
7876
attach(it)
7977
}
8078
}
@@ -87,7 +85,9 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
8785
if (managerView == null) {
8886
initManagerView()
8987
} else {
90-
(managerView?.parent as? ViewGroup)?.removeView(managerView)
88+
mContainer?.get()?.removeView(managerView)
89+
mContainer?.clear()
90+
mContainer = null
9191
}
9292
FxDebug.d("view-lifecycle-> addView")
9393
mContainer = WeakReference(container)
@@ -97,7 +97,7 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
9797
/** 删除view */
9898
override fun detach(activity: Activity) {
9999
if (managerViewOrContainerIsNull) return
100-
activity.rootView?.let {
100+
activity.fxParentView?.let {
101101
detach(it)
102102
}
103103
}
@@ -107,7 +107,10 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
107107
FxDebug.d("view-lifecycle-> removeView")
108108
container.removeView(managerView)
109109
}
110-
mContainer = null
110+
if (container === mContainer?.get()) {
111+
mContainer?.clear()
112+
mContainer = null
113+
}
111114
}
112115

113116
override fun setClickListener(obj: (View) -> Unit) {
@@ -124,9 +127,9 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
124127
}
125128

126129
private fun showInit() {
127-
if (getContainer() == null) {
130+
if (getContainer() == null && topActivity != null) {
128131
// 这里的异常还是要抛出去
129-
attach(FloatingX.topActivity)
132+
attach(topActivity!!)
130133
return
131134
}
132135
if (managerView == null) initManagerView()
@@ -149,8 +152,8 @@ open class FxControlImpl(private val helper: FxHelper) : IFxControl {
149152
@SuppressLint("WrongConstant")
150153
val windowsInsetsListener: OnApplyWindowInsetsListener =
151154
OnApplyWindowInsetsListener { _, insets ->
152-
FxDebug.v("System--StatusBar---old-(${BarExt.realStatusBarHeight}),new-(${insets.systemWindowInsetTop})")
153-
BarExt.realStatusBarHeight = insets.systemWindowInsetTop
155+
FxDebug.v("System--StatusBar---old-(${UiExt.statsBarHeightConfig}),new-(${insets.systemWindowInsetTop})")
156+
UiExt.statsBarHeightConfig = insets.systemWindowInsetTop
154157
insets
155158
}
156159

0 commit comments

Comments
 (0)