|
26 | 26 | import android.support.annotation.Px; |
27 | 27 | import android.support.annotation.StringRes; |
28 | 28 | import android.support.transition.Slide.GravityFlag; |
| 29 | +import android.util.DisplayMetrics; |
29 | 30 | import android.util.TypedValue; |
| 31 | +import android.view.Display; |
30 | 32 | import android.view.Gravity; |
31 | 33 | import android.view.LayoutInflater; |
32 | 34 | import android.view.View; |
@@ -258,6 +260,26 @@ public X setWindowSize(int width, int height) { |
258 | 260 | return (X) this; |
259 | 261 | } |
260 | 262 |
|
| 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 | + |
261 | 283 | /** |
262 | 284 | * 设置窗口位置 |
263 | 285 | * |
|
0 commit comments