开发者

android browser: get scale factor

开发者 https://www.devze.com 2023-01-31 05:01 出处:网络
is it possible to somehow get the curren开发者_如何学编程t scaling factor in javascript on the android browser?

is it possible to somehow get the curren开发者_如何学编程t scaling factor in javascript on the android browser?


The Android Browser and WebView support a DOM property that allows you to query the density of the current device—the window.devicePixelRatio DOM property. The value of this property specifies the scaling factor used for the current device. For example, if the value of window.devicePixelRatio is "1.0", then the device is considered a medium density device and no scaling is applied by default; if the value is "1.5", then the device is considered a high density device and the page is scaled 1.5x by default; if the value is "0.75", then the device is considered a low density device and the page is scaled 0.75x by default. Of course, the scaling that the Android Browser and WebView apply is based on the web page's target density—as described in the section about Defining the viewport target density, the default target is medium-density, but you can change the target to affect how your web page is scaled for different screen densities.

For example, here's how you can query the device density with JavaScript:

if (window.devicePixelRatio == 1.5) {
    alert("This is a high-density screen");
} else if (window.devicePixelRatio == 0.75) {
   alert("This is a low-density screen");
}

For more information you can check out Tageting Screens from Web Apps

0

精彩评论

暂无评论...
验证码 换一张
取 消