Using HTML5 to develop Android (1)---Multi-resolution problems with Android devices

Source: Internet
Author: User

Android Browser default preview mode browsing will be reduced to the original size in the page WebView

Android Browser and webview default to MDPI. hdpi equivalent to mdpi 1.5 times times ldpi equivalent to 0.75 times times

Three solutions: 1 viewport attribute 2 CSS control 3 JS control

1 viewport attributes are placed in the <meta> of HTML

HTML code
<span style= "Font-size:x-small" >   

The attributes of viewport in Meta are as follows

HTML code
<span style= "Font-size:x-small" >   <meta name= "viewport"          content= "               height = [Pixel_value | Device-height],               width = [Pixel_value | device-width],               initial-scale = float_value,               minimum-scale = Float_v Alue,               Maximum-scale = float_value,               user-scalable = [yes | no],               target-densitydpi = [Dpi_value | device-dpi |               high-dpi | medium-dpi | LOW-DPI]           "       /></span>  

2 CSS Control device density

Create a separate style sheet for each density (note that the webkit-device-pixel-ratio 3 values correspond to 3 resolutions)

HTML code
<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"/> <link  rel= "stylesheet" Media= "screen and (-webkit-device-pixel-ratio:0.75)" href= "Ldpi.css"/>  

In a style sheet, specify a different style

HTML code
#header {     <span style= "White-space:pre" ></SPAN> 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 {            Background:url (low-density-image.png);        }    }  

HTML code

<meta name= "viewport" content= "target-densitydpi=device-dpi, Width=device-width"/>  

3 JS Control

Android Browser and WebView support querying DOM features of the current set density

Window.devicepixelratio has 3 values (0.75,1,1.5 corresponds to 3 resolutions)

The method of querying device density in JS

JS Code

if (Window.devicepixelratio = = 1.5) {       alert ("This was a high-density screen");   } else if (window.devicepixelration = = 0.75) {       alert ("This was a low-density screen");   }  

Using HTML5 to develop Android (1)---Multi-resolution problems with Android devices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.