Android webView Load picture show the problem of excessive

The basic process of using webview I will not repeat here the description herein, this situation is for the article details after loading is complete, the method we use here is: by js script, reset the width and height of the img tag in the picture.

Steps for usage:

1, this method requires the use js, so the properties must be provided webview add the following sentence, or invalid.

webView.getSettings () setJavaScriptEnabled (true); . // support JavaScript
2, to webview reset WebViewClient

webView.setWebViewClient (new new ArticleWebViewClient ());
. 3, the rewriting WebViewClient method onPageFinished

public class MyWebViewClient extends WebViewClient {

@Override
public void onPageFinished (the WebView View, String URL) {
super.onPageFinished (View, URL);
// reset the picture size webview img tag
imgReset ();
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

/ **
* reset the picture size, the width of the phone is the screen width, height is automatically scaled to width ratio
** /
Private void imgReset () {
webView.loadUrl ( "JavaScript: (function () {" +
"var objs document.getElementsByTagName = ( 'IMG'); "+
" for (var I = 0; I <objs.length; I ++) "+
" { "
+" var = IMG OBJS [I]; "+
" img.style. = maxWidth '100%'; img.style.height = 'Auto'; "+
"} "+
"}) () ");
}

 

Guess you like

Origin www.cnblogs.com/ly570/p/11667871.html