Can Android's WebView automatically resize huge images?

前端 未结 8 1794
心在旅途
心在旅途 2020-11-28 05:41

In some web browsers, huge images are automatically resized to fit the screen.

Is it possible to do the same in an Android WebView?

The web page just contain

相关标签:
8条回答
  • 2020-11-28 06:35

    You could send the size you want in the request parameters and let the server set the width/height in the img element for you.

    0 讨论(0)
  • 2020-11-28 06:41

    You can have the browser resize the image for you to fit the maximum width of the screen:

    <img src="huge-image.jpg" width="100%" />
    

    Resizing its height to WebView's viewport is possible too:

    <img src="huge-image.jpg" height="100%" />
    

    However, resizing both width and height would result in a stretched image. To either resize the width or height depending of what side fits best you may consider a bit of JavaScript, like this:

    <img src="huge-image.jpg" onload="resize(this);" />
    
    
    <script type="text/javascript">
    
        function resize(image)
        {
            var differenceHeight = document.body.clientHeight - image.clientHeight;
            var differenceWidth  = document.body.clientWidth  - image.clientWidth;
            if (differenceHeight < 0) differenceHeight = differenceHeight * -1;
            if (differenceWidth  < 0) differenceWidth  = differenceWidth * -1;
    
            if (differenceHeight > differenceWidth)
            {
                image.style['height'] = document.body.clientHeight + 'px';
            }
            else
            {
                image.style['width'] = document.body.clientWidth + 'px';
            }
    
            // Optional: remove margins or compensate for offset.
            image.style['margin'] = 0;
            document.body.style['margin'] = 0;
        }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题