如何找到设备为LDPI MDPI华电国际或XHDPI设备、国际、MDPI、LDPI

2023-09-03 21:47:33 作者:幼稚园新童鞋

在没有任何code段的应用,如何让屏幕的屏幕分辨率和长度。我怎么能找到的设备是否LDPI,MDPI,华电国际和xhdpi?

Without any code snippet for the application, how to get screen resolution and length of the screen. How could I find whether the device is ldpi, mdpi , hdpi or xhdpi ?

推荐答案

编辑: 使用 DisplayMetrics 获取屏幕的密度

use DisplayMetrics to get the density of the screen

getResources().getDisplayMetrics().densityDpi;

这将返回重新presents以下常量的int值。 DisplayMetrics.DENSITY_LOW,DisplayMetrics.DENSITY_MEDIUM,DisplayMetrics.DENSITY_HIGH,DisplayMetrics.DENSITY_XHIGH

this will return the int value that represents the following constants. DisplayMetrics.DENSITY_LOW ,DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_XHIGH

  int density= getResources().getDisplayMetrics().densityDpi;

switch(density)
{
case DisplayMetrics.DENSITY_LOW:
   Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_MEDIUM:
     Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_HIGH:
    Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_XHIGH:
     Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
    break;
}

这将返回  以下常量的基础上thsi可以识别该设备

This will return following constants based on thsi you can identify the device

试试这个

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
        break;
    default:
        Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}

来源确定屏幕分辨率