ImageView上的Border-Radius和Shadow

Fra*_*oth 20 javafx

我想在JavaFX中应用border-radius和shadow.

在CSS3中它将是:

box-shadow: rgba(0,0,0,0.8) 0 0 10px;
border-radius: 3px;
Run Code Online (Sandbox Code Playgroud)

现在我想在JavaFX中使用它,但即使是border-radius也不能在JavaFX Scene Builder中工作.这是我的问题的屏幕截图:

JavaFX截图http://rapid-img.de/images/b92e2112.jpg

在屏幕截图中,您可以看到我使用:

-fx-border-radius: 10 10 10 10;
-fx-background-radius: 10 10 10 10;
Run Code Online (Sandbox Code Playgroud)

jew*_*sea 47

使用以下css获取投影:

-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅JavaFX CSS参考指南.

要获得投影以外的边框,请将包含图像的ImageView放在StackPane中.并将上面的效果css应用于StackPane,以及StackPane上的背景和填充.

例如,下面应用于包含ImageView的StackPane的css将在图像周围提供红色边框:

-fx-padding: 10;
-fx-background-color: firebrick;
Run Code Online (Sandbox Code Playgroud)

如果您希望定义边框的背景在边缘处弯曲,请使用:

-fx-background-radius: 5;
Run Code Online (Sandbox Code Playgroud)

这会为您提供如下图像,其中您的图像被包含在阴影边框中:

batmanlost

如果你想真正围绕图像本身,它有点棘手.您需要将一些代码应用于:

  1. 将图像剪切为圆角矩形.
  2. 快照剪裁的图像.
  3. 将快照图像存储回ImageView.
  4. 从ImageView中删除剪辑.
  5. 将阴影效果应用于ImageView.

然后你可以得到类似下面的东西:

whereisbatman

"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() {
            // set a clip to apply rounded border to the original image.
            Rectangle clip = new Rectangle(
                imageView.getFitWidth(), imageView.getFitHeight()
            );
            clip.setArcWidth(20);
            clip.setArcHeight(20);
            imageView.setClip(clip);

            // snapshot the rounded image.
            SnapshotParameters parameters = new SnapshotParameters();
            parameters.setFill(Color.TRANSPARENT);
            WritableImage image = imageView.snapshot(parameters, null);

            // remove the rounding clip so that our effect can show through.
            imageView.setClip(null);

            // apply a shadow effect.
            imageView.setEffect(new DropShadow(20, Color.BLACK));

            // store the rounded image in the imageView.
            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();
    }
}
Run Code Online (Sandbox Code Playgroud)

使用一些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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*son 7

如果你使用jewelsea提供的答案,那么一定要先测试是否支持剪辑:

Platform.isSupported(ConditionalFeature.SHAPE_CLIP)
Run Code Online (Sandbox Code Playgroud)

我试图避免使用条件特征,除非我必须使用它们.就我而言,我想拍一张照片.所以另一种方法是使用a Circle而不是ImageView:

Circle circle = new Circle(14);
ImagePattern pattern = new ImagePattern(myImage);
circle.setFill(pattern);
Run Code Online (Sandbox Code Playgroud)

如果支持,可以增强圆圈以使用阴影:

if (Platform.isSupported(ConditionalFeature.EFFECT)) {
    circle.setEffect(new DropShadow(8, Color.rgb(0, 0, 0, 0.8)));
}
Run Code Online (Sandbox Code Playgroud)