mise en page - Comment obtenir des dimensions d'écran comme des pixels dans Android

Mots clés : androidlayoutscreenpixeldimensionsandroid

meilleur 4 Réponses mise en page - Comment obtenir des dimensions d'écran comme des pixels dans Android

vote vote

100

Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; 
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); 
Display display = getWindowManager().getDefaultDisplay();  int width = display.getWidth();  // deprecated int height = display.getHeight();  // deprecated 
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); 
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels); 
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();  // Gets all excluding insets  final WindowInsets windowInsets = metrics.getWindowInsets();  Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()          | WindowInsets.Type.displayCutout());   int insetsWidth = insets.right + insets.left;  int insetsHeight = insets.top + insets.bottom;   // Legacy size that Display#getSize reports  final Rect bounds = metrics.getBounds();  final Size legacySize = new Size(bounds.width() - insetsWidth,          bounds.height() - insetsHeight); 
vote vote

81

Display display = getWindowManager().getDefaultDisplay();  int width = display.getWidth(); int height = display.getHeight(); 
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);        int height = metrics.heightPixels; int width = metrics.widthPixels; 
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();  // Gets all excluding insets  final WindowInsets windowInsets = metrics.getWindowInsets();  Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()          | WindowInsets.Type.displayCutout());   int insetsWidth = insets.right + insets.left;  int insetsHeight = insets.top + insets.bottom;   // Legacy size that Display#getSize reports  final Rect bounds = metrics.getBounds();  final Size legacySize = new Size(bounds.width() - insetsWidth,          bounds.height() - insetsHeight); 
vote vote

80

int measuredWidth = 0; int measuredHeight = 0; WindowManager w = getWindowManager();  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {     Point size = new Point();     w.getDefaultDisplay().getSize(size);     measuredWidth = size.x;     measuredHeight = size.y; } else {     Display d = w.getDefaultDisplay();     measuredWidth = d.getWidth();     measuredHeight = d.getHeight(); } 
vote vote

60

WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; try {     // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)     widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);     heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } try {     // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)     Point realSize = new Point();     Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);     widthPixels = realSize.x;     heightPixels = realSize.y; } catch (Exception ignored) { } 
WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) try {     widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);     heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 17) try {     Point realSize = new Point();     Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);     widthPixels = realSize.x;     heightPixels = realSize.y; } catch (Exception ignored) { } 

Questions similaires