[英]Android: scaling SVG in WebView
I have a problem with WebView and SVG. 我有WebView和SVG的问题。 When i load svg (with javascript) into WebView, it scales... randomly.
当我将svg(使用javascript)加载到WebView中时,它会随机缩放。 Absolutely randomly on different devices and different screen orientation on one device.
绝对随机地在一台设备上的不同设备和不同屏幕方向上。 I need to place SVG in WebView with fot to WebView size, ventered and enabled zoom.
我需要将SVG放在WebView中,使用fot到WebView大小,通信和启用缩放。 A tried this html:
试过这个html:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<object type="image/svg+xml" data="AutomobileMain.svg" width="100%" height="100%" align="middle">
<param name="src" value="AutomobileMain.svg" />
<param name="wmode" value="transparent" />
</object>
</body>
</html>
It propretly works in browser, but not works in WebView. 它很可能在浏览器中工作,但在WebView中不起作用。
Here a code: 这里有一个代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mSchemeImageWebView = (WebView) inflater.inflate(
R.layout.newremark_image, container, false);
WebSettings webSettings = mSchemeImageWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);
mSchemeImageWebView.addJavascriptInterface(new IJavascriptHandler(),
"android");
mSchemeImageWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
prepareResizedToFit(mSchemeImageWebView);
}
});
reloadWebView() ;
return mSchemeImageWebView;
}
public void reloadWebView() {
mSchemeImageWebView.loadUrl(getSchemePath()) ;
}
private void prepareResizedToFit(WebView webView) {
webView.setInitialScale(scale(mSchemeImageWebView));
}
private int scale(View view) {
int height = Math.round(view.getHeight()) ;
int width = Math.round(view.getWidth()) ;
Dimension parsedDimension ;
try {
parsedDimension = new SVGDimensionsParser().parse(getResources()
.getAssets().open(getSchemeFileName())) ;
double scaleXtoMax = width / parsedDimension.width ;
double scaleYtoMax = height / parsedDimension.height ;
double scale = Math.min( scaleXtoMax , scaleYtoMax ) ;
return (int) ( scale * 100.0 ) ;
} catch (Exception e) {
return 100;
}
}
private static class SVGDimensionsParser {
public Dimension parse(InputStream in) throws XmlPullParserException,
IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private Dimension readFeed(XmlPullParser parser)
throws XmlPullParserException, IOException {
if (parser.getName().equals("svg")) {
return new Dimension(
Double.parseDouble(parser.getAttributeValue(null,
"height").replaceAll("px", "")),
Double.parseDouble(parser.getAttributeValue(null,
"width").replaceAll("px", "")));
} else {
return null;
}
}
}
You might be seeing the effects of the device pixel ratio which varies by device. 您可能会看到设备像素比率的影响因设备而异。
Try adding the following into the header of your HTML file. 尝试将以下内容添加到HTML文件的标题中。
<meta name="viewport" content="width=device-width, initial-scale=1">
If that doesn't work, it might be a problem with how your SVG file is written. 如果这不起作用,则可能是您的SVG文件编写方式有问题。 How the SVG is positioned in the window can be affected by various attributes of the root
<svg>
element. SVG在窗口中的位置可能会受到根
<svg>
元素的各种属性的影响。 Specifically the viewBox, width, height and preserveAspectRatio attributes. 特别是viewBox,width,height和preserveAspectRatio属性。
Try tinkering with them. 尝试修补它们。 More info on how they work can be found in the SVG spec.
有关它们如何工作的更多信息可以在SVG规范中找到。
http://www.w3.org/TR/SVG/struct.html#SVGElement http://www.w3.org/TR/SVG/struct.html#SVGElement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.