修复Android 11 适配问题
This commit is contained in:
@@ -70,79 +70,6 @@ internal class WarningFloatWindowHelper(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置浮窗的对齐方式,支持上下左右、居中、上中、下中、左中和右中,默认左上角
|
||||
* 支持手动设置的偏移量
|
||||
*/
|
||||
@SuppressLint("RtlHardcoded")
|
||||
private fun setGravity(view: View?) {
|
||||
if (config.locationPair != Pair(0, 0) || view == null) return
|
||||
val parentRect = Rect()
|
||||
// 获取浮窗所在的矩形
|
||||
windowManager.defaultDisplay.getRectSize(parentRect)
|
||||
val location = IntArray(2)
|
||||
// 获取在整个屏幕内的绝对坐标
|
||||
view.getLocationOnScreen(location)
|
||||
// 通过绝对高度和相对高度比较,判断包含顶部状态栏
|
||||
val statusBarHeight =
|
||||
if (location[1] > params.y) WindowUtils.getStatusBarHeight(view.context.applicationContext) else 0
|
||||
val parentBottom =
|
||||
WindowUtils.getScreenHeight(view.context.applicationContext) - statusBarHeight
|
||||
when (config.gravity) {
|
||||
// 右上
|
||||
Gravity.END, Gravity.END or Gravity.TOP, Gravity.RIGHT, Gravity.RIGHT or Gravity.TOP ->
|
||||
params.x = parentRect.right - view.width
|
||||
// 左下
|
||||
Gravity.START or Gravity.BOTTOM, Gravity.BOTTOM, Gravity.LEFT or Gravity.BOTTOM ->
|
||||
params.y = parentBottom - view.height
|
||||
// 右下
|
||||
Gravity.END or Gravity.BOTTOM, Gravity.RIGHT or Gravity.BOTTOM -> {
|
||||
params.x = parentRect.right - view.width
|
||||
params.y = parentBottom - view.height
|
||||
}
|
||||
// 居中
|
||||
Gravity.CENTER -> {
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
}
|
||||
// 上中
|
||||
Gravity.CENTER_HORIZONTAL, Gravity.TOP or Gravity.CENTER_HORIZONTAL ->
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
// 下中
|
||||
Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL -> {
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
params.y = parentBottom - view.height
|
||||
}
|
||||
// 左中
|
||||
Gravity.CENTER_VERTICAL, Gravity.START or Gravity.CENTER_VERTICAL, Gravity.LEFT or Gravity.CENTER_VERTICAL ->
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
// 右中
|
||||
Gravity.END or Gravity.CENTER_VERTICAL, Gravity.RIGHT or Gravity.CENTER_VERTICAL -> {
|
||||
params.x = parentRect.right - view.width
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
}
|
||||
// 其他情况,均视为左上
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置偏移量
|
||||
params.x += config.offsetPair.first
|
||||
params.y += config.offsetPair.second
|
||||
|
||||
if (config.immersionStatusBar) {
|
||||
if (config.showPattern != ShowPattern.CURRENT_ACTIVITY) {
|
||||
params.y -= statusBarHeight
|
||||
}
|
||||
} else {
|
||||
if (config.showPattern == ShowPattern.CURRENT_ACTIVITY) {
|
||||
params.y += statusBarHeight
|
||||
}
|
||||
}
|
||||
// 更新浮窗位置信息
|
||||
windowManager.updateViewLayout(view, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加视图
|
||||
*/
|
||||
@@ -162,12 +89,14 @@ internal class WarningFloatWindowHelper(
|
||||
frameLayout?.layoutListener = object : ParentFrameLayout.OnLayoutListener {
|
||||
override fun onLayout() {
|
||||
setGravity(frameLayout)
|
||||
config.apply {
|
||||
enterAnim(floatingView)
|
||||
// 设置callbacks
|
||||
layoutView = floatingView
|
||||
callbacks?.createdResult(true, null, floatingView)
|
||||
}
|
||||
frameLayout?.postDelayed({
|
||||
config.apply {
|
||||
enterAnim(floatingView)
|
||||
// 设置callbacks
|
||||
layoutView = floatingView
|
||||
callbacks?.createdResult(true, null, floatingView)
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,4 +192,79 @@ internal class WarningFloatWindowHelper(
|
||||
Logger.e(TAG, "浮窗关闭出现异常:$e")
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置浮窗的对齐方式,支持上下左右、居中、上中、下中、左中和右中,默认左上角
|
||||
* 支持手动设置的偏移量
|
||||
*/
|
||||
@SuppressLint("RtlHardcoded")
|
||||
private fun setGravity(view: View?) {
|
||||
if (config.locationPair != Pair(0, 0) || view == null) return
|
||||
val parentRect = Rect()
|
||||
// 获取浮窗所在的矩形
|
||||
windowManager.defaultDisplay.getRectSize(parentRect)
|
||||
val location = IntArray(2)
|
||||
// 获取在整个屏幕内的绝对坐标
|
||||
view.getLocationOnScreen(location)
|
||||
// 通过绝对高度和相对高度比较,判断包含顶部状态栏
|
||||
val statusBarHeight =
|
||||
if (location[1] > params.y) WindowUtils.getStatusBarHeight(view.context.applicationContext) else 0
|
||||
val parentBottom =
|
||||
WindowUtils.getScreenHeight(view.context.applicationContext) - statusBarHeight
|
||||
when (config.gravity) {
|
||||
// 右上
|
||||
Gravity.END, Gravity.END or Gravity.TOP, Gravity.RIGHT, Gravity.RIGHT or Gravity.TOP ->
|
||||
params.x = parentRect.right - view.width
|
||||
// 左下
|
||||
Gravity.START or Gravity.BOTTOM, Gravity.BOTTOM, Gravity.LEFT or Gravity.BOTTOM ->
|
||||
params.y = parentBottom - view.height
|
||||
// 右下
|
||||
Gravity.END or Gravity.BOTTOM, Gravity.RIGHT or Gravity.BOTTOM -> {
|
||||
params.x = parentRect.right - view.width
|
||||
params.y = parentBottom - view.height
|
||||
}
|
||||
// 居中
|
||||
Gravity.CENTER -> {
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
}
|
||||
// 上中
|
||||
Gravity.CENTER_HORIZONTAL, Gravity.TOP or Gravity.CENTER_HORIZONTAL ->
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
// 下中
|
||||
Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL -> {
|
||||
params.x = (parentRect.right - view.width).shr(1)
|
||||
params.y = parentBottom - view.height
|
||||
}
|
||||
// 左中
|
||||
Gravity.CENTER_VERTICAL, Gravity.START or Gravity.CENTER_VERTICAL, Gravity.LEFT or Gravity.CENTER_VERTICAL ->
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
// 右中
|
||||
Gravity.END or Gravity.CENTER_VERTICAL, Gravity.RIGHT or Gravity.CENTER_VERTICAL -> {
|
||||
params.x = parentRect.right - view.width
|
||||
params.y = (parentBottom - view.height).shr(1)
|
||||
}
|
||||
// 其他情况,均视为左上
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置偏移量
|
||||
params.x += config.offsetPair.first
|
||||
params.y += config.offsetPair.second
|
||||
|
||||
if (config.immersionStatusBar) {
|
||||
if (config.showPattern != ShowPattern.CURRENT_ACTIVITY) {
|
||||
params.y -= statusBarHeight
|
||||
}
|
||||
} else {
|
||||
if (config.showPattern == ShowPattern.CURRENT_ACTIVITY) {
|
||||
params.y += statusBarHeight
|
||||
}
|
||||
}
|
||||
// 更新浮窗位置信息
|
||||
windowManager.updateViewLayout(view, params)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -64,6 +64,7 @@ class MoGoWarningFragment : MvpFragment<MoGoWarningContract.View?, WaringPresent
|
||||
.setLayout(notificationView)
|
||||
.setSidePattern(SidePattern.TOP)
|
||||
.setGravity(Gravity.CENTER_HORIZONTAL, offsetY = 110)
|
||||
.setImmersionStatusBar(true)
|
||||
.setAnimator(object : DefaultAnimator() {
|
||||
override fun enterAnim(
|
||||
view: View,
|
||||
|
||||
Reference in New Issue
Block a user