Java – how to zoom the WebView display SVG to a fixed pixel height?

I want to display an SVG graphic called logo on my JavaFX application

FXML WebView

<WebView fx:id="logo" 
    disable="true" 
    maxHeight="100" 
    minHeight="100"
    minWidth="100" 
    AnchorPane.leftAnchor="0.0" 
    AnchorPane.rightAnchor="0.0"
    AnchorPane.topAnchor="0.0" />

Part of the controller main class

@FXML WebView logo = new WebView();

@Override
public void initialize(URL location,ResourceBundle resources) {
    String logoUrl = Main.class.getResource("FILENAME.svg").toExternalForm();
    logo.getEngine().load(logoUrl);
}

The graphics are displayed, but the size is turned off I want it to fix the pixel height and automatically scale the width My problem is that I can only use one factor logo Setscalex (0.8d) for scaling And logo setScaleY(0.8d);, This is not good, because I will always fiddle with finding the right factors. Similarly, it needs to be readjusted when replacing SVG, which is also unnecessary

So my question is: how do I scale the WebView (content) to a fixed pixel height (for example, 100px) and keep its aspect ratio as width?

Solution

Have you ever tried to use the maxwidht maxheight or prefwidth prefheight property to serve you?

Without this work, the only option is to use scaleX & Scaley You can write some routines that will do for you

class WebViewUtil {

    public static void scaleTo(WebView view,Int size) {
        double currentHeight = view.getHeight();
        double currentWidth = view.getWidth();

        double scaleX = size / currentWidth;
        double scaleY = size / currentHeight;
        double scale = Math.min(scaleX,scaleY);

        view.scaleX(scale); view.scaleY(scale);
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>