获取手机像素,密度和宽高(dp)

//                获取手机像素,密度和宽高(dp                WindowManager wm = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
                DisplayMetrics dm = new DisplayMetrics();
                wm.getDefaultDisplay().getMetrics(dm);
                int width = dm.widthPixels;         // 屏幕宽度(像素)
                int height = dm.heightPixels;       // 屏幕高度(像素)
                float density = dm.density;         // 屏幕密度(0.75 / 1.0 / 1.5                int densityDpi = dm.densityDpi;     // 屏幕密度dpi120 / 160 / 240                // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
                int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
                int screenHeight = (int) (height / density);// 屏幕高度(dp)
                Log.e("h_bl", "屏幕宽度(像素):" + width);
                Log.e("h_bl", "屏幕高度(像素):" + height);
                Log.e("h_bl", "屏幕密度(0.75 / 1.0 / 1.5):" + density);
                Log.e("h_bl", "屏幕密度dpi120 / 160 / 240):" + densityDpi);
                Log.e("h_bl", "屏幕宽度(dp):" + screenWidth);
                Log.e("h_bl", "屏幕高度(dp):" + screenHeight);

你可能感兴趣的:(获取手机像素,密度和宽高(dp))