Android/画面サイズ
提供: 初心者エンジニアの簡易メモ
import android.graphics.Point; import android.view.WindowManager; import android.view.Display; // 画面幅サイズ(ステータスバーを含まない) Point point = new Point(); // Android 3.2~ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE); Display disp = wm.getDefaultDisplay(); disp.getSize(point); } Log.i("test", "x=" + point.x); Log.i("test", "y=" + point.y);
// 画面幅サイズ(ステータスバーを含む) WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // Android 4.2~ try { Method getRealSize = Display.class.getMethod("getRealSize", Point.class); getRealSize.invoke(display, point); Log.i("test", "width=" + point.x); Log.i("test", "height=" + point.y); } catch (Exception e) { e.printStackTrace(); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { // Android 3.2~ try { Method getRawWidth = Display.class.getMethod("getRawWidth"); Method getRawHeight = Display.class.getMethod("getRawHeight"); int width = (Integer) getRawWidth.invoke(display); int height = (Integer) getRawHeight.invoke(display); point.set(width, height); Log.i("test", "width=" + point.x); Log.i("test", "height=" + point.y); } catch (Exception e) { e.printStackTrace(); } }