简书链接:android真实分辨率和非真实分辨率
文章字数:292,阅读全文大约需要1分钟
今天写代码的时候发现一个奇葩的问题,明明一样的公式,的出来的结果是不一样的,后面才知道这屏幕高度不是1920而是1794,所以的出来的值不一样。
1 2 3 4 5
| //1794/1080 1794/1080 =1.66111 this.smartScreenHeight= screenSize.height/screenSize.width>=1.8f? (float) (1.6611111111111111111111111111111f * screenSize.width) :screenSize.height; // this.smartScreenHeight=screenSize.height;
|
这句代码是解决一个问题,这个问题就是宽和高过长导致的按钮 太高太丑的bug.
另外这里有一个获取真实高度的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| }
/** * 通过反射,获取包含虚拟键的整体屏幕高度 * * @return */ public static int getRawScreenHeight(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int width = display.getWidth(); int height = wm.getDefaultDisplay().getHeight(); DisplayMetrics dm = new DisplayMetrics(); @SuppressWarnings("rawtypes") Class c; int dpi; try { c = Class.forName("android.view.Display"); @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, dm); dpi = dm.heightPixels; return dpi; } catch (Exception e) { e.printStackTrace(); return 0; } }
|
根据比例得出自己手机应该的高度
1 2 3 4
| this.smartScreenHeight = screenSize.width * (667f / 375f);//传递1080得到1920.96
Log.w("CALC_HEIGHT",this.smartScreenHeight+"");
|
1 2 3 4 5 6 7
| 期望的屏幕高度=当前屏幕宽度*(667/375) 转换后的坐标=(坐标/667)*期望的屏幕高度
//第二句话的第二种写法 667为高度 期望的屏幕高度/667 *坐标
|