//获得手机的宽度和高度像素单位为px
// 通过WindowManager获取
//必须在Activity中获取,即使使用Context 也必须强转为Activity
public void getScreenDensity_ByWindowManager(){
DisplayMetrics mDisplayMetrics = new DisplayMetrics();//屏幕分辨率容器
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
int width = mDisplayMetrics.widthPixels;
int height = mDisplayMetrics.heightPixels;
float density = mDisplayMetrics.density;
int densityDpi = mDisplayMetrics.densityDpi;
Log.d(TAG,"Screen Ratio: ["+width+"x"+height+"],density="+density+",densityDpi="+densityDpi);
Log.d(TAG,"Screen mDisplayMetrics: "+mDisplayMetrics);
}
// 通过Resources获取
// 不需要在Activity中使用, Application 和Activity中的Context 均可以使用此函数
public void getScreenDensity_ByResources(){
DisplayMetrics mDisplayMetrics = getResources().getDisplayMetrics();
int width = mDisplayMetrics.widthPixels;
int height = mDisplayMetrics.heightPixels;
float density = mDisplayMetrics.density;
int densityDpi = mDisplayMetrics.densityDpi;
Log.d(TAG,"Screen Ratio: ["+width+"x"+height+"],density="+density+",densityDpi="+densityDpi);
Log.d(TAG,"Screen mDisplayMetrics: "+mDisplayMetrics);
}
// 获取屏幕的默认分辨率
public void getDefaultScreenDensity(){
Display mDisplay = getWindowManager().getDefaultDisplay();
int width = mDisplay.getWidth();
int height = mDisplay.getHeight();
Log.d(TAG,"Screen Default Ratio: ["+width+"x"+height+"]");
Log.d(TAG,"Screen mDisplay: "+mDisplay);
}