Skip to content

Commit 923ece6

Browse files
committed
新增 setWindowSizePercent 方法使得外层设置窗口大小更加便捷
1 parent 65a749c commit 923ece6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ easyWindow.setWindowTag(@Nullable String tag);
255255
easyWindow.getWindowTag();
256256
// 设置悬浮窗宽度和高度
257257
easyWindow.setWindowSize(int width, int height);
258+
// 设置悬浮窗大小(按照屏幕百分比)
259+
easyWindow.setWindowSizePercent(@FloatRange(from = 0, to = 1) float widthPercent, @FloatRange(from = 0, to = 1) float heightPercent);
258260

259261
// 设置悬浮窗的位置
260262
easyWindow.setWindowLocation(@Px int x, @Px int y);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import android.support.annotation.Px;
2727
import android.support.annotation.StringRes;
2828
import android.support.transition.Slide.GravityFlag;
29+
import android.util.DisplayMetrics;
2930
import android.util.TypedValue;
31+
import android.view.Display;
3032
import android.view.Gravity;
3133
import android.view.LayoutInflater;
3234
import android.view.View;
@@ -258,6 +260,26 @@ public X setWindowSize(int width, int height) {
258260
return (X) this;
259261
}
260262

263+
/**
264+
* 设置悬浮窗大小(按照屏幕百分比)
265+
*
266+
* @param widthPercent 窗口宽度百分比
267+
* @param heightPercent 窗口高度百分比
268+
*/
269+
@SuppressWarnings("deprecation")
270+
public X setWindowSizePercent(@FloatRange(from = 0, to = 1) float widthPercent, @FloatRange(from = 0, to = 1) float heightPercent) {
271+
widthPercent = Math.min(Math.max(widthPercent, 0), 1);
272+
heightPercent = Math.min(Math.max(heightPercent, 0), 1);
273+
Display defaultDisplay = mWindowManager.getDefaultDisplay();
274+
if (defaultDisplay == null) {
275+
return setWindowSize(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
276+
}
277+
278+
DisplayMetrics metrics = new DisplayMetrics();
279+
defaultDisplay.getMetrics(metrics);
280+
return setWindowSize((int) (metrics.widthPixels * widthPercent), (int) (metrics.heightPixels * heightPercent));
281+
}
282+
261283
/**
262284
* 设置窗口位置
263285
*

0 commit comments

Comments
 (0)