This article is a full translation and summary of the official Google Tutorial, has ensured the correctness of the document.
Lest everyone be confused by a variety of other information, there is no system to learn.
One, set the window size and adapt the screen resolution
Google's official document mentions two big things.
1.Viewport View Window
This is set in HTML. The main is to set the height and width, as well as the initial column. This width and height will be the absolute HTML inside the CSS with how many pixels are available.
The width of the viewport has no decisive relationship with the width of the screen. For example, your phone width is 480PX but you can set the width of the viewport to 960PX, so that a 800PX picture can be displayed intact, because many browsers, such as Chrome, will zoom as much as possible, zoom out to a screen can show the entire page, That is, the width of 960PX.
Note: The global profile mode is not used by default when using WebView. That is, auto-shrinking, set it up in this way setUseWideViewPort() .
<title>Example</title> <meta name="Viewport" content="Width=device-width, User-scalable=no" />
<meta name="Viewport" content=" Height = [Pixel_value | "Device-height"] , width = [Pixel_value | "Device-width"] , Initial-scale = Float_value , Minimum-scale = Float_value , Maximum-scale = Float_value , user-scalable = ["Yes" | "No"] " />
Note: Do not turn off user-scalable unless you are sure that your program is well-suited and can be used in the smallest size.
2. Screen resolution
HTML is written in CSS in pixel units, which are equivalent to 160DPI phones in Android. If it is a 320DPI phone, the unit will be magnified. For example, if a picture of 300 pixels wide, in 160PI can be normal display, but to the 320DPI of the phone will be magnified twice times, this time the picture will be distorted, blurred, texture.
Second, through the CSS to adapt
1. Use different CSS for different resolutions
By setting -webkit-device-pixel-ratio this attribute 0.75,1,,1.5 to correspond to low-rate, medium-resolution, high-resolution. is actually the following wording
<link rel="stylesheet" Media="screen and (-webkit-device-pixel-ratio:1.5)" href="Hdpi.css" /><link rel="stylesheet" Media="screen and (-webkit-device-pixel-ratio:1.0)" href="Mdpi.css" />
2. Or through different styles and style sheets, use the following notation
#header { background:url (medium-density-image.png);} @media screen and (-webkit-device-pixel-ratio:1.5) {/ * CSS for high-density screens */ #header { background : URL (high-density-image.png);} } @media screen and (-webkit-device-pixel-ratio:0.75) {/ * CSS for low-density screens */ #header { Backgroun D:url (low-density-image.png); }}
For more information on how to adapt resolution, especially picture adaptation, see High DPI Images for Variable Pixel densities..
Later, if there is time to summarize the contents of this link.
Third, using JavaScript to fit
The wording is as follows:
Android Browser or WebView will pass window.devicePixelRatio这个DOM文档定义的属性来指定当前的缩放比例。
默认的缩放比例是1.0.可以通过如下代码来获取比例,并作出对应的处理。
if (window.Devicepixelratio== 1.5) {Alert("This was a high-density screen");} Else if (window.Devicepixelratio== 0.75) {Alert("This was a low-density screen");}