简体   繁体   中英

WebView autoscales local images

My question is similar to this one , but is different enough that I am going to post a new one.

I am writing an image viewer that downloads a PNG from a remote server, saves it to storage, and then opens it in a WebView so that I get multi-touch zoom for free. These images are usually going to be documents, so the user will want to be able to zoom in very far. I am sending down images of sufficient resolution to enable this, but the WebView seems to be "helping" me by scaling my image to the screen size to start out with. This means that my image is much too low resolution to be properly zoomed in on.

I have searched around for this, and I have found some other people encountering a similar problem , but the only possible solution I found was to cut the image up into tiles and reassemble them using HTML before loading it in the view. This seems like a kludge, and I am asking the StackOverflow hivemind if anyone has either a way to turn off that scaling, or an alternative method to get what I need.

The code that initializes the WebView :

viewer = (WebView)findViewById(R.id.activity_imageviewer_webview);
viewer.setBackgroundColor(0);
WebSettings settings = viewer.getSettings();
settings.setJavaScriptEnabled(false);
settings.setBuiltInZoomControls(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

The code that loads the image into the WebView :

viewer.loadUrl(Uri.fromFile(imageFile).toString());

Changing the viewport properties might help, take a look at the

<meta name="viewport" ...>

meta tag in the html header

We ended up solving this the hard way, by using the tiling method. We split the image up into vertical slices on our server, then send them down to be reassembled using HTML on the client before putting it into the WebView .

It seems to work reasonably well, actually. The graphic is definitely higher quality, and one can easily read the text in it. The only issue we've seen is that there is a performance hit for panning and zooming that for reasons unknown to me (yet) is extremely bad only on the Nexus S.

Until something else changes, I think this may be the only solution to the problem without writing a custom view control.

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