Android WebView: scale to a certain scale after loading content

How to zoom WebView content to a certain scale after loading content? (I do not want to reload the content.)

I am working on Android 2.3 and later.

+4
source share
1 answer

Solution 1. Here we set the zoom level in percent, this means that when the page is ready, getScale will return 5.2f:

mView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        view.setInitialScale(520);

        super.onPageStarted(view, url, favicon);
    }

Solution 2. This approach is not 100% accurate as it rounds the scale to an int value:

private WebView mView;
private Handler mZoomHandler = new Handler();
private Runnable mZoomRunnable = new Runnable() {

    @Override
    public void run() {
        if ((int) mView.getScale() < WANTED_SCALE) {
            mView.zoomIn();
            mZoomHandler.postDelayed(this, 10);
        } else if ((int) mView.getScale() > WANTED_SCALE) {
            mView.zoomOut();
            mZoomHandler.postDelayed(this, 10);
        }
    }
};

mView.getSettings().setSupportZoom(true);
mView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        mZoomRunnable.run();
    }
});
+4
source

All Articles