Android 如何计算设备像素比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23841812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:31:06  来源:igfitidea点击:

How to calculate device pixel ratio

androidhtmlcssandroid-layout

提问by Adityaraj Pednekar

I would like to know how the -webkit-device-pixel-ratio is calculated. I had already read this. Yet I am unable to understand it clearly. I would also like to know if there is a list of which devices use which pixel ratio. The android website says

我想知道 -webkit-device-pixel-ratio 是如何计算的。我已经读过这篇了。然而我无法清楚地理解它。我还想知道是否有哪些设备使用哪种像素比的列表。安卓网站说

The Android Browser and WebView support a CSS media feature that allows you to create styles for specific screen densities—the -webkit-device-pixel-ratio CSS media feature. The value you apply to this feature should be either "0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium density, or high density screens, respectively.

Android 浏览器和 WebView 支持 CSS 媒体功能,该功能允许您为特定屏幕密度创建样式——-webkit-device-pixel-ratio CSS 媒体功能。应用于此功能的值应为“0.75”、“1”或“1.5”,以指示样式分别适用于低密度、中密度或高密度屏幕的设备。

but I found that we need to use -webkit-device-pixel-ratio=2 to make a web application compatible on 768 x 1280 resolution screen.

但我发现我们需要使用 -webkit-device-pixel-ratio=2 使 Web 应用程序兼容 768 x 1280 分辨率的屏幕。

采纳答案by Jet Blue

According to this article

根据这篇文章

http://www.html5rocks.com/en/mobile/high-dpi/

http://www.html5rocks.com/en/mobile/high-dpi/

The magic number seems to be 150.

神奇的数字似乎是 150。

Dividing the physical ppi by the ideal ppi of 150, gives the device pixel ratio.

将物理 ppi 除以 150 的理想 ppi,得出设备像素比。

E.g. a device with 445ppi would have a dpr of 3 ->> 445 / 150

例如,具有 445ppi 的设备的 dpr 为 3 ->> 445 / 150

The simple calculation seems to hold up okay for many items on this list,

这个简单的计算似乎对这个清单上的许多项目都成立,

http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density

http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density

回答by Dmitry Davydov

You can find many of your screen parameters by visiting https://www.mydevice.io/enter image description here

您可以通过访问https://www.mydevice.io/找到许多屏幕参数在此处输入图片说明

回答by user2991535

you can find the pixel ratio using javascript window.devicePixelRatio

您可以使用 javascript window.devicePixelRatio 找到像素比

回答by MokoJumbie

Device Pixel Ratio (DPR) is the relationship between physical hardware-based pixels and Device-Independent Pixels which are an abstraction. The value of the device pixel ratio is not something which can reliably be calculated as it's based on how the device manufacturer intends to map one type of pixel system to the other. To reliably obtain this information, you will need to request it from the device.

设备像素比 (DPR) 是基于物理硬件的像素与抽象的设备无关像素之间的关系。设备像素比的值不是可以可靠计算的值,因为它基于设备制造商打算如何将一种类型的像素系统映射到另一种类型。要可靠地获取此信息,您需要从设备请求它。

回答by Piyush Sharma

claculate display metrics and get

caculate 显示指标并获得

DisplayMetrics dm = context.getResources().getDisplayMetrics(); int densityDpi = dm.densityDpi

DisplayMetrics dm = context.getResources().getDisplayMetrics(); 整数密度Dpi = dm.密度Dpi

and use dm.xdpi, or dm.ydpi i.e pixcel desity

并使用 dm.xdpi 或 dm.ydpi 即像素密度

回答by Hashim

The problem is that there is no one database with all of (even the most common) devices listed, so looking up the viewport specs for a given device are a case of trying to find the device in any one of several databases.

问题是没有一个数据库列出了所有(甚至是最常见的)设备,因此查找给定设备的视口规格是一种尝试在多个数据库中的任何一个中查找设备的情况。

These are the databases that I've come across and used, that list device viewports along with their pixel ratios:

这些是我遇到并使用过的数据库,其中列出了设备视口及其像素比率:

Feel free to add to the list above, as I'm sure there are much more comprehensive databases out there.

随意添加到上面的列表中,因为我确信那里有更全面的数据库。

Also remember that resolution / DPR = viewport, so for testing the responsiveness of a site you generally only need one of either DPR or viewport, and the viewport itself is generally better.

还请记住resolution / DPR = viewport,为了测试站点的响应能力,您通常只需要 DPR 或视口之一,而视口本身通常更好。