Android 获取屏幕宽度和高度的几种方法
Posted 王睿丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 获取屏幕宽度和高度的几种方法相关的知识,希望对你有一定的参考价值。
参考:https://www.jianshu.com/p/1a931d943fb4
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public String getPingMu4(){
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getRealSize(outSize);
int x = outSize.x;
int y = outSize.y;
return "x = " + x + ",y = " + y;
}
public String getPingMu3(){
Rect outSize = new Rect();
getWindowManager().getDefaultDisplay().getRectSize(outSize);
int left = outSize.left;
int top = outSize.top;
int right = outSize.right;
int bottom = outSize.bottom;
return "left = " + left + ",top = " + top + ",right = " + right + ",bottom = " + bottom;
}
public String getPingMu2(){
Display defaultDisplay = getWindowManager().getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
int x = point.x;
int y = point.y;
return x + " * " + y;
}
public static float getPingMu1(Context mContext) { int densityDpi = mContext.getResources().getDisplayMetrics().densityDpi;
float scaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
float density = mContext.getResources().getDisplayMetrics().density;
float xdpi = mContext.getResources().getDisplayMetrics().xdpi;
float ydpi = mContext.getResources().getDisplayMetrics().ydpi;
int width = mContext.getResources().getDisplayMetrics().widthPixels;
int height = mContext.getResources().getDisplayMetrics().heightPixels;
// 这样可以计算屏幕的物理尺寸
float width2 = (width / xdpi)*(width / xdpi);
float height2 = (height / ydpi)*(width / xdpi);
return (float) Math.sqrt(width2+height2);
}
以上是关于Android 获取屏幕宽度和高度的几种方法的主要内容,如果未能解决你的问题,请参考以下文章