Skip to content

Commit

Permalink
perf:优化浮窗半隐藏逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Petterpx committed Jul 21, 2024
1 parent 1ea7b42 commit a490d4c
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 64 deletions.
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".test.ImmersedActivity"
Expand All @@ -39,11 +43,7 @@
android:name=".test.SimpleRvActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/com/petterp/floatingx/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class MainActivity : AppCompatActivity() {
}
}.show()
}
addItemView("开启半贴边"){
FloatingX.control(MultipleFxActivity.TAG_1).configControl.setEnableHalfHide(true)
}
addItemView("隐藏半贴边"){
FloatingX.control(MultipleFxActivity.TAG_1).configControl.setEnableHalfHide(false)
}
addItemView("隐藏全局悬浮窗") {
FloatingX.control(MultipleFxActivity.TAG_1).hide()
}
Expand All @@ -62,7 +68,6 @@ class MainActivity : AppCompatActivity() {
this.updateViewContent {
it.setText(R.id.tvItemFx, "App")
}
gravity=
}.show()
}
addItemView("更新当前[全局浮窗]内容-(传递view方式)") {
Expand Down Expand Up @@ -101,14 +106,6 @@ class MainActivity : AppCompatActivity() {
addItemView("进入system浮窗测试页面") {
SystemActivity::class.java.start(this@MainActivity)
}

var halfHide = false
addItemView("切换半隐状态") {
FloatingX.control(MultipleFxActivity.TAG_1).apply {
halfHide = !halfHide
configControl.setEnableHalfHide(halfHide)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object FxAppSimple {
setTag(MultipleFxActivity.TAG_2)
setEnableLog(true)
setEdgeOffset(20f)
setEnableEdgeAdsorption(false)
setEnableEdgeAdsorption(true)
}.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ object FxComposeSimple {
FloatingX.install {
setContext(context)
setTag("compose")
//system浮窗必须调用此方法,才可以启用Compose支持
enableComposeSupport()
setScopeType(FxScopeType.SYSTEM)
setGravity(FxGravity.RIGHT_OR_BOTTOM)
setOffsetXY(10f, 10f)
Expand All @@ -49,7 +51,6 @@ object FxComposeSimple {
}
}
)
enableComposeSupport()
setEnableLog(true)
setEdgeOffset(20f)
setEnableEdgeAdsorption(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class SimpleRvActivity : AppCompatActivity() {
setLayoutView(createRvView(applicationContext))
setEnableLog(true)
setTouchListener(object : IFxTouchListener {
override fun onDown() {
super.onDown()
}
override fun onInterceptTouchEvent(
event: MotionEvent,
control: IFxInternalHelper?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.petterp.floatingx.assist
* FxView 边界配置
* @author petterp
*/
class FxBoundaryConfig(
data class FxBoundaryConfig(
var minW: Float = 0f,
var maxW: Float = 0f,
var minH: Float = 0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ class FxAppHelper(
private var whiteInsertList: MutableList<Class<*>> = mutableListOf()
private var blackFilterList: MutableList<Class<*>> = mutableListOf()

/** 用于启用全局浮窗标志,与control.show()同理
*
* 浮窗install后,直接使用控制器调用 show() 即可
* */
@Deprecated("目前control.show()支持了懒加载,所以这个方法已经不再需要")
fun enableFx(): Builder {
this.enableFx = true
return this
}

/**
* 设置context
* @param context context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import com.petterp.floatingx.assist.FxAnimation
import com.petterp.floatingx.assist.FxDisplayMode
import com.petterp.floatingx.assist.helper.FxBasisHelper
import com.petterp.floatingx.listener.IFxConfigStorage
import com.petterp.floatingx.listener.IFxScrollListener
import com.petterp.floatingx.listener.IFxTouchListener
import com.petterp.floatingx.listener.IFxViewLifecycle
import com.petterp.floatingx.listener.control.IFxConfigControl
import com.petterp.floatingx.listener.provider.IFxPlatformProvider
import com.petterp.floatingx.util.FX_HALF_PERCENT_MAX
import com.petterp.floatingx.util.FX_HALF_PERCENT_MIN
import com.petterp.floatingx.util.coerceInFx
import com.petterp.floatingx.view.FxBasicContainerView
import com.petterp.floatingx.view.IFxInternalHelper

/**
* Fx基础配置更改 提供者
* @author petterp
*/
class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
private val helper: F,
private val p: P?
open class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
val helper: F,
val p: P?
) : IFxConfigControl {

private val internalView: IFxInternalHelper?
Expand All @@ -27,7 +31,7 @@ class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
helper.enableClickListener = isEnable
}

override fun setEnableAnimation(isEnable: Boolean, animationImpl: FxAnimation) {
override fun setEnableAnimation(isEnable: Boolean, animationImpl: FxAnimation?) {
helper.enableAnimation = isEnable
helper.fxAnimation = animationImpl
}
Expand All @@ -51,6 +55,10 @@ class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
internalView?.moveToEdge()
}

override fun setTouchListener(listener: IFxTouchListener) {
helper.iFxTouchListener = listener
}

override fun setEdgeAdsorbDirection(direction: FxAdsorbDirection) {
helper.adsorbDirection = direction
internalView?.moveToEdge()
Expand All @@ -68,13 +76,18 @@ class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(

override fun setEnableHalfHide(isEnable: Boolean) {
helper.enableHalfHide = isEnable
(internalView as? FxBasicContainerView)?.locationHelper?.updateMoveBoundary()
internalView?.moveToEdge()
}

override fun setScrollListener(listener: IFxScrollListener) {
helper.iFxTouchListener = listener
override fun setEnableHalfHide(isEnable: Boolean, percent: Float) {
helper.enableHalfHide = isEnable
helper.halfHidePercent = percent.coerceInFx(FX_HALF_PERCENT_MIN, FX_HALF_PERCENT_MAX)
(internalView as? FxBasicContainerView)?.locationHelper?.updateMoveBoundary()
internalView?.moveToEdge()
}

@Deprecated("use addViewLifecycle", replaceWith = ReplaceWith("addViewLifecycleListener"))
override fun setViewLifecycleListener(listener: IFxViewLifecycle) {
helper.iFxViewLifecycles[0] = listener
}
Expand All @@ -96,12 +109,6 @@ class FxBasicConfigProvider<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
helper.iFxConfigStorage?.clear()
}

@Deprecated("已废弃,建议使用[setDisplayMode()]")
override fun setEnableTouch(isEnable: Boolean) {
val mode = if (isEnable) FxDisplayMode.Normal else FxDisplayMode.ClickOnly
setDisplayMode(mode)
}

override fun setDisplayMode(mode: FxDisplayMode) {
helper.displayMode = mode
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.petterp.floatingx.imp.system

import com.petterp.floatingx.assist.helper.FxAppHelper
import com.petterp.floatingx.imp.FxBasicConfigProvider

/**
* Fx系统浮窗ConfigProvider
* @author petterp
*/
class FxSystemConfigProvider(
helper: FxAppHelper,
platformProvider: FxSystemPlatformProvider?
) : FxBasicConfigProvider<FxAppHelper, FxSystemPlatformProvider>(helper, platformProvider) {
override fun setEnableHalfHide(isEnable: Boolean) {
if (helper.enableHalfHide != isEnable) p?.internalView?.updateFlags(isEnable)
super.setEnableHalfHide(isEnable)
}

override fun setEnableHalfHide(isEnable: Boolean, percent: Float) {
if (helper.enableHalfHide != isEnable) p?.internalView?.updateFlags(isEnable)
super.setEnableHalfHide(isEnable, percent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import com.petterp.floatingx.assist.FxScopeType
import com.petterp.floatingx.assist.helper.FxAppHelper
import com.petterp.floatingx.imp.FxBasisControlImp
import com.petterp.floatingx.listener.control.IFxAppControl
import com.petterp.floatingx.listener.control.IFxConfigControl

/**
*
* FxSystemControl
* @author petterp
*/
class FxSystemControlImp(helper: FxAppHelper) :
FxBasisControlImp<FxAppHelper, FxSystemPlatformProvider>(helper), IFxAppControl {

override fun createPlatformProvider(f: FxAppHelper) = FxSystemPlatformProvider(helper, this)

override fun createConfigProvider(
f: FxAppHelper,
p: FxSystemPlatformProvider
) = FxSystemConfigProvider(f, p)

override fun getBindActivity(): Activity? {
return null
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.petterp.floatingx.listener.control
import com.petterp.floatingx.assist.FxAnimation
import com.petterp.floatingx.assist.FxDisplayMode
import com.petterp.floatingx.listener.IFxConfigStorage
import com.petterp.floatingx.listener.IFxScrollListener
import com.petterp.floatingx.listener.IFxViewLifecycle
import com.petterp.floatingx.assist.FxAdsorbDirection
import com.petterp.floatingx.listener.IFxTouchListener

/**
* 配置更改接口,使用此接口运行时更改配置层
Expand All @@ -17,7 +17,7 @@ interface IFxConfigControl {
* @param isEnable 是否启用
* @param animationImpl 具体实现实例
* */
fun setEnableAnimation(isEnable: Boolean, animationImpl: FxAnimation)
fun setEnableAnimation(isEnable: Boolean, animationImpl: FxAnimation? = null)

/** 是否启用动画 */
fun setEnableAnimation(isEnable: Boolean)
Expand All @@ -38,15 +38,16 @@ interface IFxConfigControl {
* */
fun setEnableEdgeRebound(isEnable: Boolean)

/** 设置是否支持悬浮窗半隐模式
/** 设置是否支启用悬浮窗半隐藏模式
* */
fun setEnableHalfHide(isEnable: Boolean)

/** 设置是否启用触摸事件
* @param isEnable true,则允许悬浮窗拖动
/**
* 设置悬浮窗半隐藏模式的隐藏比例
* @param isEnable 是否启用
* @param percent 半隐比例
* */
@Deprecated("已废弃,建议使用[setDisplayMode()]")
fun setEnableTouch(isEnable: Boolean)
fun setEnableHalfHide(isEnable: Boolean, percent: Float = 0.5f)

/**
* 设置浮窗展示模式
Expand All @@ -64,10 +65,13 @@ interface IFxConfigControl {
fun setEnableEdgeAdsorption(isEnable: Boolean)

/** 设置滑动监听 */
fun setScrollListener(listener: IFxScrollListener)
fun setTouchListener(listener: IFxTouchListener)

/** 设置view-lifecycle监听 */
@Deprecated(replaceWith = ReplaceWith("addViewLifecycleListener"), message = "use addViewLifecycle")
@Deprecated(
replaceWith = ReplaceWith("addViewLifecycleListener"),
message = "use addViewLifecycle"
)
fun setViewLifecycleListener(listener: IFxViewLifecycle)

fun addViewLifecycleListener(listener: IFxViewLifecycle)
Expand Down
2 changes: 2 additions & 0 deletions floatingx/src/main/java/com/petterp/floatingx/util/FxExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ internal const val FX_INSTALL_SCOPE_SYSTEM_TAG = "system"
internal const val FX_INSTALL_SCOPE_ACTIVITY_TAG = "activity"
internal const val FX_INSTALL_SCOPE_FRAGMENT_TAG = "fragment"
internal const val FX_INSTALL_SCOPE_VIEW_GROUP_TAG = "view"
internal const val FX_HALF_PERCENT_MIN = 0F
internal const val FX_HALF_PERCENT_MAX = 1F

internal val topActivity: Activity?
get() = FxAppLifecycleProvider.getTopActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class FxBasicContainerView @JvmOverloads constructor(
private var _viewHolder: FxViewHolder? = null
private val touchHelper = FxViewTouchHelper()
private val animateHelper = FxViewAnimationHelper()
private val locationHelper = FxViewLocationHelper()
internal val locationHelper = FxViewLocationHelper()
private val helpers = listOf(locationHelper, touchHelper, animateHelper)

abstract fun currentX(): Float
Expand All @@ -52,9 +52,6 @@ abstract class FxBasicContainerView @JvmOverloads constructor(
visibility = View.INVISIBLE
}

/**
* 修改内容:添加半隐藏的逻辑处理
*/
override fun moveToEdge() {
val (x, y) = locationHelper.getDefaultEdgeXY() ?: return
moveLocation(x, y, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,27 @@ class FxSystemContainerView @JvmOverloads constructor(
return helper.context.screenWidth to helper.context.screenHeight
}

internal fun updateFlags(enableHalfHide: Boolean) {
wl.flags = findFlags(enableHalfHide)
wm.updateViewLayout(this, wl)
}

private fun findFlags(enableHalfHide: Boolean): Int {
var flags = (WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
if (enableHalfHide) {
flags = flags or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
}
return flags
}

private fun initWLParams() {
wl = WindowManager.LayoutParams().apply {
width = helper.layoutParams?.width ?: WindowManager.LayoutParams.WRAP_CONTENT
height = helper.layoutParams?.height ?: WindowManager.LayoutParams.WRAP_CONTENT
format = PixelFormat.RGBA_8888
gravity = Gravity.TOP or Gravity.START
flags = (WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
flags = findFlags(helper.enableHalfHide)
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
Expand Down
Loading

0 comments on commit a490d4c

Please sign in to comment.