private void getSystemInfo() { //第一种方式获取屏幕的像素宽高在Activity里面可使用 //DisplayMetrics metric = new DisplayMetrics(); //Activity.getWindowManager().getDefaultDisplay().getMetrics(metric); //第二种方式获取屏幕的像素宽高 //获取屏幕的像素高度PIX screenHeight = getResources().getDisplayMetrics().heightPixels; //获取屏幕的像素宽度PIX screenWidth = getResources().getDisplayMetrics().widthPixels; }
private void getSystemInfo() { //需要上下文 DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); //第一种获取像素密度的方式 int width = metric.widthPixels; // 宽度(PX) int height = metric.heightPixels; // 高度(PX) Log.e("++MyLog++widthPixels ",""+width); Log.e("++MyLog++heightPixels ",""+height); float density = metric.density; // 密度(0.75 / 1.0 / 1.5) int densityDpi = metric.densityDpi; // 密度DPI(120 / 160 / 240) Log.e("++MyLog++density ",""+density); Log.e("++MyLog++densityDpi ",""+densityDpi); //第二种获取像素密度的方式getResources() int height1 = getResources().getDisplayMetrics().heightPixels; // 宽度(PX) int width1 = getResources().getDisplayMetrics().widthPixels; // 高度(PX) Log.e("++MyLog++width1",""+width1); Log.e("++MyLog++height1",""+height1); float density1 = getResources().getDisplayMetrics().density; int densityDpi1 = getResources().getDisplayMetrics().densityDpi; Log.e("++MyLog++density1 ",""+density1); Log.e("++MyLog++densityDpi1 ",""+densityDpi1); float scaledDensity1 = getResources().getDisplayMetrics().scaledDensity; Log.e("++MyLog++scaledDensity1"," "+scaledDensity1); //像素密度X/Ydpi //physical pixels per inch of the screen in the Y dimension float xdpi1 = getResources().getDisplayMetrics().xdpi; float ydpi1 = getResources().getDisplayMetrics().ydpi; Log.e("++MyLog++xdpi1 ",""+xdpi1); Log.e("++MyLog++ydpi1 ",""+ydpi1); }
时间: 2024-11-29 04:28:42