如何在加载内容后将webview的内容缩放到特定的比例?(我不想重新加载内容。)
我正在开发android 2.3和更高版本。
最佳答案
解决方案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();
}
});