获取屏幕尺寸

可以使用类DisplayMetrics来获取屏幕尺寸:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Log.i("ClockFrog", "widthPixels: " + metrics.widthPixels);
        Log.i("ClockFrog", "heightPixels: " + metrics.heightPixels);
        Log.i("ClockFrog", "density: " + metrics.density);
        Log.i("ClockFrog", "densityDpi: " + metrics.densityDpi);
        Log.i("ClockFrog", "widthDpi: " + metrics.widthPixels/metrics.density);
        Log.i("ClockFrog", "heightDpi: " + metrics.heightPixels/metrics.density);
    }

最终数据为:

02-01 18:17:21.868 23731-23731/com.nd.sample I/ClockFrog: widthPixels: 2560
02-01 18:17:21.868 23731-23731/com.nd.sample I/ClockFrog: heightPixels: 1600
02-01 18:17:21.869 23731-23731/com.nd.sample I/ClockFrog: density: 2.0
02-01 18:17:21.869 23731-23731/com.nd.sample I/ClockFrog: densityDpi: 320
02-01 18:17:21.869 23731-23731/com.nd.sample I/ClockFrog: widthDpi: 1280.0
02-01 18:17:21.869 23731-23731/com.nd.sample I/ClockFrog: heightDpi: 800.0

分别对应于:

名称
屏幕宽度 2560
屏幕高度 1600
屏幕密度 2.0
像素密度 320
像素宽度 1280.0
像素高度 800.0

你可能感兴趣的:(获取屏幕尺寸)