From a2a4dceacff0baf782eef8fcc6b4c0ddf8283597 Mon Sep 17 00:00:00 2001 From: Lewis Date: Thu, 24 Sep 2020 11:18:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- UpdateDoc.md | 3 ++ .../main/java/com/lzf/easyfloat/EasyFloat.kt | 8 ++--- .../easyfloat/permission/rom/MiuiUtils.java | 4 +-- .../activityfloat/AbstractDragFloatingView.kt | 23 ++++++++------ .../widget/appfloat/AppFloatManager.kt | 31 +++++++++---------- .../easyfloat/widget/appfloat/TouchUtils.kt | 29 +++++++++++++---- 7 files changed, 61 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 0142694..2821729 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ allprojects { - **在应用模块的`build.gradle`添加:** ``` dependencies { - implementation 'com.github.princekin-f:EasyFloat:1.3.3' + implementation 'com.github.princekin-f:EasyFloat:1.3.4' } ``` diff --git a/UpdateDoc.md b/UpdateDoc.md index 38cd95c..7a42c58 100644 --- a/UpdateDoc.md +++ b/UpdateDoc.md @@ -1,4 +1,7 @@ ## 版本更新日志 +#### v 1.3.4: +- 优化细节。 + #### v 1.3.3: - 支持传入`with(context)`实现部分系统浮窗功能; - 对未合理`init`、未合理`with(context)`的情况,直接进行抛异常; diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt b/easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt index afce1ad..edfa383 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt @@ -89,21 +89,21 @@ class EasyFloat { // *************************** 以下系统浮窗的相关方法 *************************** /** - * 关闭系统级浮窗,发送广播消息,在Service内部接收广播 + * 关闭系统级浮窗 */ @JvmStatic @JvmOverloads fun dismissAppFloat(tag: String? = null) = FloatManager.dismiss(tag) /** - * 隐藏系统浮窗,发送广播消息,在Service内部接收广播 + * 隐藏系统浮窗 */ @JvmStatic @JvmOverloads fun hideAppFloat(tag: String? = null) = FloatManager.visible(false, tag, false) /** - * 显示系统浮窗,发送广播消息,在Service内部接收广播 + * 显示系统浮窗 */ @JvmStatic @JvmOverloads @@ -280,7 +280,7 @@ class EasyFloat { else callbackCreateFailed(WARN_CONTEXT_ACTIVITY) /** - * 通过Service创建系统浮窗 + * 通过浮窗管理类创建系统浮窗 */ private fun createAppFloat() = FloatManager.create(activity, config) diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MiuiUtils.java b/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MiuiUtils.java index 0dfedbe..40cd9b6 100755 --- a/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MiuiUtils.java +++ b/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MiuiUtils.java @@ -84,7 +84,7 @@ public static void applyMiuiPermission(Context context) { goToMiuiPermissionActivity_V6(context); } else if (versionCode == 7) { goToMiuiPermissionActivity_V7(context); - } else if (versionCode == 8) { + } else if (versionCode >= 8) { goToMiuiPermissionActivity_V8(context); } else { Log.e(TAG, "this is a special MIUI rom version, its version code " + versionCode); @@ -182,7 +182,7 @@ public static void goToMiuiPermissionActivity_V8(Context context) { intent.setPackage("com.miui.securitycenter"); intent.putExtra("extra_pkgname", context.getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - + if (isIntentAvailable(intent, context)) { context.startActivity(intent); } else { diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/widget/activityfloat/AbstractDragFloatingView.kt b/easyfloat/src/main/java/com/lzf/easyfloat/widget/activityfloat/AbstractDragFloatingView.kt index cbd62af..076b8d1 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/widget/activityfloat/AbstractDragFloatingView.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/widget/activityfloat/AbstractDragFloatingView.kt @@ -31,12 +31,15 @@ abstract class AbstractDragFloatingView( // 浮窗配置 var config: FloatConfig + // 悬浮的父布局高度、宽度 private var parentHeight = 0 private var parentWidth = 0 + // 终点坐标 private var lastX = 0 private var lastY = 0 + // 浮窗各边距离父布局的距离 private var leftDistance = 0 private var rightDistance = 0 @@ -204,15 +207,17 @@ abstract class AbstractDragFloatingView( // 如果是拖动状态下即非点击按压事件 isPressed = !config.isDrag - when (config.sidePattern) { - SidePattern.RESULT_LEFT, - SidePattern.RESULT_RIGHT, - SidePattern.RESULT_TOP, - SidePattern.RESULT_BOTTOM, - SidePattern.RESULT_HORIZONTAL, - SidePattern.RESULT_VERTICAL, - SidePattern.RESULT_SIDE -> sideAnim() - else -> if (config.isDrag) touchOver() + if (config.isDrag) { + when (config.sidePattern) { + SidePattern.RESULT_LEFT, + SidePattern.RESULT_RIGHT, + SidePattern.RESULT_TOP, + SidePattern.RESULT_BOTTOM, + SidePattern.RESULT_HORIZONTAL, + SidePattern.RESULT_VERTICAL, + SidePattern.RESULT_SIDE -> sideAnim() + else -> touchOver() + } } } diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt index 43fb300..f66a080 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt @@ -123,7 +123,7 @@ internal class AppFloatManager(val context: Context, var config: FloatConfig) { 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.LEFT, Gravity.LEFT or Gravity.BOTTOM -> + 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 -> { @@ -132,24 +132,24 @@ internal class AppFloatManager(val context: Context, var config: FloatConfig) { } // 居中 Gravity.CENTER -> { - params.x = ((parentRect.right - view.width) * 0.5f).toInt() - params.y = ((parentBottom - view.height) * 0.5f).toInt() + 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) * 0.5f).toInt() + params.x = (parentRect.right - view.width).shr(1) // 下中 Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL -> { - params.x = ((parentRect.right - view.width) * 0.5f).toInt() + 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) * 0.5f).toInt() + 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) * 0.5f).toInt() + params.y = (parentBottom - view.height).shr(1) } // 其他情况,均视为左上 else -> { @@ -167,22 +167,19 @@ internal class AppFloatManager(val context: Context, var config: FloatConfig) { * 设置浮窗的可见性 */ fun setVisible(visible: Int, needShow: Boolean = true) { - if (frameLayout == null) return + if (frameLayout == null || frameLayout!!.childCount < 1) return // 如果用户主动隐藏浮窗,则该值为false config.needShow = needShow - frameLayout?.visibility = visible + frameLayout!!.visibility = visible + val view = frameLayout!!.getChildAt(0) if (visible == View.VISIBLE) { config.isShow = true - if (frameLayout!!.childCount > 0) { - config.callbacks?.show(frameLayout!!.getChildAt(0)) - config.floatCallbacks?.builder?.show?.invoke(frameLayout!!.getChildAt(0)) - } + config.callbacks?.show(view) + config.floatCallbacks?.builder?.show?.invoke(view) } else { config.isShow = false - if (frameLayout!!.childCount > 0) { - config.callbacks?.hide(frameLayout!!.getChildAt(0)) - config.floatCallbacks?.builder?.hide?.invoke(frameLayout!!.getChildAt(0)) - } + config.callbacks?.hide(view) + config.floatCallbacks?.builder?.hide?.invoke(view) } } diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/TouchUtils.kt b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/TouchUtils.kt index 198feaf..17f22dd 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/TouchUtils.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/TouchUtils.kt @@ -22,23 +22,29 @@ internal class TouchUtils(val context: Context, val config: FloatConfig) { // 窗口所在的矩形 private var parentRect: Rect = Rect() + // 悬浮的父布局高度、宽度 private var parentHeight = 0 private var parentWidth = 0 + // 起点坐标 private var lastX = 0f private var lastY = 0f + // 浮窗各边距离父布局的距离 private var leftDistance = 0 private var rightDistance = 0 private var topDistance = 0 private var bottomDistance = 0 + // x轴、y轴的最小距离值 private var minX = 0 private var minY = 0 private val location = IntArray(2) + // 屏幕可用高度 - 浮窗自身高度 的剩余高度 private var emptyHeight = 0 + // 是否包含状态栏 private var hasStatusBar = true @@ -219,19 +225,24 @@ internal class TouchUtils(val context: Context, val config: FloatConfig) { val animator = ValueAnimator.ofInt(if (isX) params.x else params.y, end) animator.addUpdateListener { - if (isX) params.x = it.animatedValue as Int else params.y = it.animatedValue as Int - windowManager.updateViewLayout(view, params) + try { + if (isX) params.x = it.animatedValue as Int else params.y = it.animatedValue as Int + // 极端情况,还没吸附就调用了关闭浮窗,会导致吸附闪退 + windowManager.updateViewLayout(view, params) + } catch (e: Exception) { + animator.cancel() + } } animator.addListener(object : Animator.AnimatorListener { override fun onAnimationRepeat(animation: Animator?) {} override fun onAnimationEnd(animation: Animator?) { - config.isAnim = false - config.callbacks?.dragEnd(view) - config.floatCallbacks?.builder?.dragEnd?.invoke(view) + dragEnd(view) } - override fun onAnimationCancel(animation: Animator?) {} + override fun onAnimationCancel(animation: Animator?) { + dragEnd(view) + } override fun onAnimationStart(animation: Animator?) { config.isAnim = true @@ -240,6 +251,12 @@ internal class TouchUtils(val context: Context, val config: FloatConfig) { animator.start() } + private fun dragEnd(view: View) { + config.isAnim = false + config.callbacks?.dragEnd(view) + config.floatCallbacks?.builder?.dragEnd?.invoke(view) + } + /** * 计算一些边界距离数据 */