How to determine android device resolution is small, medium, or large

此生再无相见时 提交于 2021-02-08 10:44:50

问题


I am working on a Xamarin.Forms app and I need to identify the android screen/resolution is whether small, medium or large to adjust some content with the available space. For example, some labels (single line) are getting truncated in smaller devices. So I could make some adjustments if the resolution is smaller or not.

In iOS, when iPhone screen getting bigger, the resolution is also getting increased so it's easy to identify smaller resolution devices in iOS. But in android, this seems hard.

Android device resolution can be taken from

var resolutionH = Resources.DisplayMetrics.HeightPixels;
var resolutionW = Resources.DisplayMetrics.WidthPixels;

For testing, I have created the following emulators and run the app in them. Here's my result whether a label getting truncated or not.

Resolution  Density  Result
---------------------------
2560x1440   560      OK
1920x1080   400      OK

1280x720    320      Truncated
1280x720    280      OK
1280x720    240      OK
800x480     240      Truncated
800x480     160      OK

The problem here is a device with higher resolution and lower DPI won't cause any problem. Like a device with 1280x720 resolution and 240 DPI (or 280 DPI). Since there are tons of Android devices are available with different resolutions and densities this problem seems harder.

Is there a better way to categorized android devices (small, medium, and large)?


回答1:


The reason where a label's text getting truncated (in my case) or an element doesn't get enough space in a particular device is, the actual pixel calculation for an element using the density (dpi/ppi) and the density bucket that screen falls into. This article gives a good idea about calculating the physical size of an element for different display densities.

After some exhausting research, I was able to categorize the device screen by taking the combination of screen width pixels and density. (I got the data from Android developer website Distribution dashboard and Support different screen sizes)

I have categorized the screen width pixels into 4 categories and then calculated the screen size for each display density using following formula:

sqrt((widthPixels x widthPixels) + (heightPixels x heightPixels)) / density

Then I have searched in the GSMArena to find devices with the screen configurations in the above table. Screen size lower than 3 inches is mostly smartwatches and more than 8 inches could be Tabs and smart TVs. So I have taken devices with the screen size between 3 - 8 inches as mobile devices (smartphones).




回答2:


What you are looking for is easy to do in native android you can create a method and check for the DensityMetricsDensity something like below:

private string GetDeviceDensity()
    {
        var density = Resources.DisplayMetrics.DensityDpi;
        switch (density)
        {
            case DisplayMetricsDensity.Medium:
                return "MDPI";
            case DisplayMetricsDensity.High:
                return "HDPI";
            case DisplayMetricsDensity.Low:
                return "LDPI";
            case DisplayMetricsDensity.Xhigh:
                return "XHDPI";
            case DisplayMetricsDensity.Tv:
                return "TV";
            case DisplayMetricsDensity.Xxhigh:
                return "XXHDPI";
            case DisplayMetricsDensity.Xxxhigh:
                return "XXXHDPI";
            default:
                return "Unknown";
        }
    }


来源:https://stackoverflow.com/questions/55351752/how-to-determine-android-device-resolution-is-small-medium-or-large

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!