A, Android get screen width and height
- Get it from the Resources object
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float widthPixels = displayMetrics.widthPixels; / / screen width float heightPixels = displayMetrics. HeightPixels; // Screen height float density = displayMetrics. Density; // Screen density
- From the WindowManager object
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(displayMetrics); float widthPixels = displayMetrics.widthPixels; / / / / screen width float widthPixels = windowManager. GetDefaultDisplay () getWidth (); / / screen width float heightPixels = displayMetrics. HeightPixels; / / / / screen height float heightPixels = windowManager. GetDefaultDisplay () getHeight (). // Screen height float density = displayMetrics. Density; // Screen density
Two, hongmeng get screen width height DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes();
Attributes. Height Screen height Attributes. Width Screen width attributes. XDpi xDpi attributes Attributes. DensityDpi DPI density Attributes. ScalDensity Density