在我使用JavaFX开发的游戏中,我有带有Label
的卡(DropShadow
s)。卡可以移动,并且当它们移动时,阴影会留在一条微弱的痕迹后面。我试过使用Node#setCache(boolean)
,它可以工作,但会使标签中的文本模糊。
还有其他方法可以防止DropShadow
留下痕迹吗?
这是结果的MCVE:
public class DropShadowTest extends Application {
@Override
public void start(final Stage stage) throws Exception {
final Label label = new Label("Hello, world!");
label.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(10), Insets.EMPTY)));
label.setEffect(new DropShadow(3, 0, 1, Color.BLACK));
label.setMaxSize(100, 100);
label.setMinSize(100, 100);
stage.setScene(new Scene(new StackPane(label), 640, 480));
stage.show();
label.setOnMouseClicked(event -> {
final TranslateTransition anim = new TranslateTransition(Duration.seconds(1), label);
anim.setToX(Math.random() * 500 - 250);
anim.setToY(Math.random() * 300 - 150);
anim.play();
});
}
public static void main(final String[] args) {
Application.launch(args);
}
}
结果:
谢谢!
最佳答案
尝试对Cache hints使用缓存,例如,在开始动画之前,将缓存提示设置为SPEED
完成Animation后,您将切换回QUALITY,它将重新渲染您的Node(如swing中的repaint())