Skip to content

Commit

Permalink
优化细节
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewis committed Sep 24, 2020
1 parent c1ef152 commit a2a4dce
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
3 changes: 3 additions & 0 deletions UpdateDoc.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 版本更新日志
#### v 1.3.4:
- 优化细节。

#### v 1.3.3:
- 支持传入`with(context)`实现部分系统浮窗功能;
- 对未合理`init`、未合理`with(context)`的情况,直接进行抛异常;
Expand Down
8 changes: 4 additions & 4 deletions easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -280,7 +280,7 @@ class EasyFloat {
else callbackCreateFailed(WARN_CONTEXT_ACTIVITY)

/**
* 通过Service创建系统浮窗
* 通过浮窗管理类创建系统浮窗
*/
private fun createAppFloat() = FloatManager.create(activity, config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand All @@ -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 -> {
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

/**
* 计算一些边界距离数据
*/
Expand Down

0 comments on commit a2a4dce

Please sign in to comment.