我需要得到一个点相对于屏幕的精确坐标,不管应用窗口的尺寸或offsetsinsets。我正在开发的手机分辨率为1080x2280,安卓9。我试着用以下方法找到屏幕尺寸 getDefaultDisply
但屏幕上的缺口高度却被减去了。
// Testing with notch hidden; the screen is pushed down below it
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); // 1080x2062 (-notification bar height!)
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); // 1080x2192 (actual window height when notch is hidden)
我怎样才能得到真实的分辨率和凹槽的高度呢? 当它被隐藏的时候?
我找到了我自己的解决方案,只在root设备上工作(需要有 壳牌 库)。)
public static int getCutoutHeight() {
CommandResult result = Shell.SU.run("dumpsys display | grep mCurrentDisplayRect");
String output = result.getStdout();
String regex = "^\\s+mCurrentDisplayRect=Rect\\(\\d+, (\\d+) - \\d+, \\d+\\)*$";
if (output != null) {
if (output.matches(regex)) {
Pattern patt = Pattern.compile(regex);
Matcher matcher = patt.matcher(output);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
}
}
else Log.e(TAG, "Unexpedted outptu: " + output);
}
else Log.e(TAG, "Command failed: " + result.getStderr());
return 0;
}
希望能尽快有更好的答案。