【发布时间】:2019-01-18 05:30:04
【问题描述】:
我有一个 SVG 文件,我正在尝试使用 javafx 将它集成到一个按钮中,我成功了,但它保持很大的大小,在乞求文件是这样的
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M8 256c0 137 111 248 248 248s248-111 248-248S393 8 256 8 8 119 8 256zM256 40c118.7 0 216 96.1 216 216 0 118.7-96.1 216-216 216-118.7 0-216-96.1-216-216 0-118.7 96.1-216 216-216zm-32 88v64H120c-13.2 0-24 10.8-24 24v80c0 13.2 10.8 24 24 24h104v64c0 28.4 34.5 42.8 54.6 22.6l128-128c12.5-12.5 12.5-32.8 0-45.3l-128-128c-20.1-20-54.6-5.8-54.6 22.7zm160 128L256 384v-96H128v-64h128v-96l128 128z"/></svg>
所以我想我必须改变它的比例,所以我添加了一个特定的宽度和高度
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="150px" height="150px">...
我使用了git的SVGLoader,我写了这段代码来查看结果
package tutoFX;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import net.javainthebox.caraibe.svg.SVGContent;
import net.javainthebox.caraibe.svg.SVGLoader;
public class SVGLoaderSample extends Application {
@Override
public void start(Stage stage) {
SVGContent content = SVGLoader.load(getClass().getResource("arrow-alt-circle-right10.svg").toString());
// create a button and set the graphics node
Button button = new Button();
button.setGraphic(content);
// add the button to the scene and show the scene
HBox layout = new HBox(button);
HBox.setMargin(button, new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.setTitle("SVGLoader Sample");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
但不是像预期的那样,你们中的一些人可能会说使用这个
// Scale the image and wrap it in a Group to make the button
// properly scale to the size of the image
content.setScaleX(0.1);
content.setScaleY(0.1);
是的,它改变了图标的大小,但它使节点更大 enter image description here
感谢帮助
【问题讨论】:
-
是的,它会改变大小,但会使节点变大 - 使用较小的比例值。原始 svg 文件是 512x512px 盒子内的 496x496px 路径,因此尺寸是正确的。如果您不想更改原始文件并对其进行缩放,则必须使用
setScaleX和setScaleY以编程方式对其进行缩放。