如何在Android上平移和缩放.svg?

问题描述 投票:1回答:1

我正在使用Webview查看.svg文件(位于资产文件夹中)。在Android Studio上..svg的预览模式下,我可以完美地平移和缩放。但是,当我导出到.apk时,平移和缩放在该应用程序上根本不起作用。

注意:我用于测试的.svg文件是这个:https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg

您能帮我解决这个问题吗?

public class MainActivity extends AppCompatActivity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    //webView.loadUrl("file:///android_asset/www/index.html");


    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new Locater(), "locater");
    //webView.loadUrl("file:///android_asset/gallardo.svg");
    webView.loadUrl("file:///android_asset/Tiger.svg");
}}
android svg webview zoom pan
1个回答
0
投票

我以前有同样的问题。我对阅读英语没有信心,但是如果我理解的问题是正确的,这会有所帮助。

webview.getSettings().setSupportZoom(true);
//Sets whether the WebView should support zooming using its on-screen zoom controls and gestures.

webview.getSettings().setBuiltInZoomControls(true);
//Sets whether the WebView should use its built-in zoom mechanisms.
//The default is false. I think this is the cause.

尝试此代码。如果您想了解更多信息,请访问此网站。https://developer.android.com/reference/android/webkit/WebSettings

添加到

如果您想不显示显示缩放控制栏

webview.getSetting().setDisplayZoomControls(false); //The default is true

以编程方式在Build.VERSION_CODES.LOLLIPOP上控制缩放。

webView.zoomBy(factor); //This value must be in the range 0.01 to 100.0 inclusive.

在Build.VERSION_CODES.LOLLIPOP下以编程方式控制缩放。

webView.zoomOut(); //or zoomIn(). the return value means. it was changed;

这是简单的代码;此代码将svgWidth,svgHeight设置为关于Webview的全屏显示。getWidth(),getHeight()的值大约是Webview的宽度,高度。您可以在WebViewClient.onScaleChanged重写方法中获取currentScale值。

public void zoom(int svgWidth, int svgHeight){
    int targetWidth =  svgWidth + 300;
    int targetHeight = svgHeight + 300;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        float zoomWidth =  (getWidth() / (targetHeight * currentScale));
        float zoomHeight = (getHeight() / (targetHeight * currentScale));
        if(zoomWidth < zoomHeight) {
            this.zoomBy(zoomWidth);
        }else{
            this.zoomBy(zoomHeight);
        }
    }else {

        if (targetWidth > targetHeight) {
            while (targetWidth * getScale() < getWidth()) {
                if (!this.zoomIn()) break;
            }
            while (targetWidth * getScale() > getWidth()) {
                if (!this.zoomOut()) break;
            }
            this.zoomOut();
        } else {
            while (targetHeight * getScale() < getHeight()) {
                if (!this.zoomIn()) break;
            }
            while (targetHeight * getScale() > getHeight()) {
                if (!this.zoomOut()) break;
            }
            this.zoomOut();
        }
    }
}

注意。此代码只是简单的示例代码。我没有发现问题,但可能无法正常工作。如果您发现此代码有问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.