当我使用以下 html 时:
img src="file:///sdcard/image1.jpg" width="100%" heighth="100%"
并使用 WebView.setBuiltInZoomControls(true)
,我的 webview 是可缩放的,但在我松开手指后,webview 又缩小了。
我猜这是因为 width="100%"heighth="100%"
。
但是 image1.jpg
很大,一开始我想在屏幕上显示整个图像。稍后用户可以放大/缩小。
任何解决方案或代码示例?
最佳答案
您必须手动计算需要使用的比例。 PIC_WIDTH 是 webview 的宽度。
private int getScale(){
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
Double val = new Double(width)/new Double(PIC_WIDTH);
val = val * 100d;
return val.intValue();
}
然后使用
WebView web = new WebView(this);
web.setPadding(0, 0, 0, 0);
web.setInitialScale(getScale());
关于Android WebView 缩放图像不适用于宽度 ="100%",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7439823/