Android WebView, Scaling Image to fit the screen

前端 未结 9 1091
醉梦人生
醉梦人生 2020-11-29 01:13

What I have: I\'m loading image from a URL. I simply do (WebView).loadUrl(imageurl, extraheaders)

What I get: Image i

相关标签:
9条回答
  • 2020-11-29 01:49

    I had the same issue and doing this worked just fine:

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    
    String data = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
    data = data + "<body><center><img width=\""+width+"\" src=\""+url+"\" /></center></body></html>";
    webView.loadData(data, "text/html", null);
    

    Edit: As this remained as the accepted answer, here is a better solution (all credit to Tony below):

    WebView content = (WebView) findViewById(R.id.webView1);
    content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null);
    
    0 讨论(0)
  • 2020-11-29 01:50

    You could also do something like this.

    Add CSS style for img at the beginning (depends on your web data format) of your data string.

    <style>img{display: inline; height: auto; max-width: 100%;}</style>
    

    To quickly do it to data in WebView i did this.

    WebView content = (WebView) findViewById(R.id.webView1);
    content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null);
    

    It's pretty much like what bansal21ankit said, but instead it will work on every image in your HTML without extra work.


    Edit (clarification on post content):

    You can have any text/html value instead of post.getContent() from the example.

    Post content here is just an example of a text/html content which is loaded from some data source and then concatenated with the style part which makes any image in given content to fit the screen.

    0 讨论(0)
  • 2020-11-29 01:51

    Its a bit late but I hope this will help, what you can do is:

    String html = "<html><body><img src=\"" + URL + "\" width=\"100%\" height=\"100%\"\"/></body></html>";
    mWebView.loadData(html, "text/html", null);
    

    this will make the image exactly similar to your WebView's width, rest you can adjust the WebView itself.

    0 讨论(0)
  • 2020-11-29 02:03

    Code:

    web = (WebView) findViewById(R.id.at_image_web);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setUseWideViewPort(true);
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setLoadWithOverviewMode(true);
    web.loadUrl(linkImage);
    
    0 讨论(0)
  • 2020-11-29 02:04

    This work for me: webView = (WebView) findViewById(R.id.webView); WebSettings webSettings = webView.getSettings(); webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

    0 讨论(0)
  • 2020-11-29 02:11

    you should scale the webView to fit the screen:

     WebView data = (WebView) getViewById(R.id.webview1);
     data.getSettings().setLoadWithOverviewMode(true);
     data.getSettings().setUseWideViewPort(true);
    
    0 讨论(0)
提交回复
热议问题