Skip to content

Commit 2909cee

Browse files
committed
新增 setWindowLocationPercent 方法使得外层设置窗口位置更加便捷
1 parent 923ece6 commit 2909cee

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ easyWindow.setWindowSizePercent(@FloatRange(from = 0, to = 1) float widthPercent
261261
// 设置悬浮窗的位置
262262
easyWindow.setWindowLocation(@Px int x, @Px int y);
263263
easyWindow.setWindowLocation(@GravityFlag int gravity, @Px int x, @Px int y);
264+
// 设置窗口位置(偏移量按照屏幕百分比)
265+
easyWindow.setWindowLocationPercent(@FloatRange(from = 0, to = 1) @Px float horizontalPercent, @FloatRange(from = 0, to = 1) @Px float verticalPercent);
266+
easyWindow.setWindowLocationPercent(@GravityFlag int gravity, @FloatRange(from = 0, to = 1) @Px float horizontalPercent, @FloatRange(from = 0, to = 1) @Px float verticalPercent);
264267

265268
// 设置悬浮窗外层是否可触摸
266269
easyWindow.setOutsideTouchable(boolean touchable);

library/src/main/java/com/hjq/window/EasyWindow.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,33 @@ public X setWindowLocation(@Px int x, @Px int y) {
307307
return setWindowLocation(Gravity.LEFT | Gravity.TOP, x, y);
308308
}
309309

310+
/**
311+
* 设置窗口位置(偏移量按照屏幕百分比)
312+
*
313+
* @param gravity 窗口重心
314+
* @param horizontalPercent 水平方向百分比
315+
* @param verticalPercent 垂直方向百分比
316+
*/
317+
@SuppressWarnings("deprecation")
318+
public X setWindowLocationPercent(@GravityFlag int gravity, @FloatRange(from = 0, to = 1) @Px float horizontalPercent,
319+
@FloatRange(from = 0, to = 1) @Px float verticalPercent) {
320+
horizontalPercent = Math.min(Math.max(horizontalPercent, 0), 1);
321+
verticalPercent = Math.min(Math.max(verticalPercent, 0), 1);
322+
Display defaultDisplay = mWindowManager.getDefaultDisplay();
323+
if (defaultDisplay == null) {
324+
return (X) this;
325+
}
326+
327+
DisplayMetrics metrics = new DisplayMetrics();
328+
defaultDisplay.getMetrics(metrics);
329+
return setWindowLocation(gravity, (int) (metrics.widthPixels * horizontalPercent), (int) (metrics.heightPixels * verticalPercent));
330+
}
331+
332+
public X setWindowLocationPercent(@FloatRange(from = 0, to = 1) @Px float horizontalPercent,
333+
@FloatRange(from = 0, to = 1) @Px float verticalPercent) {
334+
return setWindowLocationPercent(Gravity.LEFT | Gravity.TOP, horizontalPercent, verticalPercent);
335+
}
336+
310337
/**
311338
* 设置悬浮窗外层是否可触摸
312339
*/

0 commit comments

Comments
 (0)