如何在JavaFx中设置svgPath的大小

Fra*_*ler 5 java svg javafx

我有一个包含ListCells的ListView,它包含一个由SvgPath定义的图标.

private class CustomCell extends ListCell<String> {
    private SVGPath icon = new SVGPath();

    public CustomCell() {
        icon.setContent("...");
    }

    @Override
    public void updateItem(String value, boolean empty) {
        super.updateItem(value, empty);
        if(empty || value == null) {
            setGraphic(null);
            setText(null);
        } else {
            setGraphic(icon);
            setText(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望图标填充列表单元格的高度(大约30px).但它总是显示非常大,并且单元格的大小设置为图标的高度.

即使我设置了列表单元格和图标的大小

public CustomCell()
{
    setPrefHeight(30);
    setMaxHeight(30);
    icon.setContent("...");
    icon.maxHeight(30);
    icon.prefHeight(30);
}
Run Code Online (Sandbox Code Playgroud)

它不起作用.列表单元格的高度是正确的,但图标仍然显示得太大.

我哪里错了?

编辑:这是我的svg的路径:

M 289.00,74.00 C 299.18,61.21 307.32,52.80 320.00,42.42 331.43,33.07 343.66,26.03 357.00,19.84 427.64,-12.98 509.92,2.91 564.42,58.28 583.93,78.10 595.94,99.15 605.58,125.00 607.76,130.86 611.37,144.75 612.54,151.00 613.15,154.23 613.28,160.06 615.58,162.44 617.49,164.42 624.11,165.84 627.00,166.86 634.80,169.62 639.97,172.04 647.00,176.42 673.69,193.07 692.76,221.39 695.83,253.00 700.60,302.03 676.64,345.41 630.00,364.00 621.17,367.52 608.48,370.99 599.00,371.00 599.00,371.00 106.00,371.00 106.00,371.00 96.50,370.99 87.00,368.97 78.00,366.00 36.29,352.22 6.21,312.25 6.00,268.00 5.77,219.90 34.76,179.34 81.00,165.02 96.78,160.14 107.02,161.00 123.00,161.00 124.59,150.68 130.49,137.79 136.05,129.00 150.70,105.88 173.22,88.99 200.00,82.65 213.13,79.55 219.79,79.85 233.00,80.00 247.37,80.17 264.61,85.94 277.00,93.00 279.11,86.37 284.67,79.45 289.00,74.00 Z
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它是一个我希望以特定大小显示的云(例如30,30).但是我找不到如何设置特定尺寸的方法.

Ita*_*iha 13

使用区域

在使用SVGPath一段时间之后,我发现最好将其设置为Region的形状并在此区域上定义min/pref/max大小.这样就不需要使用scaleX/Y因子,这会导致父级大小的布局边界和边界不同.

为实现这一目标,我们需要:

  • 定义一个新Region并将SVGPath设置为形状.
  • 定义此区域的最小/最大/最大大小.
  • 为此区域设置背景颜色,将其设置为svg形状的填充.

MCVE

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;

public class SVGPathResize extends Application {

    private static final double REQUIRED_WIDTH = 50.0;
    private static final double REQUIRED_HEIGHT = 30.0;

    @Override
    public void start(Stage primaryStage) throws Exception {
        SVGPath svg = new SVGPath();
        svg.setContent("M 289.00,74.00 C 299.18,61.21 307.32,52.80 320.00,42.42 331.43,33.07 343.66,26.03 357.00,19.84 427.64,-12.98 509.92,2.91 564.42,58.28 583.93,78.10 595.94,99.15 605.58,125.00 607.76,130.86 611.37,144.75 612.54,151.00 613.15,154.23 613.28,160.06 615.58,162.44 617.49,164.42 624.11,165.84 627.00,166.86 634.80,169.62 639.97,172.04 647.00,176.42 673.69,193.07 692.76,221.39 695.83,253.00 700.60,302.03 676.64,345.41 630.00,364.00 621.17,367.52 608.48,370.99 599.00,371.00 599.00,371.00 106.00,371.00 106.00,371.00 96.50,370.99 87.00,368.97 78.00,366.00 36.29,352.22 6.21,312.25 6.00,268.00 5.77,219.90 34.76,179.34 81.00,165.02 96.78,160.14 107.02,161.00 123.00,161.00 124.59,150.68 130.49,137.79 136.05,129.00 150.70,105.88 173.22,88.99 200.00,82.65 213.13,79.55 219.79,79.85 233.00,80.00 247.37,80.17 264.61,85.94 277.00,93.00 279.11,86.37 284.67,79.45 289.00,74.00 Z");
        final Region svgShape = new Region();
        svgShape.setShape(svg);
        svgShape.setMinSize(REQUIRED_WIDTH, REQUIRED_HEIGHT);
        svgShape.setPrefSize(REQUIRED_WIDTH, REQUIRED_HEIGHT);
        svgShape.setMaxSize(REQUIRED_WIDTH, REQUIRED_HEIGHT);
        svgShape.setStyle("-fx-background-color: black;");
        Scene scene = new Scene(new StackPane(svgShape), 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

在SVGPath上使用scaleX/Y.

您可以使用scaleX/scaleY来定义Shape的宽度和高度.

找到Shape的原始宽度/高度,然后通过将所需宽度/高度除以原始值来查找缩放系数.

Scaling factor for width = required width / original width

Scaling factor for height = required height / original height
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用此缩放系数将形状缩放到所需的大小.

shape.setScaleX(Scaling factor for width);
shape.setScaleY(Scaling factor for height);
Run Code Online (Sandbox Code Playgroud)

MCVE

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;

public class SVGPathResize extends Application {

    private static final double REQUIRED_WIDTH = 50.0;
    private static final double REQUIRED_HEIGHT = 30.0;

    @Override
    public void start(Stage primaryStage) throws Exception {
        SVGPath svg = new SVGPath();
        svg.setContent("M 289.00,74.00 C 299.18,61.21 307.32,52.80 320.00,42.42 331.43,33.07 343.66,26.03 357.00,19.84 427.64,-12.98 509.92,2.91 564.42,58.28 583.93,78.10 595.94,99.15 605.58,125.00 607.76,130.86 611.37,144.75 612.54,151.00 613.15,154.23 613.28,160.06 615.58,162.44 617.49,164.42 624.11,165.84 627.00,166.86 634.80,169.62 639.97,172.04 647.00,176.42 673.69,193.07 692.76,221.39 695.83,253.00 700.60,302.03 676.64,345.41 630.00,364.00 621.17,367.52 608.48,370.99 599.00,371.00 599.00,371.00 106.00,371.00 106.00,371.00 96.50,370.99 87.00,368.97 78.00,366.00 36.29,352.22 6.21,312.25 6.00,268.00 5.77,219.90 34.76,179.34 81.00,165.02 96.78,160.14 107.02,161.00 123.00,161.00 124.59,150.68 130.49,137.79 136.05,129.00 150.70,105.88 173.22,88.99 200.00,82.65 213.13,79.55 219.79,79.85 233.00,80.00 247.37,80.17 264.61,85.94 277.00,93.00 279.11,86.37 284.67,79.45 289.00,74.00 Z");
        resize(svg, REQUIRED_WIDTH, REQUIRED_HEIGHT);
        Scene scene = new Scene(new StackPane(svg), 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void resize(SVGPath svg, double width, double height) {

        double originalWidth = svg.prefWidth(-1);
        double originalHeight = svg.prefHeight(originalWidth);

        double scaleX = width / originalWidth;
        double scaleY = height / originalHeight;

        svg.setScaleX(scaleX);
        svg.setScaleY(scaleY);
    }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

在此输入图像描述

附加说明

矢量图形实际上没有大小

实际上,创建Shape时定义的路径实际上定义了矢量图的大小.

例如,我可以使用以下方法创建相同的形状:

 M 100 100 L 300 100 L 200 300 z
Run Code Online (Sandbox Code Playgroud)

M 10 10 L 30 10 L 20 30 z
Run Code Online (Sandbox Code Playgroud)

但后者比前者小10倍,因此其实际尺寸也小10倍.

如果我们将较小的三角形缩放10倍,它将会增长并且具有相同的大小.

在此输入图像描述