什么是设备像素比率? [英] what exactly is device pixel ratio?

查看:139
本文介绍了什么是设备像素比率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于移动网络的每一篇文章,但在这里我找不到这个属性究竟是什么测量的解释。

任何人都可以详细说明这样的查询是什么?

  @media only screen and(-webkit-min-device-pixel-ratio:1.5),
only screen and(min- moz-device-pixel-ratio:1.5),
只屏幕和(-o-device-pixel-ratio:3/2),
只屏幕和){

//高分辨率图片到这里

}


例如,iPhone 4和iPhone 4S报告设备像素比为2,因为物理线性分辨率是逻辑线性分辨率的两倍。




  • 物理分辨率:960 x 640

  • 逻辑分辨率:480 x 320



,其中和

其他设备报告不同的设备像素比率,包括: 非整数。例如,诺基亚Lumia 1020报告1.6667,Samsumg Galaxy S4报告3和Apple iPhone 6 Plus报告2.46 (来源:



这涉及到网页设计,例如准备高清晰度的图像资源,并仔细应用不同的设备像素比率的不同的图像。你不想强迫低端设备下载一个非常高分辨率的图像,只能在本地降级。您还不希望高端设备升级低分辨率图片以获得模糊的用户体验。



如果您遇到位图图片,可以容纳许多不同的设备像素比率,则应使用 CSS媒体查询为不同的广告素材提供不同的资源集设备组。结合使用 background- size:cover 或明确设置 background-size 到百分比值。



示例



  #element {background-image:url('lores.png'); } 

@media only screen and(min-device-pixel-ratio:2){
#element {background-image:url('hires.png'); }
}

@media only screen and(min-device-pixel-ratio:3){
#element {background-image:url('superhires.png') ; }
}

这样,每种设备类型只加载正确的映像资源。另请注意,CSS 始终中的 px 单位在逻辑像素上运行。



向量图形的情况



随着越来越多的设备类型出现,向所有设备提供足够的位图资源变得更加棘手。在CSS中,媒体查询是目前唯一的方法,而在HTML5中,图片元素允许您为不同的媒体查询使用不同的来源,但是支持仍然不是100%,因为大多数网络开发人员仍然需要支持IE11一段时间更多(源: caniuse



如果您需要简洁不是照片的图标,线条艺术,设计元素的图片,您需要开始考虑SVG,它可以精美地缩放到所有分辨率。


this is mentioned every article about mobile web, but nowhere I can found an explanation of what exactly does this attribute measure.
Can anyone please elaborate what does queries like this check?

@media only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (min--moz-device-pixel-ratio: 1.5),
only screen and (-o-device-pixel-ratio: 3/2), 
only screen and (min-device-pixel-ratio: 1.5) {

    //high resolution images go here

}

解决方案

Short answer

The device pixel ratio is the ratio between physical pixels and logical pixels. For instance, the iPhone 4 and iPhone 4S report a device pixel ratio of 2, because the physical linear resolution is double the logical linear resolution.

  • Physical resolution: 960 x 640
  • Logical resolution: 480 x 320

The formula is , where and are the physical and logical linear resolutions.

Other devices report different device pixel ratios, including non-integer ones. For example, the Nokia Lumia 1020 reports 1.6667, the Samsumg Galaxy S4 reports 3, and the Apple iPhone 6 Plus reports 2.46 (source: dpilove). But this does not change anything in principle, as you should never design for any one specific device.

Discussion

The CSS "pixel" is not even defined as "one picture element on some screen", but rather as a non-linear angular measurement of viewing angle, which is approximately of an inch at arm's length. Source: CSS Absolute Lengths

This has lots of implications when it comes to web design, such as preparing high-definition image resources and carefully applying different images at different device pixel ratios. You wouldn't want to force a low-end device to download a very high resolution image, only to downscale it locally. You also don't want high-end devices to upscale low resolution images for a blurry user experience.

If you are stuck with bitmap images, to accommodate for many different device pixel ratios, you should use CSS Media Queries to provide different sets of resources for different groups of devices. Combine this with nice tricks like background-size: cover or explicitly set the background-size to percentage values.

Example

#element { background-image: url('lores.png'); }

@media only screen and (min-device-pixel-ratio: 2) {
    #element { background-image: url('hires.png'); }
}

@media only screen and (min-device-pixel-ratio: 3) {
    #element { background-image: url('superhires.png'); }
}

This way, each device type only loads the correct image resource. Also keep in mind that the px unit in CSS always operates on logical pixels.

A case for vector graphics

As more and more device types appear, it gets trickier to provide all of them with adequate bitmap resources. In CSS, media queries is currently the only way, and in HTML5, the picture element lets you use different sources for different media queries, but the support is still not 100 % since most web developers still have to support IE11 for a while more (source: caniuse).

If you need crisp images for icons, line-art, design elements that are not photos, you need to start thinking about SVG, which scales beautifully to all resolutions.

这篇关于什么是设备像素比率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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