使用下面的CSS来获取一个阴影效果:
-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);
请参考JavaFX CSS 参考指南了解详细信息。
如果要除了投影效果外,还同时添加边框,请将包含图像的 ImageView 放置在 StackPane 中。然后将上面提到的效果css应用于 StackPane 上,并在 StackPane 上设置背景和填充。
例如,将下面的 css 应用于包含 ImageView 的 StackPane 将为图像提供红色边框:
-fx-padding: 10;
-fx-background-color: firebrick;
如果你想让边框的背景呈现圆形边缘,则使用以下代码:
-fx-background-radius: 5;
以下是您的图片被包含在具有阴影边框中的效果:
![batmanlost](https://istack.dev59.com/v1Yy8.webp)
如果您想要真正将图像本身变成圆形,则需要进行一些特殊处理。您需要应用某些代码来执行以下操作:
- 将图像剪切为圆角矩形。
- 对剪切后的图像进行快照。
- 将快照图像存储回 ImageView 中。
- 从 ImageView 中删除剪辑。
- 对 ImageView 应用投影效果。
然后,您就可以获得如下所示的效果:
![whereisbatman](https://istack.dev59.com/2Ps3g.webp)
"BatmanLost.java" 的一些代码:
import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.io.IOException;
public class BatmanLost extends Application {
class WingClipper {
@FXML
private ImageView imageView;
@FXML
public void initialize() {
Rectangle clip = new Rectangle(
imageView.getFitWidth(), imageView.getFitHeight()
);
clip.setArcWidth(20);
clip.setArcHeight(20);
imageView.setClip(clip);
SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
WritableImage image = imageView.snapshot(parameters, null);
imageView.setClip(null);
imageView.setEffect(new DropShadow(20, Color.BLACK));
imageView.setImage(image);
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"batmanlostinthemix.fxml"
)
);
loader.setController(new WingClipper());
Pane batman = loader.load();
stage.setTitle("Where's Batman?");
stage.setScene(new Scene(batman));
stage.show();
}
}
使用一些FXML "batmanlostinthemix.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="313.0" prefWidth="477.0" style="-fx-background-color: azure;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<ImageView fx:id="imageView" layoutX="29.0" layoutY="44.0" fitHeight="224.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="http://collider.com/wp-content/uploads/lego-batman-movie-dc-super-heroes-unite-1.jpg" />
</image>
</ImageView>
</children>
</AnchorPane>