简体   繁体   中英

Check android device density in html

How do I check device density of android device inside html page? I tried below code but not working for android device -

if($(window).devicePixelRatio == 1) {
    a1=a1+a1*5;
}
else if($(window).devicePixelRatio == 2) {
    a1=a1+a1*10;
}

you can get device density by using javaScriptInterFace.

wv.addJavascriptInterface(new JavaScriptInterface(context),
            "jsInterface");


public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public float getDensity() {
        return scale = mContext.getResources().getDisplayMetrics().density;
    }
}  

and in your html get density by javascript

var scale = jsInterface.getDensity();  
  • 0.75 means low density
  • 1.0 means standard (medium) density
  • 1.5 means high (large) density
  • 2.0 means extra high density

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM