动态获取popupWindow的宽高

本文介绍了一种根据屏幕尺寸动态调整弹窗大小的方法,确保了在不同设备上保持一致的比例显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

popupWindow的长宽按比例去取屏幕宽度

思路:

思路是这样的
比如:保存的时候宽高是300和100 
宽高比就是n=3/1=3 
弹窗的时候是取得到屏幕宽高的
因为不知道是平板还是手机
比如都要取屏幕的0.8
n>1 那宽就是 屏幕宽度*0.8   高度就是  屏幕宽度*0.8/n
n<1 那高就是 屏幕高度*0.8   宽度就是  屏幕高度*0.8*n


代码:

    public static double window_scale = 0.8;// 给定的屏幕比例
	public static float width = 130;// 给定的图片宽度
	public static float height = 78;// 给定的图片高度
	public static double scale = width / height;// scale=1.67
	
	    int width, heigth;
		if (Const.width > Const.height) {
			width = (int) (ScreenUtils.getScreenWidth(context) * Const.window_scale);
			heigth = (int) ((ScreenUtils.getScreenWidth(context) * Const.window_scale) /Const.scale);
			Const.pop_scale = width / heigth;
		} else {
			width = (int) (ScreenUtils.getScreenHeight(context) * Const.window_scale);
			heigth = (int) ((ScreenUtils.getScreenHeight(context) * Const.window_scale) * Const.scale);
			Const.pop_scale = heigth / width;
		}
		
		
			/**
	 * 获取屏幕的宽度(单位:px)
	 *
	 * @return 屏幕宽px
	 */
	public static int getScreenWidth(Context context) {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();// 创建了一张白纸
		windowManager.getDefaultDisplay().getMetrics(dm);// 给白纸设置宽高
		return dm.widthPixels;
	}

	/**
	 * 获取屏幕的高度(单位:px)
	 *
	 * @return 屏幕高px
	 */
	public static int getScreenHeight(Context context) {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();// 创建了一张白纸
		windowManager.getDefaultDisplay().getMetrics(dm);// 给白纸设置宽高
		return dm.heightPixels;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值