Android WebView:加载内容后缩放到特定比例

时间:2013-12-19 06:40:06

标签: android webview zoom

如何在加载内容后将WebView的内容缩放到特定比例? (我不想重新加载内容。)

我正在使用Android 2.3及更高版本。

1 个答案:

答案 0 :(得分:4)

解决方案1:这里我们设置缩放级别百分比,这意味着当页面准备就绪时,getScale将返回5.2f:

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

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

解决方案2:此方法不是100%准确,因为它将比例舍入为int值:

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();
    }
});