参考: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);
}
DisplayMetrics dm = getResources().getDisplayMetrics(); //实例化显示指标对象
int displayWidth = dm.widthPixels; //得到屏幕宽度
int displayHeight = dm.heightPixels; //得到屏幕高度