如何确定android设备分辨率是小、中还是大 [英] How to determine android device resolution is small, medium, or large

查看:29
本文介绍了如何确定android设备分辨率是小、中还是大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Xamarin.Forms 应用程序,我需要确定 android 屏幕/分辨率是小、中还是大,以根据可用空间调整一些内容.例如,一些标签(单行)在较小的设备中被截断.所以如果分辨率更小,我可以做一些调整.

在 iOS 中,当 iPhone 屏幕变大时,分辨率也会提高,因此很容易在 iOS 中识别分辨率较小的设备.但在安卓中,这似乎很难.

Android 设备分辨率可取自

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

为了测试,我创建了以下模拟器并在其中运行应用程序.无论标签是否被截断,这是我的结果.

分辨率密度结果---------------------------2560x1440 560 正常1920x1080 400 正常1280x720 320 截断1280x720 280 正常1280x720 240 正常800x480 240 截断800x480 160 正常

这里的问题是具有更高分辨率和更低 DPI 的设备不会导致任何问题.就像具有 1280x720 分辨率和 240 DPI(或 280 DPI)的设备.由于有大量具有不同分辨率和密度的 Android 设备可用,因此这个问题似乎更难.

是否有更好的方法来对 Android 设备(小型、中型和大型)进行分类?

解决方案

标签的文本被截断(在我的例子中)或元素在特定设备中没有获得足够空间的原因是,实际像素计算使用密度(dpi/ppi)和屏幕落入的密度桶的元素.这篇文章提供了一个关于计算不同显示密度下元素的物理尺寸的好主意.

经过一番艰苦的研究,我能够通过结合屏幕宽度像素和密度来对设备屏幕进行分类.(我从 Android 开发者网站

然后我在GSMArena中搜索了上表屏幕配置的设备.屏幕尺寸低于 3 英寸的主要是智能手表,超过 8 英寸的可能是 Tabs 和智能电视.所以我把屏幕尺寸在 3 到 8 英寸之间的设备作为移动设备(智能手机).

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)?

解决方案

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).

这篇关于如何确定android设备分辨率是小、中还是大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆