美文网首页
JavaFx 圆角边框图片

JavaFx 圆角边框图片

作者: Ggx的代码之旅 | 来源:发表于2017-11-02 09:34 被阅读480次

    群里问道一个JavaFx的需求,如何显示一个圆角边框图片。总不能让原来的图片直接就做成圆角的吧,这肯定不现实。最终在stackoverflow上有大神写出了自己的方案。在此纪录在案——先膜拜一下。原话如下:

    If you want to actually round the image itself, it's a bit trickier. You need to apply some code to:

    1. Clip the image to a rounded rectangle.
    2. Snapshot the clipped image.
    3. Store the snapshot image back in the ImageView.
    4. Remove the clip from the ImageView.
    5. Apply the drop shadow effect to the ImageView.

    Then you can get something like below:

    圆角图片

    Some code:

            @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);
            }
    

    欢迎共同探讨更多安卓,java,c/c++相关技术QQ群:392154157

    相关文章

      网友评论

          本文标题:JavaFx 圆角边框图片

          本文链接:https://www.haomeiwen.com/subject/fvfopxtx.html