项目上遇到一点小问题,显示的字体超大,但是分辨率又都是对的,根据大佬的指点了解到是显示密度的问题,第一次遇见顺便记录一下
如何知道当前的分辨率和显示密度
DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getRealMetrics(metric); int w = metric.widthPixels; // 宽(PX) int h = metric.heightPixels; // 高(PX) int densityDpi = metric.densityDpi;// 密度(一般为160/320) String str = "当前分辨率:" + Integer.toString(w) + '*' + Integer.toString(h) + " 密度:" + Integer.toString(densityDpi); Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();调节显示密度的方法有两种
方法1 直接通过abd命令修改 adb shell wm density 160
方法二直接通过代码修改
execRootCmd("wm density 160"); //pm命令可以通过adb在shell中执行,同样,我们可以通过代码来执行 private int execRootCmd(String cmd) { Process process = null; DataOutputStream dos = null; try { process = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(process.getOutputStream()); dos.writeBytes(cmd + "\n"); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); process.waitFor(); return process.exitValue(); } catch (Exception e) { return -1; } finally { try { if (dos != null) { dos.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } }