Skip to content

Commit 8e61c96

Browse files
committed
优化计算留白大小比例方法实现
1 parent 712c228 commit 8e61c96

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

library/src/main/java/com/hjq/window/draggable/IWindowDraggableAuxiliary.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ default void offsetMotionEventLocation(@NonNull View parentView, @NonNull View c
253253
}
254254

255255
/**
256-
* 计算空余空间的比例
256+
* 计算留白大小比例
257257
*
258258
* @param gapSize 空余空间的大小
259259
* @param totalSize 总空间的大小
@@ -263,16 +263,25 @@ default float calculateGapSizeRatio(float gapSize, float totalSize) {
263263
if (totalSize <= 0) {
264264
return 0.5f;
265265
}
266-
float ratio = gapSize / totalSize;
266+
267+
final float ratio = gapSize / totalSize;
268+
267269
if (ratio > 0.99f) {
268270
// 如果比例超过了 99%,就认为它已经贴边了,直接放在边缘位置
269271
return 1f;
270-
} else if (ratio < 0.01f) {
272+
}
273+
274+
if (ratio < 0.01f) {
271275
// 如果比例小于了 1%,就认为它已经贴边了,直接放在边缘位置
272276
return 0f;
273-
} else {
274-
// 正常情况,返回计算的比例
275-
return ratio;
276277
}
278+
279+
if (ratio > 0.49f && ratio < 0.51f) {
280+
// 如果比例在 49% ~ 51%,就认为它已经居中了,直接放在中间位置
281+
return 0.5f;
282+
}
283+
284+
// 正常情况,返回计算的比例
285+
return ratio;
277286
}
278287
}

0 commit comments

Comments
 (0)