Java代碼
Display display = getWindowManager().getDefaultDisplay();
display.getWidth();//返回px值
display.getHeight();//返回px值
display.getRotation();返回0或1,0表示豎屏,1表示橫屏
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.density;
dm.heightPixels;
dm.widthPixels;
看看上面兩者的關系
Sdk代碼片段代碼
public void getMetrics(DisplayMetrics outMetrics) {
outMetrics.widthPixels = getWidth();
outMetrics.heightPixels = getHeight();
outMetrics.density = mDensity;
outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f);
outMetrics.scaledDensity= outMetrics.density;
outMetrics.xdpi = mDpiX;
outMetrics.ydpi = mDpiY;
}
//僅僅是將Display的內部值賦給瞭DisplayMetrics的屬性值
作者“程序人生”