Skip to content

Commit 8b7f956

Browse files
committed
优化屏幕旋转后计算坐标距离屏幕 Gap 比例的算法
1 parent adf8ddd commit 8b7f956

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,11 @@ public void onScreenOrientationChange() {
409409
// Log.i(getClass().getSimpleName(), "currentViewOnScreenX = " + currentViewOnScreenX + ",currentViewOnScreenY = " + currentViewOnScreenY);
410410

411411
// 先扣除 View 自身宽度,用剩余空余空间算比例
412-
int currentHorizontalGap = Math.max(screenWidth - windowViewWidth, 0);
413-
final float leftGapRatio = currentHorizontalGap > 0 ? currentViewOnScreenX / (float) currentHorizontalGap : 0.5f;
412+
final int currentHorizontalGap = Math.max(screenWidth - windowViewWidth, 0);
413+
final float leftGapRatio = calculateGapSizeRatio(currentViewOnScreenX, currentHorizontalGap);
414414
// 先扣除 View 自身高度,用剩余空余空间算比例
415-
int currentVerticalGap = Math.max(screenHeight - windowViewHeight, 0);
416-
final float topGapRatio = currentVerticalGap > 0 ? currentViewOnScreenY / (float) currentVerticalGap : 0.5f;
415+
final int currentVerticalGap = Math.max(screenHeight - windowViewHeight, 0);
416+
final float topGapRatio = calculateGapSizeRatio(currentViewOnScreenY, currentVerticalGap);
417417
// Log.i(getClass().getSimpleName(), "leftGapRatio = " + leftGapRatio + ",topGapRatio = " + topGapRatio);
418418

419419
easyWindow.sendTask(() -> {
@@ -446,6 +446,30 @@ public boolean isFollowScreenRotationChanges() {
446446
return true;
447447
}
448448

449+
/**
450+
* 计算空余空间的比例
451+
*
452+
* @param gapSize 空余空间的大小
453+
* @param totalSize 总空间的大小
454+
*/
455+
protected float calculateGapSizeRatio(float gapSize, float totalSize) {
456+
// 防止除零异常,如果没有空余空间了,就默认放在中间位置
457+
if (totalSize <= 0) {
458+
return 0.5f;
459+
}
460+
float ratio = gapSize / totalSize;
461+
if (ratio > 0.99f) {
462+
// 如果比例超过了 99%,就认为它已经贴边了,直接放在边缘位置
463+
return 1f;
464+
} else if (ratio < 0.01f) {
465+
// 如果比例小于了 1%,就认为它已经贴边了,直接放在边缘位置
466+
return 0f;
467+
} else {
468+
// 正常情况,返回计算的比例
469+
return ratio;
470+
}
471+
}
472+
449473
public void updateLocation(float x, float y) {
450474
updateLocation(x, y, isAllowMoveToScreenSafeArea());
451475
}

0 commit comments

Comments
 (0)