javascript Android webView 支持 svg 渲染

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18756433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:07:04  来源:igfitidea点击:

Android webView support for svg rendering

javascriptandroidsvgwebviewandroid-webview

提问by direndd

I have an app which is developed with android 4.03 - API Level 15. It has a webView which i want to use to show an html page with some svg content. Some svg content are directly embeded to html and some are dynamically rendered using javascript.

我有一个用 android 4.03 - API Level 15 开发的应用程序。它有一个 webView,我想用它来显示一个带有一些 svg 内容的 html 页面。有些 svg 内容直接嵌入到 html 中,有些则使用 javascript 动态呈现。

I have a huawei S7 tablet which runs with android 2.2. I have added a backward compatibility pack so i can run my app in the tab.

我有一台运行 android 2.2 的华为 S7 平板电脑。我添加了一个向后兼容包,所以我可以在选项卡中运行我的应用程序。

Now when i create the html page and run it in the dektop browser it perfectly renders all the svg conent. When i run the app in the tablet it doesn't show any svg content. It just displays a white background. But when i try the same app in my friends nexus 7 tablet with android 4.3 it perfectly shows all the svg content in the webView.

现在,当我创建 html 页面并在桌面浏览器中运行它时,它会完美呈现所有 svg 内容。当我在平板电脑上运行该应用程序时,它不显示任何 svg 内容。它只显示白色背景。但是当我在我的朋友 nexus 7 平板电脑和 android 4.3 中尝试相同的应用程序时,它完美地显示了 webView 中的所有 svg 内容。

I use this code to initialize the webView

我使用此代码来初始化 webView

WebView mapView;
mapView = (WebView) findViewById(R.id.mapview);
mapView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
            }
        });

        WebSettings s = mapView.getSettings();
        s.setLoadWithOverviewMode(true);
        s.setLoadsImagesAutomatically(true);
        s.setUseWideViewPort(true);
        s.setJavaScriptEnabled(true);
        s.setSupportZoom(true);
        s.setBuiltInZoomControls(true);


        File externalStorage = Environment.getExternalStorageDirectory();    
        String url = "file:///" + externalStorage + "/floor_one.html";

mapView.loadUrl(url);

Is there any compatibility issue in android 2.2 webView with SVG ?

android 2.2 webView 与 SVG 是否存在兼容性问题?

回答by Yuichi Araki

SVG was not supported before Android 3.0, so you have to find some workaround.

Android 3.0 之前不支持 SVG,因此您必须找到一些解决方法。

This blog post explains two of Javascript polyfills for SVG.

这篇博文解释了 SVG 的两个 Javascript polyfill。

http://www.kendoui.com/blogs/teamblog/posts/12-02-17/using_svg_on_android_2_x_and_kendo_ui_dataviz.aspx

http://www.kendoui.c​​om/blogs/teamblog/posts/12-02-17/using_svg_on_android_2_x_and_kendo_ui_dataviz.aspx

回答by cYrixmorten

What if you add this:

如果你添加这个怎么办:

webView.getSettings().setPluginState(PluginState.ON);

The only parameter I can see that you dont have that might have an effect.

我可以看到您没有的唯一参数可能会产生影响。